Excluding tasks from execution

In this post of Gradle, I will show how to exclude a particular task from execution.

Below is the gradle script with three tasks

build.gradle


task task1 {
    doFirst {
        println 'I am task1'
    }
}

task task2 {
    doFirst {
        println 'I am task2'
    }
}

task task3 {
    doFirst {
        println 'I am task3'
    }
}

In the above example I will exclude task3 from execution.

Open the command line and go to the folder containing the build.gradle.

Run the below code

gradle task1 task2 -x task3

We exclude task3 from execution using -x or –exclude-task

The gradle build will search for build.gradle file in the current folder and execute the mentioned tasks in the order they are listed in the argument except task3.

If you want to exclude multiple task say task2 and task3 from execution, type the below code in the command line

gradle task1 task2 -x task2 -x task3

Below is the output where only task3 is excluded from execution

Output

E:\Projects\GradleConcepts\package3>gradle task1 task2 -x task3

Task :task1
I am task1

Task :task2
I am task2

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

Leave a Reply