gradleskip

Skip a task when running another task


I added a task to my gradle project:

task deploy() {
    dependsOn "build"
    // excludeTask "test"  <-- something like this

    doFirst {
       // ...
    }
}

Now the build task always runs before the deploy task. This is fine because the build task has many steps included. Now I want to explicitly disable one of these included tasks.

Usually I disable it from command line with

gradle deploy -x test

How can I exclude the test task programmatically?


Solution

  • You need to configure tasks graph rather than configure the deploy task itself. Here's the piece of code you need:

    gradle.taskGraph.whenReady { graph ->
        if (graph.hasTask(deploy)) {
            test.enabled = false
        }
    }
    

    WARNING: this will skip the actions defined by the test task, it will NOT skip tasks that test depends on. Thus this is not the same behavior as passing -x test on the command line