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' }
Executing Multiple Tasks You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.
The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build. Both of these print the line, but the first one only prints the line when the testTask is supposed to be executed.
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") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With