Hi,
I am trying to write a gradle plugin that has a task which uses the configuration values coming from the extension object as input and outputs of the task. It seems the extension object values are not available at “configuration” phase which is when the inputs and outputs of the task are defined. SO I am lost as to how to define the inputs/outputs of the task. Below is the code I am testing with
You can use a task’s convention mapping. However, note that it is an internal component of Gradle which could be modified or removed from Gradle sometime in the future.
Note, I also changed your implementation from using Strings, to any object. Doing so allows users of your plugin to use a wider range of types without you needing to do any additional work. In this case, everything supported by Project.file() will work (for example, I defaulted the output using a closure that generates a string).
class MyPluginExtension {
def input = "default input"
def output = {
["default", "output"].join(" ")
}
}
class MyTask extends DefaultTask {
@InputDirectory
def input
@OutputDirectory
def output
@TaskAction
void doSomething() {
// In order for convention mapping to work, it is
// critical that you call the getters explicitly.
// Accessing the fields directly will not work.
println " input = ${input} (${input ? input.class.name : 'N/A'})"
println " getInput = ${getInput()} (${getInput() ? getInput().class.name : 'N/A'})"
println " input file = ${project.file(getInput())}"
println " output = ${output} (${output ? output.class.name : 'N/A'})"
println " getOutput = ${getOutput()} (${getOutput() ? getOutput().class.name : 'N/A'})"
println " output file = ${project.file(getOutput())}"
}
}
class MyPlugin implements Plugin<Project> {
void apply(Project project) {
MyPluginExtension e = project.extensions.create("myplugin", MyPluginExtension)
MyTask t = project.tasks.create('test', MyTask)
// Using closures here is key.
t.conventionMapping.input = {e.input}
t.conventionMapping.output = {e.output}
}
}
apply plugin: MyPlugin
// Experiment with un/commenting these values to see the effects.
myplugin {
input = "src1"
}
test {
//input = file('src2')
}