Question

I have a gradle build script with a handful of source sets that all have various dependencies defined (some common, some not), and I'm trying to use the Eclipse plugin to let Gradle generate .project and .classpath files for Eclipse, but I can't figure out how to get all the dependency entries into .classpath; for some reason, quite few of the external dependencies are actually added to .classpath, and as a result the Eclipse build fails with 1400 errors (building with gradle works fine).

I've defined my source sets like so:

sourceSets {
    setOne
    setTwo {
        compileClasspath += setOne.runtimeClasspath
    }
    test {
        compileClasspath += setOne.runtimeClasspath
        compileClasspath += setTwo.runtimeClasspath
    }
}

dependencies {
    setOne 'external:dependency:1.0'
    setTwo 'other:dependency:2.0'
}

Since I'm not using the main source-set, I thought this might have something to do with it, so I added

sourceSets.each { ss ->
    sourceSets.main {
        compileClasspath += ss.runtimeClasspath
    }
}

but that didn't help.

I haven't been able to figure out any common properties of the libraries that are included, or of those that are not, but I can't find anything that I'm sure of (although of course there has to be something). I have a feeling that all included libraries are dependencies of the test source-set, either directly or indirectly, but I haven't been able to verify that more than noting that all of test's dependencies are there.

How do I ensure that the dependencies of all source-sets are put in .classpath?

Was it helpful?

Solution

This was solved in a way that was closely related to a similar question I asked yesterday:

// Create a list of all the configuration names for my source sets
def ssConfigNames = sourceSets.findAll { ss -> ss.name != "main" }.collect { ss -> "${ss.name}Compile".toString() }
// Find configurations matching those of my source sets
configurations.findAll { conf -> "${conf.name}".toString() in ssConfigNames }.each { conf ->
    // Add matching configurations to Eclipse classpath
    eclipse.classpath {
        plusConfigurations += conf
    }
}

Update:

I also asked the same question in the Gradle forums, and got an even better solution:

eclipseClasspath.plusConfigurations = configurations.findAll { it.name.endsWith("Runtime") }

It is not as precise, in that it adds other stuff than just the things from my source sets, but it guarantees that it will work. And it's much easier on the eyes =)

OTHER TIPS

I agree with Tomas Lycken, it is better to use second option, but might need small correction:

eclipse.classpath.plusConfigurations = configurations.findAll { it.name.endsWith("Runtime") }

This is what worked for me with Gradle 2.2.1:

eclipse.classpath.plusConfigurations = [configurations.compile]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top