Removing tasks from taskGraph / Remove a task dependency

I came up with some code to block task execution if a build condition is met. I just was wondering if there was a more efficient way to implement this type of functionality?

In my specific use case, my build creates two sets of artifacts, my legacy output and the new gradle output. I wanted to prevent gradle from running legacy tasks if it was not explicitly run from the command line. In other words if the ‘assemble’ task is running, do not run any ‘legacyBuild’ tasks.

gradle.taskGraph.whenReady { taskGraph ->
    def tasks = taskGraph.getAllTasks()
    if (tasks.find {it.name == 'assemble'}) {
        tasks.findAll {it.name == 'legacyBuild'}.each { task ->
            task.enabled = false
        }
    }
}
1 Like

I’m looking into something similar as well.

In my war block I want to check if some particular task is on taskGraph and if so do some special configuration in the war block.

What I’m thinking of now is to implement the taskGraph.whenReady event and set a ext variable to store if the particular task was on the taskGraph.

Probably this will work but it feels not natural… So I’m also wondering what the most efficient ‘Gradle’ way of doing this would be.

The natural solution is to do the conditional configuration right inside the ‘taskGraph.whenReady’ callback. You can no longer change the task graph at this point, but you can still configure tasks (which includes setting ‘task.enabled = false’).