Eclipse Buildship(Gradle 插件)可以根据它的依赖关系更新我的 java 项目库路径配置吗?

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

我正在将 Eclipse 中的 Java(EJB 项目)和动态 Web 项目迁移到 Gradle。我正在使用 Buildship。我的项目有一个 Eclipse 库配置,维护起来很痛苦。有了成绩,这很棒,因为我只是将那些 "dependencies" 添加到 build.gradle 文件...等等。你知道的。没关系。

我的问题是我是否可以使用 Gradle 中的 Eclipse 插件来重新配置 eclipse 中项目的构建路径并删除旧的 Eclipse 库并让 Eclipse 中的项目基于相同的构建Gradle 下载的 jar 作为依赖项,因此如果我更改其中一个依赖项的版本,Eclipse 编译器也会根据新的 jar 依赖项编译代码。谢谢。

我正在编辑添加文件:

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

.类路径:

<?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'

}

我认为您不需要或不想使用 Gradle 中的 eclipseClasspath 任务。 Buildship 应该为您维护正确的项目配置。

问题是您的项目构建路径中仍然有手动添加的库。例如,Jackson、Commons Collections、Redis 等。应该删除这些,只留下 Gradle 管理的“项目和外部依赖项”库。您项目的构建路径库基本上应该如下所示:

看看它如何包含 项目和外部依赖项 build.gradle 文件中的一些库,例如 commons collections 和 slf4j?

以这种方式设置构建路径后,您可以让 Buildship update/manage 进行 Eclipse 项目设置,而不是要求 Gradle 进行设置:对 build.gradle 进行更改后, 右键单击​​项目并选择 Gradle > 刷新 Gradle 项目

如果 Bulidship 有一个选项可以在任何时候 build.gradle 发生变化时自动执行此操作,那就太好了,但我怀疑这样做的性能可能是个问题。