Search code examples
gradlebuild.gradlegradle-kotlin-dsl

Gradle - Use project dependency with configuration


I'm using Gradle 5.0 and Kotlin DSL. How can I include a configuration from another gradle subproject as a dependency to a subproject? I have the following setup:

root
  |--A
  |--B

Now in my B project I want to include project A with a certain configuration:

dependencies {
    testImplementation(project(":A", "testUtilsCompile"))
}

The sourcesets for all subprojects are defined like this:

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())

Everything seems to work ok as long it's within one subproject but when I try to use a class which is located within the testUtils sourceset from another subproject it won't work. Does anybody have an idea why?


Solution

  • In case anyone stumbles over this. I missed to publish an artifact from in my project 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())
            }
    

    after that I change this in my B project:

    dependencies {
        testImplementation(project(":A", "testUtilsCompile"))
    }
    

    Then it works..