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
}
}
}