NO127
NO127

Reputation: 1271

How can I disable a task in build.gradle

I want to skip some tasks when I run gradle build. I know that it can be done from command line with -x:

gradle build -x unwantedTask 

My question is how can the same result be achieved in the build.gradle?

Upvotes: 89

Views: 95061

Answers (6)

Mahozad
Mahozad

Reputation: 24722

Examples for Kotlin DSL (build.gradle.kts):

tasks.clean {
    isEnabled = false
}

Another way:

tasks.getByName("MyTaskName") {
    onlyIf { 2 * 2 == 4 }
    // Another example: check whether there is an environment variable called CI with value true
    // onlyIf { System.getenv()["CI"] == "true" }
}

Upvotes: 6

kolobok_ua
kolobok_ua

Reputation: 4220

project.gradle.taskGraph.whenReady { graph ->
  project.tasks.findAll().forEach { task ->
    if (task.name.contains("<your-text>")) {
      task.enabled = false
    }
  }
}

Upvotes: 4

Ondra Žižka
Ondra Žižka

Reputation: 46904

For a bit more generic approach, you can:

unwantedTask.onlyIf { <expression> }

For instance:

compileJava.onlyIf { false }

Advanced IDEs, like IDEA, through code completion, will give you a lots of what you can do on any given object in the build.gradle - it's just a Groovy script, after all.

Upvotes: 26

Vic Seedoubleyew
Vic Seedoubleyew

Reputation: 10576

As hinted to by @LukasKörfer in a comment, to really remove a task from the build, instead of just skipping it, one solution is to add this to your build script:

project.gradle.startParameter.excludedTaskNames.add('yourTaskName')

However this seems to remove the task for all subprojects.

Upvotes: 21

NO127
NO127

Reputation: 1271

Because I need to disable a bunch of tasks, so I use the following codes before apply plugin: in my build.gradle file:

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

Upvotes: 37

Opal
Opal

Reputation: 84854

You can try e.g.:

unwantedTask.enabled = false

Upvotes: 71

Related Questions