Gradle with Eclipse - incomplete .classpath when multiple sourcesets

雨燕双飞 提交于 2019-12-08 19:16:22

问题


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?


回答1:


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 =)




回答2:


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") }




回答3:


This is what worked for me with Gradle 2.2.1:

eclipse.classpath.plusConfigurations = [configurations.compile]


来源:https://stackoverflow.com/questions/17047956/gradle-with-eclipse-incomplete-classpath-when-multiple-sourcesets

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