【问题标题】:Gradle - Use project dependency with configurationGradle - 将项目依赖项与配置一起使用
【发布时间】:2018-12-18 08:11:39
【问题描述】:

我正在使用 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 源集中的类时,它将无法正常工作。有人知道为什么吗?

【问题讨论】:

    标签: gradle build.gradle gradle-kotlin-dsl


    【解决方案1】:

    万一有人绊倒了。我错过了在我的项目 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"))
    }
    

    然后就可以了..

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多