How to task dependency on clean task that behaves same way as "primary task" execution

Hi,

I am doing a migration of a project from Ant to Gradle. In the Gradle version I want to create an alias “clean.all” to the clean task.

When executing clean as the “primary task” on the command line Gradle builds a DAG with all the clean task of every sub-project in the build. How can I create the alias task that does exactly the same behavior of creating the DAG hooking in all the other clean tasks. Is there a one line elegant way or is the solution lots of .dependsOn scripting for each sub-project ?

Regards, Jeremy

Why do you want/need to have an alias for the ‘clean’ task?

You can achieve it this way:

task cleanAll {
    dependsOn subprojects.collect { subproject ->
        subproject.tasks.matching { it.name == "clean" }
    }
}

This makes sure that project evaluation order doesn’t matter and also handles projects for which there is no ‘clean’ task.

You can use this method: http://www.gradle.org/docs/current/javadoc/org/gradle/api/Project.html#getTasksByName(java.lang.Stri…

’’‘
task myClean {
  dependsOn getTasksByName(“clean”, true)
}
’’’

@Marcin, the reason for the alias is this is a migration project. Users that have a build relying on the alias can reasonably expect to use the same target/task names after the migration. Thank you for the suggestion.

@Luke, perfect and that’s what I am looking for. Thank you.

@Luke, I tried setting the dependsOn for my task. Using ‘clean.all’ as the primary task.

The build didn’t remove the build directies from my projects. This is the ‘clean.all’ output on the command line with the --info flag

… > Selected primary task ‘clean.all’ > Tasks to be executed: [task ‘:clean.all’] > :clean.all > > BUILD SUCCESSFUL

I was expecting all the clean tasks in all projects to be called. The call to getTasksByName does return many projects as expected. But setting them to the task using dependsOn doesn’t necessarily mean they get executed.

This is a sample of the output with the --info flag when ‘clean’ is called as the primary task.

… > All projects evaluated. > Selected primary task ‘clean’ > Tasks to be executed: [task ‘:clean’, task ‘:common-utils:clean’, task ‘:database:clean’, task ‘:faban:clean’, task ‘:loader:clean’, task ‘:orders:clean’, task ‘:properties:clean’, task ‘:acmej.ear:clean’, task ‘:acmej.jar:clean’, task ‘:acmej.war:clean’, task ‘:acmejdriver:clean’, task ‘:supplier:clean’, task ‘:ws-buyer:clean’, task ‘:ws-supplier:clean’]

Using Gradle 1.8