代码之家  ›  专栏  ›  技术社区  ›  Joschi

Gradle-将项目依赖项与配置一起使用

  •  0
  • Joschi  · 技术社区  · 7 年前

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Joschi    7 年前

    以防有人在这件事上绊倒。我错过了发布项目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"))
    }
    

    那就行了。。