Gradle项目的自定义条件配置 [英] Custom conditional configuration for Gradle project
问题描述
从 https://github.com/gradle/gradle/提取摘录blob / master / build.gradle :
ext {
isDevBuild = {
gradle .taskGraph.hasTask(developerBuild)
}
}
任务developerBuild {
description ='构建分发并运行预检核对'
group = 'build'
dependsOn testedDists
}
当我使用这种方法创建我的项目中的自定义配置我发现:
isDevBuild === true
即它总是如此,因为任务'developerBuild'在我的build.gradle项目中,因此在图中。他们有几个不同的配置(isCIBuild,isCommitBuild,isFinalReleaseBuild,...),所以我想我在这里出了问题。
有人可以解释如何制作这配置条件基于一些外部参数?
taskGraph.hasTask()
告诉任务是否在任务执行图中,即是否会执行。由于任务执行图仅在配置阶段后创建,因此必须从 whenReady
回调(或执行阶段)中调用此方法:
gradle.taskGraph.whenReady {graph - >
if(graph.hasTask(developerBuild)){
// do conditional conditional configuration
}
}
为了使它更具可读性,我们可以引入一个新方法:
def onlyFor(task,config){
始终成立,因为(非空)闭包是
gradle.taskGraph.whenReady {graph - >
if(graph.hasTask(task)){
project.configure(project,config)
}
}
}
$ c $现在我们可以写出:
onlyFor (developerBuild){...}
onlyFor(ciBuild){...}
解决此问题的另一个更简单的方法是检查
gradle.startParameter.taskNames
中是否包含特定的任务名称。然而,这有两个限制:首先,它会比较任务名称,这可以在多项目构建中发挥作用。其次,它只能找到直接指定的任务(例如在命令行中),而不是依赖关系。
PS:在你的代码中,<$ c根据Groovy的真实情况,$ c> isDevBuild
true
。 (与isDevBuild()
不同,isDevBuild
不会调用闭包。)Having an extract from https://github.com/gradle/gradle/blob/master/build.gradle:
ext { isDevBuild = { gradle.taskGraph.hasTask(developerBuild) } } task developerBuild { description = 'Builds distributions and runs pre-checkin checks' group = 'build' dependsOn testedDists }
When I used this approach to create custom configuration in my project I discovered that:
isDevBuild === true
i.e. it's always true because task 'developerBuild' is inside my build.gradle project, and hence in graph. They have a couple of "different" configs (isCIBuild, isCommitBuild, isFinalReleaseBuild, ...) so I suppose I got something wrong here.
Can someone explain how to make this configs conditional based on some external parameter?
解决方案
taskGraph.hasTask()
tells if a task is in the task execution graph, that is whether it will get executed. Because the task execution graph is only created after the configuration phase, this method has to be called from awhenReady
callback (or in the execution phase):gradle.taskGraph.whenReady { graph -> if (graph.hasTask(developerBuild)) { // do conditional configuration } }
To make this more readable, we can introduce a new method:
def onlyFor(task, config) { gradle.taskGraph.whenReady { graph -> if (graph.hasTask(task)) { project.configure(project, config) } } }
Now we can write:
onlyFor(developerBuild) { ... } onlyFor(ciBuild) { ... }
Another, simpler way to solve this problem is to check whether a particular task name is contained in
gradle.startParameter.taskNames
. However, this has two limitations: First, it compares task names, which can make a difference in multi-project builds. Second, it will only find tasks that have been specified directly (e.g. on the command line), but not dependencies thereof.PS.: In your code,
isDevBuild
always holds because a (non-null) closure istrue
according to Groovy truth. (In contrast toisDevBuild()
,isDevBuild
won't call the closure.)这篇关于Gradle项目的自定义条件配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!