I want to replace a @VERSION@
token in a java source file with a version before building (Gradle is my build system of choice).
In my current script a
I found existing answers somewhat unsatisfying, so here is my solution:
import org.apache.tools.ant.filters.ReplaceTokens
task processSource(type: Sync) {
from sourceSets.main.java
inputs.property 'version', version
filter(ReplaceTokens, tokens: [VERSION: version])
into "$buildDir/src"
}
compileJava {
source = processSource.outputs
}
This addresses various concerns as follows:
$buildDir/src
by the processSource
task, which mirrors the standard processResources
.sourceSets.main.java.srcDirs
remains the default value and there is no sleight of hand in specifying a location that does not (yet) existfilter
with include/exclude patterns.src
, the directory under $buildDir
where the processed source files are put.inputs.property
.Sync
rather than Copy
so that files deleted from source are deleted from the filtered source as well (thanks, @Earthcomputer).