amitbansalite
amitbansalite

Reputation: 309

Gradle: replace tokens with project properties

I have resource file with some tokens that need to be replaced with file paths using the java plugin task 'processResources'. Does gradle provide a way in which, the token value is an expression?

1) src/resources/config.properties file has a token: SRC_ROOT_DIR = @SRC_ROOT_DIR@

2) build.gradle file contains the following:

processResources{
   filter ReplaceTokens, tokens:[SRC_ROOT_DIR: project.projectDir]
}

This throws an exception saying : could not copy file '..src\resources\config.properties' to '..build\resources\main\config.properties'

Upvotes: 3

Views: 8432

Answers (1)

Ori Dar
Ori Dar

Reputation: 19000

Running your snippet with --stacktrace gave me:

...

Caused by: org.gradle.api.GradleException: Could not copy file 
'C:\bss\zz\src\main\resources\config.properties' 
to 'C:\bss\zz\build\resources\main\config.properties'.

...

Caused by: java.lang.ClassCastException: 
java.io.File cannot be cast to java.lang.String
...

Changing project.projectDir to project.projectDir.name (or path) solved it:

filter ReplaceTokens, tokens:[SRC_ROOT_DIR: project.projectDir.path]

Upvotes: 8

Related Questions