为什么Eclipse不能将第三方库源文件附加到WTP-faceted Gradle项目?

22

源文件被下载到Gradle缓存中,源文件的classpath条目看起来没问题,但是Eclipse不会显示第三方库的源文件。

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="output" path="bin/main"/>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/main/resources"/>
    <classpathentry output="bin/test" kind="src" path="src/test/java"/>
    <classpathentry output="bin/test" kind="src" path="src/test/resources"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container" exported="true"/>
    <classpathentry sourcepath="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/org.springframework/spring-webmvc/3.1.2.RELEASE/source/bc9c347358c5d59998b27e8a13c75106c3d0696/spring-webmvc-3.1.2.RELEASE-sources.jar" kind="lib" path="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/org.springframework/spring-webmvc/3.1.2.RELEASE/jar/e02f3e5aa91c8caa37b2e9b780229d27e76c0f2e/spring-webmvc-3.1.2.RELEASE.jar" exported="true">
        <attributes>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes>
    </classpathentry>
    <classpathentry sourcepath="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/org.springframework.data/spring-data-jpa/1.2.0.BUILD-SNAPSHOT/source/19e4dedbb956cdc3bb1d34d61a351229b653029a/spring-data-jpa-1.2.0.BUILD-SNAPSHOT-sources.jar" kind="lib" path="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/org.springframework.data/spring-data-jpa/1.2.0.BUILD-SNAPSHOT/jar/2a3cfc2ea0b2c8d87f32166e16bb56e1f092568b/spring-data-jpa-1.2.0.BUILD-SNAPSHOT.jar" exported="true">
        <attributes>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes>
    </classpathentry>
    <classpathentry sourcepath="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/org.springframework.security/spring-security-web/3.1.2.RELEASE/source/49e8d929d835d4ba4556ba9d138488ecc083a794/spring-security-web-3.1.2.RELEASE-sources.jar" kind="lib" path="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/org.springframework.security/spring-security-web/3.1.2.RELEASE/jar/e43e7683289f08c1e073564a94e6f26298ec4f59/spring-security-web-3.1.2.RELEASE.jar" exported="true">
        <attributes>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes>
    </classpathentry>
    <classpathentry sourcepath="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/javax.validation/validation-api/1.0.0.GA/source/7a561191db2203550fbfa40d534d4997624cd369/validation-api-1.0.0.GA-sources.jar" kind="lib" path="D:/Users/SSchneider/.gradle/caches/artifacts-14/filestore/javax.validation/validation-api/1.0.0.GA/jar/b6bd7f9d78f6fdaa3c37dae18a4bd298915f328e/validation-api-1.0.0.GA.jar" exported="true">
        <attributes>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes>
    </classpathentry>

</classpath>
1个回答

58

在Eclipse中,生成的.classpath文件有一种奇怪的行为。只需将“Web App Libraries”条目放在类路径文件的底部(-> Java构建路径->顺序和导出)即可解决问题。

最好的解决方案是改进您的gradle.build文件(这样每次通过gradle生成类路径文件时就不需要重新排序构建路径):

...
eclipse.classpath.file {
    // Classpath entry for Eclipse which changes the order of classpathentries; otherwise no sources for 3rd party jars are shown
    withXml { xml ->
        def node = xml.asNode()
        node.remove( node.find { it.@path == 'org.eclipse.jst.j2ee.internal.web.container' } )
        node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.jst.j2ee.internal.web.container', exported: 'true'])
    }
}

4
我们应该将这个问题注册成一个 bug,对吗? - jelle
不确定这是否应该是一个针对Gradle STS Eclipse插件团队或Gradle团队的错误报告,但它绝对应该是一个错误报告(解决方案上的25个赞表明了其严重性)。 - icyitscold
1
嗯,看起来Gradle捆绑的eclipse-wtp插件eclipseClasspath任务不再出现此问题。然而,对于启用了“依赖管理”的eclipse STS Gradle IDE插件用户仍然存在问题。我已经在这里向STS团队报告了此问题:https://issuetracker.springsource.com/browse/STS-3742 - icyitscold
这种情况仍在发生,应该将其作为 Gradle 的错误报告。 - Dave L.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接