Don’t try to call tasks explicitly.
This never really worked properly.
For example using the execute method checks for dependency tasks were not done or check for multiple execution and so on.
Afair execute was never meant to be public API and all usages of it were simply wrong.
Instead in Gradle you register tasks (not create, to leverage task configuration avoidance) and wire task outputs to task inputs or similar places to then automatically get the necessary task dependencies without ever writing a dependsOn manually, except with a lifecycle task on the left-hand side.
Thank you for your quick response. Apologies, I’m a gradle beginner and Im trying to update an existing code.
i understand that calling out execute() is not the best practise and at this point of time i wouldn’t want to change a whole lot of code.
I removed the execute() and tried dependsOn() finalizedBy(), Though the tasks are getting executed, I see no compiled .class files in the destination dir.
The implementation & bundling is defined in the following implementation.groovy file:
**implementation.groovy**
class implementation extends DefaultTask {
**@TaskAction**
def generate(){
#process 1
#process 2
// Here after process2, I would like to compile the processed .java files. So i'm creating a gradle task of type: JavaCompile with execute()
project.tasks.create (type: JavaCompile, name: 'compileSrcFIles') {
source <source dir>
destinationDir <destination>
classpath = project.sourceSets.main.compileClasspath
}.execute()
#process3
// Uses the compiled .class files from the above task and execution continues
}
}
Now if i remove the .execute() i see that the task is not getting executed and just compiled.
I tried using finalizedBy(), dependsOn & mustRunAfter() by creating a dummy task (void task with no real purpose)
How can i call a JavaCompile task inside the @TaskAction() block of implementation.groovy?
You cannot create a new task during the execution of one task.
You either need to register the task where you also register that task and wire them together,
or if you want to do the compilation as part of that task, inject a JavaCompiler and use that, similar to how it is shown at Toolchains for JVM projects with JavaLauncher.
**implementation.groovy**
class implementation extends DefaultTask {
**@TaskAction**
def generate(){
#process 1
#process 2
//The above 2 process returns the source & destionation dir respectively.
project.tasks.getByName(compileSrcFIles)
#process3
// Uses the compiled .class files from the above task and execution continues
}
}
I understand that i can’t create a task inside the @TaskActions() block, but if i register a task in build.gradle file can i call it inside the @TaskActions() block?
If Yes, How would i pass src, destionation & classpath to the above mentioned task?
Case 2:
**implementation.groovy**
class implementation extends DefaultTask {
def src, destination, classpath
**@TaskAction**
def generate(){
#process 1
#process 2
// some way to call the task registered below?
task.getByName('compileSrcFIles')
#process3
}
// creating tasks outside the @TaskAction() block & global variables are initialised inside the block
project.tasks.register (type: JavaCompile, name: 'compileSrcFIles') {
source <src>
destinationDir <destination>
classpath = project.sourceSets.main.compileClasspath
}
No, again, there is no supported way to call a task at will.
If you need to do this action at that point either do not use a task, but use the compiler directly, either through toolchains or by using the java compiler programmatically directly,
or you probably need to split your stuff into three tasks, a task that runs before the compiler task and a task that runs after the compiler task.