Introduction

In this article, I explain about how to exclude a task when you run some task by using Gradle. You might need these configurations in a case that you want to exclude tasks generated by a plugin for each SourceSets.

$ java -version
openjdk version "11.0.6" 2020-01-14 LTS
OpenJDK Runtime Environment Corretto-11.0.6.10.1 (build 11.0.6+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.6.10.1 (build 11.0.6+10-LTS, mixed mode)

$ ./gradlew --version

------------------------------------------------------------
Gradle 6.2.2
------------------------------------------------------------

Build time:   2020-03-04 08:49:31 UTC
Revision:     7d0bf6dcb46c143bcc3b7a0fa40a8e5ca28e5856

Kotlin:       1.3.61
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.6 (Amazon.com Inc. 11.0.6+10-LTS)
OS:           Mac OS X 10.14.6 x86_64

Sample code

I create a Gradle project as a sample, and define tasks in build.gradle as below.

task checkMain {
    doLast {
        println "Main"
    }
}

def taskNames = ["First", "Second", "Third"];
taskNames.eachWithIndex { name, i ->
    task "check$name" {
        doLast {
            println "$name"
        }
    }
    if (i > 0) {
        tasks.getByName("check$name").dependsOn("check${taskNames[i-1]}")
    }
    if (i == 2) {
        checkMain.dependsOn("check$name")
    }
}

task checkStart {
    dependsOn "checkMain"
    doLast {
        println "Start"
    }
}

when I run checkStart, the tasks executed like this.

$ ./gradlew checkStart

> Task :checkFirst
First

> Task :checkSecond
Second

> Task :checkThird
Third

> Task :checkMain
Main

> Task :checkStart
Start

BUILD SUCCESSFUL in 2s
5 actionable tasks: 5 executed

Based on this sample, I configure to exclude the checkSecond task.

Exclude the task, including its dependency tasks

These 3 configurations exclude the task including its dependency tasks. When the checkStart task executed, checkSecond and checkFirst, which the dependency task of checkSecond, are excluded.

Note that when you write in settings.gradle or build.gradle, the configuration are enabled on all tasks in the project.

$ ./gradlew checkStart -x checkSecond

> Task :checkThird
Third

> Task :checkMain
Main

> Task :checkStart
Start

BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed

Set command line option

$ ./gradlew checkStart -x checkSecond

Write the configuration in the settings.gradle

startParameter.excludedTaskNames= ["checkSecond”]

Write the configuration in the build.gradle

project.gradle.startParameter.excludedTaskNames.add("checkSecond")

Exclude the task, without excluding its dependency task

These 2 configurations exclude only the specified task, so the dependency tasks of the excluded task are executed. These configurations are not exclude but disable to be exact.

In this example, the checkSecond task is skipped and the checkFirst task is executed.

$ ./gradlew checkStart

> Task :checkFirst
First

> Task :checkThird
Second

> Task :checkMain
Main

> Task :checkStart
Start

BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed

Disable when task added

tasks.whenTaskAdded { task ->
    if (task.name.startsWith("checkSecond")) {
        task.enabled = false
    }
}

Disable when task execution graph is ready

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(checkMain)) {
        tasks.findByName("checkSecond").enabled(false)
    }
}

That’s all.

References

Gradle Official Document: Build Script Basics

Gradle Official Document: Build Lifecycle

StackOverFlow: How can I disable a task in build.gradle