Reputation: 3301
When apply plugin: 'java'
in Gradle, it defines among other tasks processResources
.
I want to substitute the token/parameter in one of the resource file during processResources
.
I have the following copy task defined in my build.gradle
:
import org.apache.tools.ant.filters.ReplaceTokens
task initConfig(type: Copy) {
from("src/resources/assets/js") {
include 'config.js'
filter(ReplaceTokens, tokens: [host: "${System.env.HOST ?: 'localhost:58080'}" as String])
}
into "$sourceSets.main.output.resourcesDir/assets/js"
}
It works fine if I run gradle -q run initConfig
.
What I want is that this task to be run as part of processResources
task, I've tried to put the same logic as following:
processResources << {
// same code goes here
}
or
processResources {
doLast {
// same code goes here
}
}
None of them works. So, if run gradle -q clean processResources
resources are copied, but the token is not replaced.
I don't want to force explicitly run/add initConfig
when running the build, so ideally gradle -q build
should do everything necessary.
I believe it is possible, as build
task itself defines the way to run multiple tasks during its execution. Could not find yet how.
Upvotes: 9
Views: 13960
Reputation: 84786
processResources
is (a you can view in the docs) a plain task of type Copy
. Hence you can configure it exactly in the same way as you're configuring other copy tasks. This will be enough to filter the file:
import org.apache.tools.ant.filters.ReplaceTokens
apply plugin: 'java'
processResources {
filter(ReplaceTokens, tokens: ['token.to.replace': 'zombo.com'])
}
Here you can find a simple demo.
Upvotes: 6
Reputation: 29
ProcessResources is a task provided by java-plugin, you don't need to add << for modifying it's behaviour. Simply try :
processResources{
//same Code goes here
}
Another workaround could be to add a dependency between processResources and initConfig like this :
processResources{
dependsOn initConfig
}
This will execute initConfig before processResources.
Hope this help.
Upvotes: 1