Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access restrictions in Eclipse Multiple Projects with Gradle

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!

like image 989
Boris van Katwijk Avatar asked Jan 20 '16 13:01

Boris van Katwijk


1 Answers

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 }
}
like image 198
RaGe Avatar answered Sep 20 '22 02:09

RaGe