Add the compile/runtime classpath of a source set as a dependency


(Martin Ahrer) #1

I have a source set that I need for exposing shared test code across project modules:

configurations {
        sharedTestCompile.extendsFrom compile, testCompile
        sharedTestRuntime.extendsFrom runtime, testRuntime, sharedTestCompile
    }
      sourceSets {
        // the shared test configuration covers code for supporting test, this should not include tests!
        sharedTest {
            java {
                srcDir 'src/sharedTest/java'
            }
            groovy {
                srcDir 'src/sharedTest/groovy'
            }
            resources {
                srcDir 'src/sharedTest/resources'
            }
            compileClasspath = sourceSets.main.output + configurations.sharedTestCompile
            runtimeClasspath = output + compileClasspath + configurations.sharedTestRuntime
        }
          test {
            compileClasspath = compileClasspath + sourceSets.sharedTest.output
            runtimeClasspath = runtimeClasspath + sourceSets.sharedTest.output
        }
    }
      task sharedTestJar(type: Jar) {
        classifier = "sharedtest"
        from sourceSets.sharedTest.output
    }
      assemble.dependsOn sharedTestJar
      idea {
        module {
            //and some extra test source dirs
            testSourceDirs += file('src/sharedTest/java')
            testSourceDirs += file('src/sharedTest/groovy')
            testSourceDirs += file('src/sharedTest/resources')
            // put additional dependencies on the classpath
            scopes.TEST.plus += configurations.sharedTestCompile
            scopes.TEST.plus += configurations.sharedTestRuntime
        }
    }

In order to express a project module dependency in my multi-module build I have to come up with a rather clumsy dependencies declaration

testCompile project(':contact-domain').sourceSets.sharedTest.output
    testCompile project(':contact-domain').sourceSets.sharedTest.compileClasspath

Have I done something wrong with my initial source set definition. Can I improve on the source set so the dependency can be expressed a bit simpler?


(Peter Niederwieser) #2
artifacts {
  sharedTestRuntime sharedTestJar
}
dependencies {
  testCompile project(path: ":other", configuration: "sharedTestRuntime")
}

(Martin Ahrer) #3

Thanks, updated the configuration name as with your example. Now works nicely!