我正在使用 Gradle 5.0 和 Kotlin DSL。如何包含另一个 gradle 子项目的配置作为子项目的依赖项? 我有以下设置:
root
|--A
|--B
现在,在我的 B 项目中,我想包含具有特定配置的项目 A:
dependencies {
testImplementation(project(":A", "testUtilsCompile"))
}
所有子项目的源集定义如下:
project.the<SourceSetContainer>().register("testUtils", {
java.srcDir("src/test-utils/java")
resources.srcDir("src/test-utils/resources")
compileClasspath += project.the<SourceSetContainer>().named("main").get().output
runtimeClasspath += project.the<SourceSetContainer>().named("main").get().output
})
project.the<SourceSetContainer>().named("test").configure({
compileClasspath += project.the<SourceSetContainer>().named("testUtils").get().output
runtimeClasspath += project.the<SourceSetContainer>().named("testUtils").get().output
})
project.configurations.named("testUtilsCompile").get().extendsFrom(project.configurations.named("testCompile").get())
project.configurations.named("testUtilsRuntime").get().extendsFrom(project.configurations.named("testRuntime").get())
只要在一个子项目中,一切似乎都可以正常工作,但是当我尝试使用位于另一个子项目的 testUtils 源集中的类时,它将无法工作。有人知道为什么吗?
最佳答案
万一有人被这个绊倒了。我错过了发布项目 A 中的工件:
project.tasks.register("jarTestUtils", Jar::class) {
classifier = "testUtils"
from(project.the<SourceSetContainer>().named("testUtils").get().output)
}
project.artifacts {
add("testUtilsCompile", project.tasks.named("jarTestUtils").get())
}
之后我在我的 B 项目中更改了它:
dependencies {
testImplementation(project(":A", "testUtilsCompile"))
}
然后就可以了..
关于Gradle - 通过配置使用项目依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53828746/