How to set `onlyIf` for Gradle task dependencies?

I would like to get a list of task dependencies in the Gradle file, to add the onlyIf clause to them (so that you can disable the task dependency branches - related to Skipping the execution of dependencies of a disabled task in Gradle? ). How can I do that?

For instance:

 def shouldPublish = { def propertyName = 'publish.dryrun' !project.hasProperty(propertyName) || project[propertyName] != 'true' } subprojects { publish { onlyIf shouldPublish // the following doesn't work;the gist is that publish dependencies should be turned off, too dependencies { onlyIf shouldPublish } } } 

Then on the command line you can:

 gradlew -Ppublish.dryrun=true publish 
0
source share
1 answer

The following works:

 def recursivelyApplyToTaskDependencies(Task parent, Closure closure) { closure(parent) parent.dependsOn.findAll { dependency -> dependency instanceof Task }.each { task -> recursivelyApplyToTaskDependencies(task, closure) } } def shouldPrune = { task -> def propertyName = "${task.name}.prune" project.hasProperty(propertyName) && project[propertyName] == 'true' } /* * Prune tasks if requested. Pruning means that the task and its dependencies aren't executed. * * Use of the `-x` command line option turns off the pruning capability. * * Usage: * $ gradlew -Ppublish.prune=true publish # won't publish * $ gradlew -Ppublish.prune=false publish # will publish * $ gradlew -Dorg.gradle.project.publish.prune=true publish # won't publish * $ gradlew -Dorg.gradle.project.publish.prune=false publish # will publish */ gradle.taskGraph.whenReady { taskGraph -> taskGraph.getAllTasks().each { task -> def pruned = shouldPrune(task) if (pruned) { recursivelyApplyToTaskDependencies(task) { p -> p.enabled = false } } } } 
0
source

Source: https://habr.com/ru/post/943621/


All Articles