79

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?

1
  • I don't think you can edit the build task in the gradle file. You need to create your own task.
    – Henry
    Nov 9, 2017 at 2:14

6 Answers 6

67

You can try e.g.:

unwantedTask.enabled = false
4
  • 20
    Please note, that this will skip the task, which is not the same as using the -x option, but it should result in the desired behaviour. The exclusion of a task is only possible in the settings.gradle via startParameter.excludedTaskNames. Nov 9, 2017 at 9:06
  • Could not set unknown property 'enabled' for extension 'jib' of type com.google.cloud.tools.jib.gradle.JibExtension.
    – mjaggard
    Feb 23, 2022 at 13:09
  • Yes. For the next person who sees this, some extensions create tasks with the same name as another object and presumably Gradle/Groovy uses the other object first. To reference the task, use tasks.jib.enabled = false
    – mjaggard
    Feb 24, 2022 at 13:04
  • For Gradle 4.9 or never one should use tasks.named('jib') { enabled = false } as this approach is lazy and does not trigger creation of jib task on before any Gradle task start. Feb 24 at 10:59
34

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
    }
}
4
  • Great! Only this solution worked for tasks, which are not always present (such as startScripts or distZip). Jan 21, 2018 at 15:03
  • 1
    For all modules wrap it in subprojects { ... } and put it in root project build.gradle. Sep 6, 2018 at 16:12
  • 2
    Didn't work. The task still appears in the gradle list :(
    – htafoya
    Oct 22, 2020 at 18:17
  • @EugenPechanec it seems you need to add this event after the task graph is populated with tasks. AFAIR you can try this way: docs.gradle.org/current/javadoc/org/gradle/api/execution/…
    – Opal
    Mar 15, 2022 at 13:52
24

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.

21

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.

2
  • 4
    Any name added to excludedTaskNames is interpreted in the same way as tasks passed to Gradle via the command line. Tasks names ('yourTaskName') represent all tasks with the the name in all (sub-)projects, whereas task paths (':yourTaskName' or ':yourProject:yourTaskName') represent a single task. Aug 13, 2020 at 0:26
  • FYI, there is also a bug related to this approach on Gradle version 6.8+: github.com/gradle/gradle/issues/18196 Nov 14, 2022 at 17:44
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.