Gradle custom task which runs multiple tasks

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.


You can also use the task base class called GradleBuild

Here how you can do that with GradleBuild

Groovy DSL:

task cleanBuildPublish(type: GradleBuild) {
     tasks = ['clean', 'build', 'publish']
}

Kotlin DSL:

tasks.register<GradleBuild>("cleanBuildPublish") {
    tasks = listOf("clean", "build", "publish")
}

My approach is

task cleanBuildPublish (type: GradleBuild, dependsOn: ['clean', 'build', 'publish']) { 
}

This works for me.