77

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?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
NO127
  • 1,103
  • 1
  • 8
  • 11
  • I don't think you can edit the `build` task in the gradle file. You need to create your own task. – Henry Nov 09 '17 at 02:14

6 Answers6

67

You can try e.g.:

unwantedTask.enabled = false
Opal
  • 81,889
  • 28
  • 189
  • 210
  • 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`. – Lukas Körfer Nov 09 '17 at 09:06
  • Could not set unknown property 'enabled' for extension 'jib' of type com.google.cloud.tools.jib.gradle.JibExtension. – mjaggard Feb 23 '22 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 '22 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. – RomanMitasov Feb 24 '23 at 10:59
33

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
    }
}
NO127
  • 1,103
  • 1
  • 8
  • 11
  • Great! Only *this* solution worked for tasks, which are not always present (such as `startScripts` or `distZip`). – java.is.for.desktop Jan 21 '18 at 15:03
  • 1
    For all modules wrap it in `subprojects { ... }` and put it in root project `build.gradle`. – Eugen Pechanec Sep 06 '18 at 16:12
  • 2
    Didn't work. The task still appears in the gradle list :( – htafoya Oct 22 '20 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: https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraph.html#whenReady-groovy.lang.Closure- – Opal Mar 15 '22 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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
20

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.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
  • 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. – Lukas Körfer Aug 13 '20 at 00:26
  • FYI, there is also a bug related to this approach on Gradle version 6.8+: https://github.com/gradle/gradle/issues/18196 – Jeremiah Zucker Nov 14 '22 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" }
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
2
project.gradle.taskGraph.whenReady { graph ->
  project.tasks.findAll().forEach { task ->
    if (task.name.contains("<your-text>")) {
      task.enabled = false
    }
  }
}
kolobok
  • 3,835
  • 3
  • 38
  • 54