Gradle task replace string in .java file

前端 未结 3 1188
醉话见心
醉话见心 2020-12-03 10:31

I want to replace few lines in my Config.java file before the code gets compiled. All I was able to find is to parse file through filter during copying it. As soon as I have

3条回答
  •  情歌与酒
    2020-12-03 10:53

    To complement lance-java's answer, I found this idiom more simple if there's only one value you are looking to change:

    task generateSources(type: Copy) {
        from 'src/replaceme/java'
        into "$buildDir/generated-src"
        filter { line -> line.replaceAll('xxx', 'aaa') }
    }
    

    Caveat: Keep in mind that the Copy task will only run if the source files change. If you want your replacement to happen based on other conditions, you need to use Gradle's incremental build features to specify that.

提交回复
热议问题