Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle custom task which runs multiple tasks

Tags:

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' } 
like image 914
Niklas Avatar asked Oct 02 '15 12:10

Niklas


People also ask

How do I run multiple tasks in Gradle?

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.

What does doLast mean in Gradle?

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.


2 Answers

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.

like image 183
Stanislav Avatar answered Sep 29 '22 19:09

Stanislav


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") } 
like image 20
tasomaniac Avatar answered Sep 29 '22 19:09

tasomaniac