Friday, April 15, 2016

Access restrictions in Eclipse Multiple Projects with Gradle

Leave a Comment

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!

2 Answers

Answers 1

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

Answers 2

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment