2

我有一个多项目 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

在后一种方法中,我有以下两个问题:

问题 1

* What went wrong:
Task 'distZip' not found in root project 'root_project'.

* Try:
Run gradle tasks to get a list of available tasks.

问题 2

我尝试zipDistribution使用以下代码验证是否可以在root_project中读取该属性

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。

有人可以让我什么是正确的方法,这样我就看不到这些问题了吗?

4

1 回答 1

1

这是因为子项目是在根项目之后配置的。ext.zipDistribution这就是为什么null在那个时间点(尚未设置)。

您需要使用afterEvaluate来避免这种情况:

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('zipDistribution') && project.zipDistribution) {
            ....
        }
    }
}
于 2014-10-31T19:12:50.313 回答