Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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