# How call TaskInternal.execute() when gradle update to 5.0+

**URL:** <https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472>\
**Category:** Help/Discuss\
**Created:** [October 10, 2019, 4:24am UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472 "2019-10-10T04:24:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidTangWei](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@DavidTangWei](https://discuss.gradle.org/u/DavidTangWei)\
**Post date:** [October 10, 2019, 4:24am UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/1 "2019-10-10T04:24:56Z")

</div>

I have written a gradle plugin for my personal use. The target of this plugin is writing code dynamic and compiling and generating jar.  
In the version of 4.9, I get “compileJave” task during running, set this task source code directory and target directory, then call execute() method for compiling.  
But when I upgrade the version of gradle to 5.6.2, the execute method has been deprecated. I look for the solution recently, but the solution (set depends on) on the website seem not fit me. Can anyone help me?

---

<div class="post-metadata">

**Author:** ![smac89](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/smac89/32/6887_2.png) [@smac89](https://discuss.gradle.org/u/smac89)\
**Post date:** [October 10, 2019, 6:39am UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/2 "2019-10-10T06:39:00Z")

</div>

You should not arbitrarily call “execute” on an existing task. I recommend you use `dependsOn` or other dependency hooks to schedule when another action should happen. Alternatively, each task can be configured with a custom action by means of `doFirst`, `doLast`, etc.

---

<div class="post-metadata">

**Author:** ![Stefan\_Wolf](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/stefan_wolf/32/5519_2.png) [@Stefan\_Wolf](https://discuss.gradle.org/u/Stefan_Wolf)\
**Post date:** [October 10, 2019, 9:10am UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/3 "2019-10-10T09:10:50Z")

</div>

@DavidTangWei It sounds like you want to add a new `JavaCompile` task instead of calling `javaCompile.execute()`.

---

<div class="post-metadata">

**Author:** ![DavidTangWei](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@DavidTangWei](https://discuss.gradle.org/u/DavidTangWei)\
**Post date:** [October 10, 2019, 2:58pm UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/4 "2019-10-10T14:58:18Z")

</div>

I think you are right. I need to refactor my code entirely. Thank you for replying.

---

<div class="post-metadata">

**Author:** ![DavidTangWei](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@DavidTangWei](https://discuss.gradle.org/u/DavidTangWei)\
**Post date:** [October 10, 2019, 3:03pm UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/5 "2019-10-10T15:03:42Z")

</div>

Actually, I want to reuse `JavaCompile` Task. I set `sourceDir` and `destinationDir` of this task by reflection. So when I call `javaCompile.execute()`, this task will compile my code which generated by this source project.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [October 10, 2019, 10:12pm UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/6 "2019-10-10T22:12:55Z")

</div>

That’s not how Gradle works… Each task will run either once or not at all for each build execution. A task will never run twice (unless you hack a call to Task.execute())

If you want to run a task with two sets of inputs/outputs you’ll need two separate task instances

---

<div class="post-metadata">

**Author:** ![DavidTangWei](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@DavidTangWei](https://discuss.gradle.org/u/DavidTangWei)\
**Post date:** [October 11, 2019, 3:33am UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/7 "2019-10-11T03:33:20Z")

</div>

The purpose of my plugin is writing code automatically, compiling this codes and generating the jar for my teammate use. I combined all this jobs in one Task. So I have to reuse the `CompileJava` and `Jar` task. Like you said, maybe I need to split this jobs into different tasks. Thank you!

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [October 11, 2019, 9:51am UTC](https://discuss.gradle.org/t/how-call-taskinternal-execute-when-gradle-update-to-5-0/33472/8 "2019-10-11T09:51:17Z")

</div>

As an example, the [java plugin](https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_tasks) adds two `JavaCompile` tasks (`compileJava` and `compileTestJava`)
