Skip to content

Instantly share code, notes, and snippets.

@lizhangqu
Last active August 21, 2017 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lizhangqu/364229c0994998684a11bad9a11bb123 to your computer and use it in GitHub Desktop.
Save lizhangqu/364229c0994998684a11bad9a11bb123 to your computer and use it in GitHub Desktop.
java assemble filter for java source, java resources
apply plugin: JavaFilterPlugin
class JavaFilterPlugin implements Plugin<Project> {
void apply(Project project) {
project.afterEvaluate {
if (project.file('filter.properties').exists()) {
def processResources = project.tasks.findByName("processResources")
processResources.configure {
from(processResources.source) {
filesMatching('*') {
Properties properties = new Properties()
properties.load(new FileInputStream(project.file('filter.properties')))
filter(ReplaceTokens, tokens: properties, beginToken: '${', endToken: '}')
}
}
}
def compileJava = project.tasks.findByName("compileJava")
def filterJavaSources = project.tasks.create("filterJavaSources", Copy) {
from(compileJava.source) {
filesMatching('**/*.java') {
Properties properties = new Properties()
properties.load(new FileInputStream(project.file('filter.properties')))
filter(ReplaceTokens, tokens: properties, beginToken: '${', endToken: '}')
}
}
into "$project.buildDir/generated-java-src"
}
compileJava.configure {
dependsOn filterJavaSources
setSource(filterJavaSources.outputs.files)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment