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

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?

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.

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

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

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.

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

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!

As an example, the java plugin adds two JavaCompile tasks (compileJava and compileTestJava)