Execute task immediately after some other task


(Alexey Tsvetkov) #1

I have a task X that is not created by my plugin.
Some other tasks depend on the task X (also not created by my plugin).
I need to make a task Y that depends on X and can add some outputs to X’s output,
so all tasks that depend on X should depend on Y.

Possible solutions:

  1. Find tasks that depend on X by name/regex. It works now, but potentially fragile solution.
  2. Use ‘X.finalizedBy(Y)’. It seems that a finalizer task is not guaranteed to be executed immediately after a finalizing task, though often it does.
  3. Enumerate all tasks; check that X in ‘task.taskDependencies.getDependencies(task)’. This seems to be forcing configuration resolution and breaking Android plugin. There was a warning before 3.0:
Changed dependencies of configuration ':[module]:default' after it has been included in dependency resolution. This behaviour has been deprecated and is scheduled to be removed in Gradle 3.0.

Is there any other way to make this work that is reliable and generic?


(Stefan Oehme) #2

Hi Alexey,

if you want to add more logic to an existing task, you should add an action with task.doLast {}. Changing the output from some other task would break up-to-date checks.

Cheers,
Stefan


(Alexey Tsvetkov) #3

Changing the output from some other task would break up-to-date checks

As far as I understand it is not quite so. Outputting the same file from two tasks would probably break up-to-date checks.
However if tasks outputs do not intersect, up-to-date checks would not be broken.
Otherwise, incremental joint compilation of java and { groovy, scala, kotlin } would be impossible (they all are copying to compileJava.destinationDir).


(Stefan Oehme) #4

It’s true that extra files work with simple up to date checking. For the upcoming distributed cache this won’t be enough though. The joint compilation case is a good example of something that needs to be fixed. Tasks with overlapping outputs are not cacheable.

It’s not really joint compilation by the way, there is a strict order and the two can’t reference both ways. True joint compilation only works for Java fails in src/main/groovy