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?
You can try e.g.:
unwantedTask.enabled = false
-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
tasks.jib.enabled = false
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
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
}
}
startScripts
or distZip
).
Jan 21, 2018 at 15:03
subprojects { ... }
and put it in root project build.gradle
.
Sep 6, 2018 at 16:12
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.
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.
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
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" }
}
project.gradle.taskGraph.whenReady { graph ->
project.tasks.findAll().forEach { task ->
if (task.name.contains("<your-text>")) {
task.enabled = false
}
}
}
build
task in the gradle file. You need to create your own task.