Skip to content

Instantly share code, notes, and snippets.

@ysb33r
Last active February 28, 2016 21:57
Show Gist options
  • Save ysb33r/4034466ce638c92a3700 to your computer and use it in GitHub Desktop.
Save ysb33r/4034466ce638c92a3700 to your computer and use it in GitHub Desktop.
Using a Gradle Task as a data-driven file generator
// Uses a single Groovy template and multiple sets of text input files.
// It generates a set of Groovy source files using the content of the text input files to set tokens within the
// template file.
task generateGroovySources( type : Copy ) {
from 'examples', {
include '*.txt'
}
into "${buildDir}/generated-src"
eachFile { fcd ->
String sourceName = fcd.sourceName.replaceAll( /\.txt$/, '')
String content = fcd.file.text
fcd.filter {null}
fcd.filter ConcatFilter, append : project.file('src/templates/my.template')
fcd.filter ReplaceTokens, tokens : [
token1 : content,
token2 : sourceName
]
}
rename ~/\.txt$/, '.groovy'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment