Search code examples
eclipsegradlebuildship

Can Eclipse Buildship (Gradle plugin) update my java project library path configuration based on it's dependencies?


I am migrating my Java (EJB project) and Dynamic Web projects in Eclipse to Gradle. I am using Buildship. My projects have a Eclipse Library configuration, which it was a pain to maintain. With grade this is great as I just add those "dependencies" to build.gradle file... etc. You know. That's fine.

My question is if I can use the Eclipse plugin in Gradle to make it reconfigure the Build path for the project in eclipse and remove the old Eclipse libraries and have the project in Eclipse be build based on same jars downloaded by Gradle as dependencies so if I change a version of one of the dependencies, Eclipse compiler will also compile code based on that new jar dependency. thanks.

I editing to add files:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
        <name>ElizaTourismEJB</name>
        <comment></comment>
        <projects>
        </projects>
        <buildSpec>
                <buildCommand>
                        <name>org.eclipse.jdt.core.javabuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.eclipse.wst.common.project.facet.core.builder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.jboss.tools.jst.web.kb.kbbuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.jboss.tools.cdi.core.cdibuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.eclipse.wst.validation.validationbuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.hibernate.eclipse.console.hibernateBuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.eclipse.buildship.core.gradleprojectbuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
        </buildSpec>
        <natures>
                <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
                <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
                <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
                <nature>org.eclipse.jdt.core.javanature</nature>
                <nature>org.jboss.tools.jst.web.kb.kbnature</nature>
                <nature>org.jboss.tools.cdi.core.cdinature</nature>
                <nature>org.hibernate.eclipse.console.hibernateNature</nature>
                <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
        </natures>
</projectDescription>

.classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
        <classpathentry kind="src" path="src/main/java">
                <attributes>
                        <attribute name="FROM_GRADLE_MODEL" value="true"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="src" path="src/test/java">
                <attributes>
                        <attribute name="FROM_GRADLE_MODEL" value="true"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
        <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer">
                <attributes>
                        <attribute name="org.eclipse.jst.component.nondependency" value=""/>
                </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
        <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Jackson2"/>
        <classpathentry kind="lib" path="commons-collections4-4.1.jar"/>
        <classpathentry kind="lib" path="/ElizaTourismREST/WebContent/WEB-INF/lib/freemarker.jar"/>
        <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Redis"/>
        <classpathentry kind="con" path="org.eclipse.jst.server.core.container/com.ibm.ws.st.core.runtimeClasspathProvider/WebSphere Application Server Liberty Profile 16003">
                <attributes>
                        <attribute name="owner.project.facets" value="jst.ejb;jpt.jpa"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="lib" path="/Developer/java-lib/aspirin-0.11.01.jar"/>
        <classpathentry kind="src" path=".apt_generated">
                <attributes>
                        <attribute name="optional" value="true"/>
                </attributes>
        </classpathentry>
        <classpathentry kind="output" path="target/bin"/>
</classpath>

build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    jcenter()
    mavenCentral()
}

ext {
    swagger_annotations_version = "1.5.9"
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'

    testCompile 'junit:junit:4.12'

    compile group: 'javax', name: 'javaee-api', version:'7.0'
    compile "io.swagger:swagger-annotations:$swagger_annotations_version"
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.5.4'
    compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-jaxb-annotations', version: '2.5.4'
    compile group: 'biz.paluch.redis', name: 'lettuce', version: '4.3.0.Final'
    compile group: 'org.masukomi', name: 'aspirin', version: '0.11.01'
    compile group: 'org.freemarker', name: 'freemarker', version: '2.3.23'

}

Solution

  • I don't think you need, or want, to use the eclipseClasspath task in Gradle. Buildship should maintain the correct project configuration for you.

    The issue is that you still have manually-added libraries in your project Build Path. For example, Jackson, Commons Collections, Redis, etc. Those should be removed and only the Gradle-managed "Project and External Dependencies" library left. Your project's build path libraries should basically look like this:

    enter image description here

    See how it includes a few libraries from the build.gradle file under Project and External Dependencies, such as commons collections and slf4j?

    With the Build Path set up that way, you can let Buildship update/manage the Eclipse-project settings instead of asking Gradle to do it: after making a change to build.gradle, right-click on the project and choose Gradle > Refresh Gradle project

    It would be nice if Bulidship had an option to automatically do this any time build.gradle was changed, but I suspect the performance of doing so might be an issue.

    Update: recent versions of Buildship have an option to automatically keep the Eclipse project in sync whenever build.gradle changes. It can be enabled in the workspace Preferences > Gradle > Automatic Project Synchronization. It's also configurable at the project level in the Project Properties.