本文介绍了如何为Gradle任务的依赖设置`onlyIf`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个Gradle文件中获得一个任务依赖关系列表,以便为它们添加一个 onlyIf 子句(这样任务依赖关系的分支可以是关闭 - 与)。如何做到这一点?



例如:

  def shouldPublish = {
def propertyName ='publish.dryrun'

!project.hasProperty(propertyName)|| project [propertyName]!='true'
}

子项目{
发布{
onlyIf shouldPublish
//以下内容不起作用;要点是发布的依赖关系也应该被关闭,
依赖项{
onlyIf shouldPublish
}
}
}

然后,在命令行上,可以:

  gradlew -Ppublish.dryrun = true发布


解决方案

以下内容工作:
$ b $ pre $ def recursivelyApplyToTaskDependencies(父任务,闭合闭包){
闭包(父)

parent.dependsOn.findAll {dependency - >
依赖项instanceof任务
} .each {任务 - >
recursivelyApplyToTaskDependencies(task,closure)
}
}

def shouldPrune = {task - >
def propertyName =$ {task.name} .prune

project.hasProperty(propertyName)&& project [propertyName] =='true'
}

/ *
*如果需要修剪任务。修剪意味着任务及其依赖关系不被执行。
*
*使用`-x`命令行选项会关闭修剪功能。
*
*用法:
* $ gradlew -Ppublish.prune = true发布#不会发布
* $ gradlew -Ppublish.prune = false发布#将发布
* $ gradlew -Dorg.gradle.project.publish.prune = true发布#不会发布
* $ gradlew -Dorg.gradle.project.publish.prune = false发布#将发布
* /
gradle.taskGraph.whenReady {taskGraph - >
taskGraph.getAllTask​​s()。each {task - >
def pruned = shouldPrune(task)

if(pruned){
recursivelyApplyToTaskDependencies(task){p - >
p.enabled = false
}
}
}
}


I would like within a Gradle file to get a list of dependencies of a task in order to add an onlyIf clause to them (such that branches of task dependencies can be turned off -- related to Skipping dependency execution of a disabled task in Gradle?). How can this be done?

For example:

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's dependencies should be turned off, too
        dependencies {
            onlyIf shouldPublish
        }
    }
}

Then, on the command line, one could:

gradlew -Ppublish.dryrun=true publish
解决方案

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

这篇关于如何为Gradle任务的依赖设置`onlyIf`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 02:05