java - Gradle:为选择的子项目配置分发任务

标签 java gradle build.gradle

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

相关文章:

java - org.openqa.selenium.ElementNotVisibleException : Element is not currently visible and so may not be interacted with Command duration or timeout:

java - 使用数组创建多个文本字段

gradle - 如何在外部文件中定义函数?

java - 复选框和共享首选项

java - 序列化对象会包含元数据吗?

android - gradle 库与应用程序模块集成的问题

Android gradle 失败一次,第二次成功

android - ClassNotFoundException : kotlin. KotlinNothingValueException 使用带有 build.gradle.kts 的 Android Studio 4.2 Canary 8

java - 在 Android Studio 中使用 Gradle 构建变体

gradle - Intellij Gradle 禁用离线模式