Gradle - 通过配置使用项目依赖项

标签 gradle build.gradle gradle-kotlin-dsl

我正在使用 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/

相关文章:

android-studio - 为任务 ':app:mergeDebugResources' 构建 Gradle 失败

Android Material 和 appcompat Manifest 合并失败

android - Android Gradle Kotlin Dsl 中 Unresolved reference 问题

gradle - 如何为 buildSrc 和应用程序模块定义 Kotlin 版本?

gradle - Gradle Kotlin DSL中的closureOf和delegateClosureOf有什么区别

android - 使用变量时的 Gradle 库版本建议

Android gradle库项目: package extra modules and jars

Android Studio 0.4.0 + ABSherlock + gradle 没有 "import module"

groovy - 切换到 Groovy 2.0 时 Gradle 中的 ClassNotFoundException

java - 如何告诉 Gradle 使用特定的 JDK 版本?