Question

I have a (legacy) Java project which I'm re-configuring to build with Gradle. The build should, eventually, output a number of jar archives, so I've divided to split up the source into source sets that correspond to the subdivision of the output into archives.

The file tree looks something like this:

- project root
    - src
        - setOne
             - java
                  - ...
             - resources
                  - ...
        - setTwo
             - java
                  - ...
        - setThree
             - java
                  - ...
             - resources
                  - ...
        - test
             - java
                  - ...

    - build.gradle

and in build.gradle I have the following:

apply plugin: 'java'

sourceSets {
    setOne
    setTwo {
        compileClasspath += setOne.output
    }
    setThree

    test
}

dependencies {
    setOne group: 'foo', name: 'bar', version: '1.0'
}

// Setup dependencies so that compileJava compiles all source sets
// while avoiding cyclic dependencies for main and test
sourceSets.matching { ss -> 
    !(ss.getName().contains('test') || ss.getName().contains('main')) 
}.each { ss ->
    compileJava.dependsOn ss.getCompileJavaTaskName()
}

When I try to build this, I get a bunch of warnings from the compileSetTwoJava target about some classes not being resolved - referring to classes from the foobar library imported to setOne. I assume this is because Gradle only gives setTwo access to the classes built in setOne - but why isn't it able to resolve the dependencies for the classes imported from setOne in the process?

How do I set up this correctly?

Was it helpful?

Solution

It turns out this was just as easy as I thought it ought to be - if you just start out the right way. Definining

setTwo {
    classPath += setOne.runtimeClasspath // instead of setOne.output
}

solves the problem.

UPDATE:

It seems even better to do it among the dependencies, where one can do

dependencies {
    setTwo sourceSets.setOne.output
}

and get it working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top