Example of implicit task dependencies through task input/outputs?


(Doug Lethin) #1

Can someone provide an example or reference to an example that shows how to make a task dependent on the output of another task?

As an example, suppose I have one task A that unzips a dependency into a directory. I then have another task B that copies some files out of that directory. Rather than explicitly making task B depend on A, it’s my understanding that tying the input of task B to the output of task A implicitly forms a dependency and also take better advantage of which tasks are considered up-to-date.

For some reason, I’m having trouble finding the right google search terms to find an example of this.

I know I saw Hans demo this in one of his Gradle talks.

thanks in advance.

Doug


(Peter Niederwieser) #2

If the input property accepts a ‘FileCollection’, you can use ‘inputProperty = otherTask.outputs.files’. In case of ‘Copy#from’, ‘from otherTask’ should also work.


(Doug Lethin) #3

This is great, thanks Peter. I noodled on this some more after submitting the question and stumbled upon the same answer as well. This just worked:

(Note this is a contrived example and not exactly what I’m using this for…

task unpackConfig(type: Copy, dependsOn: configurations.config) {
  from zipTree(configurations.config.singleFile)
  into "${buildDir}/config-src"
}
  task generateRuntimeConfig(type: Copy) {
  from tasks.unpackConfig
  into "${targetConfigDir}"
}
gradle clean genConfig
:clean
:unpackConfig
:generateConfig
  BUILD SUCCESSFUL

Very cool. Thanks.


(Peter Niederwieser) #4

‘tasks.’ can be omitted, if you want. ‘zipTree(…)’ should be wrapped in curly braces so that the configuration doesn’t get resolved at configuration time. Instead of ‘into “${targetConfigDir}”’, ‘into targetConfigDir’ should be used.