我有一个多项目 gradle 构建。我只想为其中的 2 个子项目配置分发任务。
假设我有一个根项目和子项目 A、B 和 C。我只想为 B 和 C 配置分发任务。
以下方式有效:
root_project/build.gradle
subprojects{
configure ([project(':B'), project(":C")]) {
apply plugin: 'java-library-distribution'
distributions {
main {
contents {
from('src/main/') {
include 'bin/*'
include 'conf/**'
}
}
}
}
}
但我有兴趣让它以这种方式工作
subprojects{
configure (subprojects.findAll {it.hasProperty('zipDistribution') && it.zipDistribution}) ) {
apply plugin: 'java-library-distribution'
distributions {
main {
contents {
from('src/main/') {
include 'bin/*'
include 'conf/**'
}
}
}
}
}
在 B 和 C 的 build.gradle 中,我将具有以下内容:
ext.zipDistribution = true
在后一种方法中,我有以下两个问题:
问题一
* What went wrong:
Task 'distZip' not found in root project 'root_project'.
* Try:
Run gradle tasks to get a list of available tasks.
问题2
我尝试使用以下代码验证是否可以在 root_project 中读取属性 zipDistribution
subprojects {
.....
// configure ([project(':B'), project(":C")]) {
apply plugin: 'java-library-distribution'
distributions {
/* Print if the property exists */
println it.hasProperty('zipDistribution')
main {
contents {
from('src/main/') {
include 'bin/*'
include 'conf/**'
}
}
}
}
// }
.....
}
以上代码为 it.hasProperty('zipDistribution') 打印 null。
谁能告诉我正确的方法是什么,这样我就不会看到这些问题?
最佳答案
这是因为子项目是在根项目之后配置的。这就是 ext.zipDistribution
在那个时间点为 null
的原因(尚未设置)。
您需要使用 afterEvaluate
来避免这种情况:
subprojects {
afterEvaluate { project ->
if (project.hasProperty('zipDistribution') && project.zipDistribution) {
....
}
}
}
关于java - Gradle:为选择的子项目配置分发任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18839407/