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

后端 未结 5 1249
孤街浪徒
孤街浪徒 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 04:13

    You only need to replace @VERSION@ tokens before releasing your software to the public. Here I defined a task compileForRelease that accomplishes it:

    import org.apache.tools.ant.filters.ReplaceTokens
    
    task sourcesForRelease(type: Copy) {
        from 'src/main/java'
        into 'build/adjustedSrc'
        filter(ReplaceTokens, tokens: [VERSION: '2.3.1'])
    }
    
    task compileForRelease(type: JavaCompile, dependsOn: sourcesForRelease) {
        source = sourcesForRelease.destinationDir
        classpath = sourceSets.main.compileClasspath
        destinationDir = file('build/adjustedClasses')
    }
    

    I don't recommend messing with standard tasks defined by the Java plugin because that would add unnecessary overhead to each and every build.

提交回复
热议问题