Task outputs based on dependent task outputs

Hi all,

I’ve done some searching in Gradle docs and previous discussions and I haven’t been able to find an answer to this. I hope it’s a simple question.

I have a Task A which depends on Task B and Task C. All these tasks are custom with specified inputs and outputs.

task A (dependsOn: [ 'B', 'C']) {
      ...
}

I want the output for A to be an aggregation of outputs from B+C, but I haven’t been able to work out how to do that.

This seems like it should have a simple and obvious solution that I’m just not seeing. Any help is appreciated.

So this is the only thing I’ve been able to get to work. I’m sure there must be an easier way to do this that I’m not getting.

task A( dependsOn:[B, C]) {
    outputs.dir B.outputs.files.filter( f->{ f.directory })
    outputs.dir C.outputs.files.filter( f->{ f.directory })
    outputs.files B.outputs.files.filter( f->{ f.file })
    outputs.files C.outputs.files.filter( f->{ f.file })
}

The actual answer is, do not even try to do that.
You should never have overlapping task outputs.
Instead make A a task of type Sync, copying from B and C to some dedicated destination directory.
This way you also do not need an explicit dependsOn which is bad practice except for lifecycle tasks.
So you want something like

tasks.register('A', Sync) {
    from(B)
    from(C)
    into(layout.buildDirectory.dir('a')
}

Thanks, @Vampire. I played around a little further and came to the same conclusion. Clearly, I was trying to swim against the current with this idea. Always a signal that there’s an easier, different approach.

Is tasks.register preferred for gradle these days over a task declaration (as in my example)? Is there a short explanation as to why?

task A is syntactic sugar for tasks.create('A') which is against task configuration avoidance: Task Configuration Avoidance

1 Like