How to use selected classes from another subproject as a test dependency in Gradle

To add a simple dependency on sets of test sources from another subproject, I can do:

testCompile project(':subFoo1').sourceSets.test.output

This solution works, but in many cases it is not intended to add the entire source set as a dependency. For example, I would like to use only test data collectors, in which case files such as test-logback.xml (and regular tests) pollute my test path in the main module.

I tried the idea with a test JAR (which could filter the content, but it is problematic as a dependency) and some combination with eachFileRecurse, but without any luck.

My question is . How to add only a subset of the given source sets (for example, only classes with collectors matching the pattern **/*Builder.*) as a testCompile dependency in another subproject?

+3
source share
1 answer

You need something line by line:

upstream/build.gradle:

apply plugin: "java"

task testJar(type: Jar) {
    classifier = "tests"
    from sourceSets.test.output
    exclude "**/*Test.class"
}

artifacts {
    testRuntime testJar
}

downstream/build.gradle:

apply plugin: "java"

dependencies {
    testCompile project(path: ":upstream", configuration: "testRuntime")
}

Instead of using it, testRuntimeyou can also declare (for example configurations { testFixture }) and use a custom configuration that will give you more control over what external dependencies are passed to downstream projects. Another option would be to declare a separate set of sources for the part of the test code to be transmitted. (It will also give you separate compilation and runtime configurations to work with.)

PS: (, project(':subFoo1').sourceSets.test.output) , , .

+5

All Articles