0

我想在 Gradle 文件中获取任务的依赖项列表,以便向onlyIf它们添加子句(这样可以关闭任务依赖项的分支——与在 Gradle 中跳过禁用任务的依赖项执行有关?) . 如何才能做到这一点?

例如:

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

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

gradlew -Ppublish.dryrun=true publish
4

1 回答 1

1

以下作品:

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
            }
        }
    }
}
于 2013-09-18T16:27:25.173 回答