Update dependencies from within a custom task


(Norman Walsh) #1

I hope this is an easy one. I have a custom task (written in Groovy) with options for the primary input and primary output files.

task myTask(type: MyCustomTask) {
  input("primary", "somefile")
  output("result", "someotherfile)
}

That’s fine, but of course I want Gradle to know about this dependency, so what I actually end up writing is

task myTask(type: MyCustomTask) {
  inputs.file "somefile"
  outputs.file "someotherfile"
  input("primary", "somefile")
  output("result", "someotherfile)
}

Which is annoying and redundant. Is there any way for the implementation of input() and output() to put their arguments into inputs and outputs automatically?


(uklance) #2

Of course, the input(...) and output(...) methods are your custom methods, they can delegate through to TaskInputs and TaskOutputs methods in a similar way to CopyTask.from(...) or CopyTask.into(...) etc


(Sterling Greene) #3

You can use annotations to mark properties of your task as inputs/outputs:

https://guides.gradle.org/implementing-gradle-plugins/#benefiting_from_incremental_tasks


(uklance) #4

Agreed, annotations are often less verbose than explicitly invoking TaskInputs/TaskOutputs methods