Make one source set dependent on another
Asked Answered
E

2

24

I have an integration test source set in gradle, and it is dependent on my main classes being compiled. I set that up by doing

integrationTestClasses.dependsOn 'classes'

Is this the way to do it, or is there a way to setup dependencies on source sets so this happens automatically? In my configurations block I already have

integrationTestCompile { extendsFrom testCompile }
integrationTestRuntime { extendsFrom integrationTestCompile, testRuntime }
Enterprise answered 9/8, 2013 at 5:22 Comment(0)
P
29

What's missing is:

dependencies {
    integrationTestCompile sourceSets.main.output
}

With this in place, task dependencies should be established automatically.

Phytosociology answered 9/8, 2013 at 7:9 Comment(7)
That worked, thanks. If I wanted to be dependent on both main and test output, do I just need to declare a dependency on test since test is already dependent on main? That seems to be the behavior I'm seeing but wanted to verify.Enterprise
It depends on what exactly you mean here. sourceSets.test.output doesn't include sourceSets.main.output.Phytosociology
My integrationTest code has dependencies on both the test code (src/test/groovy) and main code (src/main/code).Enterprise
I think my previous comment on it appearing to work that way was wrong. Looks like I need to include sourceSets.main.output and sourceSets.test.outputEnterprise
Yes, that's expected.Phytosociology
I have the same Problem, but this sollution does not seem correct, if you are working with eclipse. If anyone could take a look at #32122863 that would be awesome.Caulis
This seems to cause severe problems if you try to then depend on integrationTestCompile configuration from a different project. For example, if in project A you have as you had described above, and then in project B you dependencies { compile project(path: ':A', configuration: 'integrationTest' } (after adding necessary artifacts and such to project A), gradle does not seem to know the transitive dependencies from the compile configuration in A (does not show up in dependencies or dependencyInsight). It adds them to the necessary classpaths but modifications cannot be madeEbarta
S
4

It is also possible to establish the dependency chain when defining the sourceSets. This worked to setup the "main" sourceSet to depend on a "generated" sourceSet:

// Default sourceSets already created by the java plugin: src/main and src/test
// Default content for each sourceSet: /java and /resources
sourceSets {
    // Adding src/generated
    generated
    // Setting src/main to depend on the dependencies and output of src/generated
    main {
        compileClasspath += generated.compileClasspath + generated.output
    }
}

The same principle should work to setup "integrationTest" to depend on "main".

Semiliquid answered 25/5, 2016 at 19:6 Comment(1)
Remember to also set the runtimeClasspath!Cogitable

© 2022 - 2024 — McMap. All rights reserved.