Gradle makes it easy to add task dependencies at runtime.
Is it also possible to remove a task dependency after it has been added?
As a use case, let's say we want to run checkstyle in a separate pipeline step, not as part of the main build. So, we apply the checkstyle plugin, but that adds some tasks that depend on check. I would like to break that dependency, so that checkstyle only runs when explicitly run, not as part of build (which depends on check, which depends on checkstyle*).
To accomplish the above, I could run the main pipeline step as build -x checkstyleMain -x checkstyleTest.
Another workaround would be to set the enabled property of the checkstyle tasks to false.
But for this question, I am interested in a generic way to remove a task dependency so that the task graph is missing the edge between 2 tasks, like that dependency was never added. The problem is not an easy one because:
- removing a dependency before it was added won't have any effect
- trying to remove a dependency on
gradle.taskGraph.whenReadywill probably fail (since it's no longer allowed to change the task graph.
A good answer would also be that this is for sure not possible, in which case I could raise a feature request with the Gradle team.
Update 1
I tried to convert @Chriki's groovy to kotlin, like this:
project.afterEvaluate {
tasks.check {
dependsOn -= tasks.find {
it.name.startsWith("checkstyle")
}
}
}
But this doesn't work. I get an error: "Removing a task dependency from a task instance is not supported."