Gradle with non-standard named source sets - how do I make them available to the test classes?

南楼画角 提交于 2020-01-16 01:11:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17028368/gradle-with-non-standard-named-source-sets-how-do-i-make-them-available-to-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!