ClassCastException in resource filtering

I have written a Copy task with ant’s ReplaceTokens filter used.

snippet of build.gradle

ext {

appVersion=“1.0”

baseDir=file("/app/foo")

srcDir="$baseDir/src"

buildDir="$baseDir/build" } task complexCopy(type: Copy){

from “$srcDir”{

include(“build.xml”)

}

into “$buildDir”

filter(

ReplaceTokens, tokens: [“app.version”: “$appVersion”, “user”:“admin”]

) }

And I am getting following errors - Could not copy ‘…/src/build.xml’ to ‘…/build/build.xml’

stack trace -

Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ‘:complexCopy’.

at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)

at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)

at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)

at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)

at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)

at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)

at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)

at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)

at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)

at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:296)

at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)

at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)

at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)

at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)

at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:86)

at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)

at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)

at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)

at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:67)

at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)

at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:61)

at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:54)

at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:148)

at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:105)

at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:85)

at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:81)

at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)

at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)

at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:39)

at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:29)

at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)

at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:171)

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)

at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)

at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)

at org.gradle.launcher.Main.doAction(Main.java:33)

at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)

at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)

at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)

at org.gradle.launcher.GradleMain.main(GradleMain.java:23) Caused by: org.gradle.api.GradleException: Could not copy file ‘…/src/build.xml’ to ‘’…/build/build.xml"

at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:79)

at org.gradle.api.internal.file.copy.DefaultFileCopyDetails.copyTo(DefaultFileCopyDetails.java:102)

at org.gradle.api.internal.file.copy.FileCopyAction$FileCopyDetailsInternalAction.processFile(FileCopyAction.java:44)

at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator$1$1.processFile(NormalizingCopyActionDecorator.java:65)

at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator$1$1.processFile(DuplicateHandlingCopyActionDecorator.java:60)

at org.gradle.api.internal.file.copy.CopyFileVisitorImpl.processFile(CopyFileVisitorImpl.java:60)

at org.gradle.api.internal.file.copy.CopyFileVisitorImpl.visitFile(CopyFileVisitorImpl.java:44)

at org.gradle.api.internal.file.collections.DirectoryFileTree.walkDir(DirectoryFileTree.java:157)

at org.gradle.api.internal.file.collections.DirectoryFileTree.visitFrom(DirectoryFileTree.java:125)

at org.gradle.api.internal.file.collections.DirectoryFileTree.visit(DirectoryFileTree.java:115)

at org.gradle.api.internal.file.collections.FileTreeAdapter.visit(FileTreeAdapter.java:96)

at org.gradle.api.internal.file.CompositeFileTree.visit(CompositeFileTree.java:54)

at org.gradle.api.internal.file.copy.CopySpecActionImpl.execute(CopySpecActionImpl.java:37)

at org.gradle.api.internal.file.copy.CopySpecActionImpl.execute(CopySpecActionImpl.java:24)

at org.gradle.api.internal.file.copy.DefaultCopySpec$DefaultCopySpecResolver.walk(DefaultCopySpec.java:498)

at org.gradle.api.internal.file.copy.DefaultCopySpec$DefaultCopySpecResolver.walk(DefaultCopySpec.java:500)

at org.gradle.api.internal.file.copy.DefaultCopySpec.walk(DefaultCopySpec.java:322)

at org.gradle.api.internal.file.copy.DelegatingCopySpecInternal.walk(DelegatingCopySpecInternal.java:206)

at org.gradle.api.internal.file.copy.CopySpecBackedCopyActionProcessingStream.process(CopySpecBackedCopyActionProcessingStream.java:36)

at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator$1.process(DuplicateHandlingCopyActionDecorator.java:44)

at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator$1.process(NormalizingCopyActionDecorator.java:56)

at org.gradle.api.internal.file.copy.FileCopyAction.execute(FileCopyAction.java:35)

at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator.execute(NormalizingCopyActionDecorator.java:52)

at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator.execute(DuplicateHandlingCopyActionDecorator.java:42)

at org.gradle.api.internal.file.copy.CopyActionExecuter.execute(CopyActionExecuter.java:38)

at org.gradle.api.internal.file.copy.FileCopier.doCopy(FileCopier.java:64)

at org.gradle.api.internal.file.copy.FileCopier.copy(FileCopier.java:49)

at org.gradle.api.internal.file.DefaultFileOperations.copy(DefaultFileOperations.java:136)

at org.gradle.api.internal.project.AbstractProject.copy(AbstractProject.java:727)

at org.gradle.groovy.scripts.DefaultScript.copy(DefaultScript.java:158)

at build_36h0liimlpgmab3cier313gvcq$_run_closure5.doCall()

at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:539)

at org.gradle.api.internal.AbstractTask$ClosureTaskAction.execute(AbstractTask.java:520)

at org.gradle.api.internal.tasks.TaskMutator$1.execute(TaskMutator.java:77)

at org.gradle.api.internal.tasks.TaskMutator$1.execute(TaskMutator.java:73)

at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)

at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)

… 44 more Caused by: java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String

at org.apache.tools.ant.filters.ReplaceTokens.read(ReplaceTokens.java:165)

at org.apache.tools.ant.filters.BaseFilterReader.read(BaseFilterReader.java:83)

at org.apache.tools.ant.util.ReaderInputStream.read(ReaderInputStream.java:117)

at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025)

at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:58)

at org.gradle.api.internal.file.copy.DefaultFileCopyDetails.copyTo(DefaultFileCopyDetails.java:94)

at org.gradle.api.internal.file.AbstractFileTreeElement.copyFile(AbstractFileTreeElement.java:93)

at org.gradle.api.internal.file.AbstractFileTreeElement.copyTo(AbstractFileTreeElement.java:74)

… 80 more

BUILD FAILED

I had found one work around as follows - tokens: [“app.version”: “$appVersion”.toString(), “user”:“admin”.toString()]

But I guess there should be something else rather than toString().

Any ideas?

ReplaceFilters class doesn’t know how to handle ‘GString’ token values, you’ll have to rewrite your filter configuration to:

filter(ReplaceTokens, tokens: ["app.version": appVersion, "user":"admin"])

as ‘appVersion’ is a ‘String’ anyway. Just don’t pass 'GString’s as tokens and using ‘toString()’ on a ‘GString’ is a totally valid option.