11

I have a Gradle Project in Eclipse consisting of multiple subprojects. I currently have subprojects A, B and C.

Project A should have access to Project B. Project B should have access to Project C. But project A should not have access to Project C.

A -> B, B -> C, but not A -> C

I can easily test this by having a java example class in Project A which tries to use a class from Project C.

I've achieved this with Gradle using the following setup in the main build.gradle file and using the transitive property:

project(':projectA') {
    dependencies {
        compile project(':projectB'), {
            transitive = false
        }
    }
}

project(':projectB') {
    dependencies {
        compile project(':projectC'), {
            transitive = false
        }
    }
}

Running Gradle's compileJava on Project A's example class gives the correct error message. I would like to have this error should up as a compile error in Eclipse. I was also able to manually configure the classpath in a way that the desired relationship holds, but a Gradle Refresh/Rebuild resets the classpath again.

Is it possible to use Gradle's Java Compiler instead of the Eclipse Compiler? Or should I influence the classpath files when doing a Gradle Refresh/Rebuild? Is there maybe a different solution?

I'd like to hear what is the preferred approach for this situation. Thanks!

Boris van Katwijk
  • 2,998
  • 4
  • 17
  • 32
  • Hi Boris, I am desperately looking for an answer to this question because I have the exact same problem. – Stefano L Apr 08 '16 at 13:47
  • Are you using any Gradle tooling in Eclipse? I mean, for example Buildship or (obsolete) Gradle IDE that came with STS? When you say *"a Gradle Refresh/Rebuild resets the classpath again"* what, exactly are you doing to "refresh/rebuild?" – E-Riz Apr 08 '16 at 21:23
  • @StefanoL, see my question comment above. – E-Riz Apr 12 '16 at 18:10
  • @E-Riz We tried both Buildship and Gradle (STS) Integration for Eclipse, but both didnt worked – Stefano L Apr 13 '16 at 14:27
  • @StefanoL posted an answer below. – RaGe Apr 14 '16 at 21:29

2 Answers2

3

You can use the gradle eclipse plugin to modify eclipse project settings from gradle, including changes to eclipse classpath. I would try something like this in build.gradle:

allprojects{
    apply plugin:'eclipse'

    eclipse.classpath.file {
        beforeMerged { classpath -> classpath.entries.removeAll{it} }
    }
}

and then run gradle eclipseClasspath to re-generate the .classpath file, and a general refresh / F5 in eclipse to pick up the modified .classpath file.


Demo:

I start with transitive=true in build.gradle. When a class in A instantiates a class in C, I do not get compile errors.

enter image description here

Now I change to transitive=false in build.gradle This causes compile failures from commandline but eclipse is happy with the old dependency information. Right-Click->Gradle->Refresh Project has no effect. To make gradle pick up the change, run gradle eclipseClasspath

gradle eclipseClasspath
 :eclipseClasspath
 :A:eclipseClasspath
 :B:eclipseClasspath
 :C:eclipseClasspath

and have Eclipse pick up the changed .classpath files by doing a refresh.

enter image description here

Which makes eclipse recognize the missing transitives and throw compile errors:

enter image description here

My full root build.gradle at this point:

allprojects{
    apply plugin:'java'
    apply plugin:'eclipse'

    eclipse.classpath.file {
        beforeMerged { classpath -> classpath.entries.removeAll {it} }
    }
}

project(':A').dependencies {
    compile project(':B'),{ transitive = false }
}

project(':B').dependencies {
    compile project(':C'),{ transitive = false }
}
RaGe
  • 22,696
  • 11
  • 72
  • 104
0

The only way I was able to achieve this was to refer to the class files of the project, thus:

project(':projectA') {
    dependencies {
        compile files("../projectB/build/classes/main")
    }
}

The path ../projectB/build/classes/main should point to where the class files are generated and stored in your project.

I also looked into replacing the Eclipse compiler with Gradle's compiler, but this is currently not supported by Eclipse.

Boris van Katwijk
  • 2,998
  • 4
  • 17
  • 32