Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Eclipse not working correctly for subprojects

Tags:

eclipse

gradle

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:

enter image description here

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:

enter image description here

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?

like image 424
smeeb Avatar asked Sep 22 '15 10:09

smeeb


People also ask

How do I import a nested gradle project into Eclipse?

Run Eclipse. Select the "File" > "Import" option from the menu: Select the "Gradle" > "Existing Gradle Project" option.

What is subproject in gradle?

In a multi-project gradle build, you have a rootProject and the subprojects. The combination of both is allprojects. The rootProject is where the build is starting from. A common pattern is a rootProject has no code and the subprojects are java projects.


1 Answers

This was nasty, but I figured it out. Posting the solution here in case anybody else runs into this.


  1. Instead of a single allprojects closure, you need allprojects and subprojects closures. Not sure what is "safe" to put in either closure, but I got this working by simply declaring my repositories inside of allprojects and then putting everything else inside subprojects.
  2. The problem was that I had previously ran gradle eclipse on the root project (before I added in the subprojects closure), and so Gradle generated the typical artifacts (e.g. .projects, .classpath, .settings/*) in the parent/root project directory. So...
  3. 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.
  4. Run gradle clean for good measure. Probably not needed.
  5. Run gradle eclipse again.
  6. Re-import into Eclipse, and all will be well in your kingdom once again, my friend.
like image 88
smeeb Avatar answered Oct 17 '22 21:10

smeeb