Access restrictions in Eclipse Multiple Projects with Gradle
Asked Answered
O

2

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!

Otherness answered 20/1, 2016 at 13:24 Comment(5)
Hi Boris, I am desperately looking for an answer to this question because I have the exact same problem.Amadou
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?"Windpipe
@StefanoL, see my question comment above.Windpipe
@Windpipe We tried both Buildship and Gradle (STS) Integration for Eclipse, but both didnt workedAmadou
@StefanoL posted an answer below.Sarcous
S
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 }
}
Sarcous answered 14/4, 2016 at 15:44 Comment(1)
BTW, I'm using gradle 2.8, Eclipse Luna and buildship 1.0.9Sarcous
O
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.

Otherness answered 13/4, 2016 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.