Issue
Gradle 2.4 and Eclipse Mars here. Here's my project directory structure:
myapp/ <-- root project
myapp-client/
src/main/groovy/*
build.gradle <-- just declares some client-specific deps
myapp-shared/
src/main/groovy/*
build.gradle <-- shared-specific deps
myapp-server/
src/main/groovy/*
build.gradle <-- server-specific deps and how to package server JAR
build.gradle
settings.gradle
Where myapp/build.gradle
is:
allprojects {
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'codenarc'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
// ...etc.
}
dependencies {
// ...etc.
}
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}
}
And where myapp/settings.gradle
is:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
When I do a ./gradlew eclipse
I get messages indicating that the command was successful. I then open Eclipse to import myapp
as a series of individual subprojects.
Expectation based on what I'm used to seeing, historically
In previous projects that contained subprojects, I would go to Import >> Existing Projects into Workspace
, where I would see this dialog:
I would then click Browse
and select the project root (in this particular case, myapp
). Eclipse would then auto-detect the 3 subprojects and populate them as a checked list inside the Projects component in the UI above. I would then click "Finish" and Eclipse would load the 3 subprojects into my workspace, each showing as a separate project (as it should).
What's actually happening
Instead, when I click on myapp
as the root project, Eclipse just populates it as a single project, almost as if it doesn't detect that I have subprojects at all:
When I click Finish it imports a single project into my workspace, that has source folders for each of the 3 subprojects. And while that's just an annoyance, the real problem is that the classpath seems to be totally jacked up. For instance, I can add a class to myapp-shared
, and then include it in a class defined inside myapp-client
, and Eclipse doesn't force me to add an import
statement into the client class! It's like the whole project is sharing the same package/namespace.
Can any Gradle gurus spot a defect in my Gradle setup that could be the cause of this?
Solution
This was nasty, but I figured it out. Posting the solution here in case anybody else runs into this.
- Instead of a single
allprojects
closure, you needallprojects
andsubprojects
closures. Not sure what is "safe" to put in either closure, but I got this working by simply declaring myrepositories
inside ofallprojects
and then putting everything else insidesubprojects
. - The problem was that I had previously ran
gradle eclipse
on the root project (before I added in thesubprojects
closure), and so Gradle generated the typical artifacts (e.g..projects
,.classpath
,.settings/*
) in the parent/root project directory. So... - Delete Gradle-Eclipse artifacts out of the root directory. Delete the project from your Eclipse workspace (but not the disk!!!). I'd even go so far as to restart Eclipse to clear out any caches if they exist.
- Run
gradle clean
for good measure. Probably not needed. - Run
gradle eclipse
again. - Re-import into Eclipse, and all will be well in your kingdom once again, my friend.
Answered By - smeeb
Answer Checked By - Mary Flores (JavaFixing Volunteer)