Gradle custom task which runs multiple tasks

gradle

I wanna run multiple gradle tasks as one. So instead of

./gradlew clean build publish

I want to have a custom task

./gradlew cleanBuildPublish

that executes clean build and publish in order.

How's that possible?

This does not work

task cleanBuildPublish {
    dependsOn 'clean'
    dependsOn 'build'
    dependsOn 'publish'
}

Best Answer

If you need to execute some tasks in predefined order, then you need to not only set dependsOn, but also to set mustRunAfter property for this tasks, like in the following code:

task cleanBuildPublish {
    dependsOn 'clean'
    dependsOn 'build'
    dependsOn 'publish'
    tasks.findByName('build').mustRunAfter 'clean'
    tasks.findByName('publish').mustRunAfter 'build'
}

dependsOn doesn't define an order of tasks execution, it just make one task dependent from another, while mustRunAfter does.