Hi guys,
This might look like a noob question, but the documentation is unclear about the topic and I can’t find an example in the Gradle source base… What I want is somehow simple: getting the task configuration from extension, with output/input specified.
Basically, I want something like this:
jbake {
input=file(...)
output=file(...)
}
and I have a ‘bake’ task, so ‘jbake’ is an extension (‘JBakePluginExtension’) and ‘bake’ is a task of type ‘JBakeTask’. So far so good, I tried several versions but I miss the relation between the extension and the task parameters.
For example if I do:
class JBakePluginExtension {
File input
File output
Map<String, Object> configuration = [:]
boolean clearCache = false
JBakePluginExtension(Project project) {
input = project.file('src/jbake')
output = project.file("${project.buildDir}/jbake")
}
}
class JBakeTask extends AbstractTask {
@TaskAction
void bake() {
new Oven(project.jbake.input, project.jbake.output, project.jbake.clearCache).with {
config = new CompositeConfiguration([new MapConfiguration(project.jbake.configuration), config])
setupPaths()
bake()
}
}
}
then I get the configuration from the extension (yeah!) but it doesn’t handle input/outputs (incremental builds). I tried this:
class JBakePluginExtension {
@InputDirectory File input
@OututDirectory File output
@Input Map<String, Object> configuration = [:]
boolean clearCache = false
JBakePluginExtension(Project project) {
input = project.file('src/jbake')
output = project.file("${project.buildDir}/jbake")
}
}
but it doesn’t work. If I change it to:
class JBakeTask extends AbstractTask {
@InputDirectory File input
@OutputDirectory File output
@TaskAction
void bake() {
prepareTask()
new Oven(input, output, clearCache).with {
config = new CompositeConfiguration([new MapConfiguration(configuration), config])
setupPaths()
bake()
}
}
private void prepareTask() {
if (input==null) {
input = project.jbake.input
}
if (output==null) {
output = project.jbake.output
}
}
}
which has a lot of boilerplate code (and that doesn’t seem right because I’m defining twice the same fields) then running the task fails with: “No value has been specified for property ‘input’.”
So the question is quite simple: what is the idiomatic way to create a task which takes configuration from an extension, defines inputs/outputs and allows overriding some configuration keys?
Thank you!