Disable particular task?

Hi,
I’m trying to disable certain tasks in build script. Based mostly on android - How can I disable a task in build.gradle - Stack Overflow I tried:

tasks.getByName("jsBrowserTest").enabled = false

but it fails with:

Task with name 'jsBrowserTest' not found in root project 'xyz'.

How Can I recursively disable it for the whole project?

(running with -x disables it corretly)

tasks.getByName is a very bad idea, as it breaks task-configuration avoidance.
Better would be tasks.named("...") { enabled = false } but it will have the same problem as it also only works if the task is already registered, but it seems that task is only executed later.
To correctly disable even if registered later, you would use tasks.named { it == "..."}.configureEach { enabled = false }.

But be aware that enabled = false is not the same as -x.
-x completely removes the task from the task list so also the tasks it depends on are not executed unless also depended upon by some other task.
enabled = false just disables the actions for that task, but the dependencies are still executed.

1 Like

For some reason it fails:

Is there a way to mimic -x with the configuration?

and/or make it configurable via gradle properties?

For some reason it fails:

some reason === your Gradle version is too old

In older version you can instead do .matching { it.name == "..." }.configureEach { ... }.

Is there a way to mimic -x with the configuration?

Add to gradle.startParameter.excludedTaskNames, that is where -x arguments land and as long as you do it during configuration phase it is effective as it is only evaluated by Gradle after configuration phase.

1 Like

OK, gradle upgraded :slight_smile:

Perfect, this is what I was looking for!

1 Like