[英]How to conditionally apply source set excludes to build based on parameter provided
我正在使用Gradle 4.4(编辑:问题仍然存在于Gradle 4.8中)。 由于某些原因,我的项目具有以下布局
src/main/java/com/company/common
src/main/java/com/company/mod1
src/main/java/com/company/mod2
构建可以生成mod1或mod2,具体取决于执行的构建任务。 来自mod1的类绝不能使用来自mod2的类,反之亦然。 因此,如果其中一个使用另一个的类,我希望构建失败。 但是,我仍然希望能够在Eclipse中开发这两个源,这就是为什么我只希望构建在CI服务器上失败的原因。 CI服务器提供参数CI_BUILD。 生成文件使用以下机制来允许此操作:
排除不适用于此处:
ext {
ext_template_mod1 = [:]
ext_template_mod1.src_excludes = "**/mod2/**"
ext_template_mod2 = [:]
ext_template_mod2.src_excludes = "**/mod1/**"
if (project.hasProperty("mod2")) {
ext_template = ext_template_mod2
} else {
ext_template = ext_template_mod1
}
}
sourceSets {
main {
java {
if (project.hasProperty("CI_BUILD")) {
exclude "${project.ext_template.src_excludes}"
}
}
}
}
由于某种原因,这不起作用。 如果gradlew build -PCI_BUILD
上的源文件引用了mod2中的源文件,则gradlew build -PCI_BUILD
不会失败。
我不明白为什么不这样做。 如果我不检查项目属性,则排除将按预期工作:
工作配置:
ext {
ext_template_mod1 = [:]
ext_template_mod1.src_excludes = "**/mod2/**"
ext_template_mod2 = [:]
ext_template_mod2.src_excludes = "**/mod1/**"
if (project.hasProperty("mod2")) {
ext_template = ext_template_mod2
} else {
ext_template = ext_template_mod1
}
}
sourceSets {
main {
java {
exclude "${project.ext_template.src_excludes}"
}
}
}
现在,当mod1上的源文件引用了mod2中的源文件时, gradlew build -PCI_BUILD
按预期失败。
但是现在我的IDE不再将mod2文件夹中的源识别为源。
如何根据构建参数的存在将排除项应用于源集?
我一直在创建一个最小的示例,它在这里可以正常工作:
apply plugin: 'java'
ext {
ext_template_mod1 = [:]
ext_template_mod1.src_excludes = "**/mod2/**"
ext_template_mod2 = [:]
ext_template_mod2.src_excludes = "**/mod1/**"
if (project.hasProperty("mod2")) {
ext_template = ext_template_mod2
} else {
ext_template = ext_template_mod1
}
}
sourceSets {
main {
java {
if (project.hasProperty("CI_BUILD")) {
exclude "${project.ext_template.src_excludes}"
}
}
}
}
jar {
if (project.hasProperty("CI_BUILD")) {
exclude "${project.ext_template.src_excludes}"
}
}
上面的问题是我自己构建的工件。 我曾经使用过动态任务生成,却忘记了将要检查的参数作为startParameter.projectProperties
来提供。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.