Replace token in file before building, but keep token in sources

后端 未结 5 1250
孤街浪徒
孤街浪徒 2021-02-12 03:33

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

5条回答
  •  甜味超标
    2021-02-12 03:53

    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:

    1. Unlike @Opal's answer, the main source remains unmolested; instead it is staged with modifications to $buildDir/src by the processSource task, which mirrors the standard processResources.
    2. Unlike @Gregory Stachowiak's answer, sourceSets.main.java.srcDirs remains the default value and there is no sleight of hand in specifying a location that does not (yet) exist
    3. Unlike @Raffaele's answer, there is no separate task set for release vs other builds. I disagree that separating them is desirable; I think the added complexity is not worth it unless you have measured any performance hit and found it to be unacceptable. Before going with @Raffaele's solution I would even for instance prefer to limit the scope of the filter with include/exclude patterns.
    4. Task dependencies are implicitly defined via outputs.
    5. All locations are taken from defaults and nothing is stringly typed. The only magic value here is src, the directory under $buildDir where the processed source files are put.
    6. (Edit: added 2019/1/12) Other answers do not properly handle situations where only the version has changed. Changing the version should, by itself, invalidate the task output. This is accomplished via inputs.property.
    7. (Edit 2019/5/20) Uses Sync rather than Copy so that files deleted from source are deleted from the filtered source as well (thanks, @Earthcomputer).

提交回复
热议问题