23

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 }
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

2 Answers2

29

What's missing is:

dependencies {
    integrationTestCompile sourceSets.main.output
}

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

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 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. – Jeff Storey Aug 12 '13 at 19:12
  • It depends on what *exactly* you mean here. `sourceSets.test.output` doesn't include `sourceSets.main.output`. – Peter Niederwieser Aug 12 '13 at 19:17
  • My integrationTest code has dependencies on both the test code (src/test/groovy) and main code (src/main/code). – Jeff Storey Aug 12 '13 at 19:19
  • 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.output` – Jeff Storey Aug 12 '13 at 19:20
  • Yes, that's expected. – Peter Niederwieser Aug 12 '13 at 19:22
  • 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 http://stackoverflow.com/questions/32122363/gradle-sourceset-depends-on-another-sourceset that would be awesome. – Adrodoc Aug 20 '15 at 15:30
  • 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 made – abhishekmukherg Jun 03 '16 at 22:34
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".

Ellesmera
  • 1,119
  • 7
  • 8