# Execute task immediately after some other task

**URL:** <https://discuss.gradle.org/t/execute-task-immediately-after-some-other-task/20276>\
**Category:** Help/Discuss\
**Created:** [November 1, 2016, 3:36pm UTC](https://discuss.gradle.org/t/execute-task-immediately-after-some-other-task/20276 "2016-11-01T15:36:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Tsvetkov](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Alexey\_Tsvetkov](https://discuss.gradle.org/u/Alexey_Tsvetkov)\
**Post date:** [November 1, 2016, 3:36pm UTC](https://discuss.gradle.org/t/execute-task-immediately-after-some-other-task/20276/1 "2016-11-01T15:36:36Z")

</div>

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:

```gradle
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?

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [November 1, 2016, 5:07pm UTC](https://discuss.gradle.org/t/execute-task-immediately-after-some-other-task/20276/2 "2016-11-01T17:07:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsvetkov](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Alexey\_Tsvetkov](https://discuss.gradle.org/u/Alexey_Tsvetkov)\
**Post date:** [November 1, 2016, 5:24pm UTC](https://discuss.gradle.org/t/execute-task-immediately-after-some-other-task/20276/3 "2016-11-01T17:24:00Z")

</div>

> 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).

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [November 2, 2016, 6:59am UTC](https://discuss.gradle.org/t/execute-task-immediately-after-some-other-task/20276/4 "2016-11-02T06:59:25Z")

</div>

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
