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

后端 未结 5 1257
孤街浪徒
孤街浪徒 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:10

    I faced the same problem. And I was unable to find working solution. None of the examples I found worked. I believe it might to a fact, that I am new to Gradle and those examples omit some obvious pieces of code. So I looked into Gradle and found my own solution, that I'd like to share:

    import org.apache.tools.ant.filters.ReplaceTokens
    
    // Change compiler source directory
    sourceSets {
        main {
            java {
                srcDirs = ['build/src/main/java']
            }
        }
    }
    
    // Prepare sources for compilation
    task prepareSources(type: Copy) {
        from('src/main/java')
        into('build/src/main/java')
        filter(ReplaceTokens, tokens: [pluginVersion: version])
    }
    
    // Prepare sources, before compile
    compileJava {
        dependsOn prepareSources
    }
    

提交回复
热议问题