How bind my task to specific stage of Gradle build

I need bind my task ;‘cleanIgm’ to stage clean whish I see in list of stages of Java plugin.
I write like
task cleanAll(type: Delete) {
def tree = fileTree("$rootDir")
tree.include '**/*.jar’
tree.each { it.delete() }
}

When I run gradle clean this task shoul be run, correct?
How it works?
Stage clean of Java plugin bound to Delete task? How they connected each other?

Thanks

Stages are a maven concept. In gradle you have a graph of tasks, so you can just call ‘taskBeforeWhichToCallMe.dependsOn(myCustomTask)’.

Thanks. It’s diffiсult to me switching to Gradle from Maven.
If I write my own task which extends embedded task:

task fatJar(type: Jar) {
baseName = project.name + '-all’
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

it’ll be run on Jar stage automatically or not? Or I have to instruct Gradle to see and run my task somewhere inside other embedded tasks - like it’s being done in Ant?

Again, there is no Jar stage. Stages are a Maven concept. If you want it to run automatically when, for example, the assemble task (which is added to the build by the java plugin) is run, then use

assemble.dependsOn fatJar

Otherwise, you’ve just added a task to the build, which will only be invoked when executing gradle fatJar

Thanks for explanation

Two years later I read my reply and I realize that the concept in Gradle that mirrors the Maven “stages” is the “lifecycle tasks” - go and look them up.

In short, these are 5 or so empty tasks (such as: assemble, check, build, clean), created by the BasePlugin, to which many other plugins add dependencies.

Use them in the same way you would use Maven’s lifecycle - to express generic hook. Just keep in mind that in Gradle it is not considered poor style to define explicit dependencies to other tasks and their outputs (and it is poor style to rely on fifo dependency execution or other scheduling implementation details)