【问题标题】:Gradle integration for eclipse keeps on changing .classpath fileEclipse 的 Gradle 集成不断更改 .classpath 文件
【发布时间】:2015-12-18 15:39:31
【问题描述】:

我当前的配置包括一个使用带有 java 和 eclipse-wtp 插件的 gradle 的 Java WAR 项目,以及最新版本的 Buildship for Eclipse 集成(今天早上是 1.0.3,现在是 1.0.4)以及 Eclipse Luna。

在遵循此解决方案https://stackoverflow.com/a/9820317/1544713(成功)以避免将测试类部署到我的本地服务器后,我注意到每次刷新项目或关闭并打开 Eclipse 时,.classpath 文件和.settings/org.eclipse.wst.common.component 都已更改到他们以前的状态,这给我带来了问题(将测试类部署到本地服务器意味着由于缺乏一些测试时间依赖性,当然还有不希望的行为,它们将无法加载)。

.classpath的内容由(右)改变:

<classpathentry kind="src" output="target/test-classes" path="src/test/java">
    <attributes>
        <attribute name="FROM_GRADLE_MODEL" value="true"/>
    </attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources">
    <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="src" path="src/test/resources">
    <attributes>
        <attribute name="FROM_GRADLE_MODEL" value="true"/>
    </attributes>
</classpathentry>

对于.settings/org.eclipse.wst.common.component,添加了这两行(同样,我不希望它们出现):

    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>

我不知道是 Gradle 正在更改文件还是 Buildship,甚至是 Eclipse。无论如何,我想有办法阻止这种情况发生。我尝试了很多替代方案,例如 build.gradle 中的以下配置:

eclipse.classpath.file.whenMerged { cp ->
    cp.entries.findAll { it.kind = "src" && it.path.startsWith("src/test/") }*.output = "target/test-classes"
}

eclipse {
    wtp.component {  
        file.withXml { xml ->       
            def node = xml.asNode()                     
            def wbrNodes = node.'**'.findAll { it.name() == 'wb-resource' && it.'@source-path'.startsWith("/src/test/")}
            if (wbrNodes.size() > 0) {
                wbrNodes.each { n -> n.parent().remove(n) }
            }                   
        }
    }
}

但是这种配置工作不规律(有时它似乎工作,有时它不工作,实际上以 eclipse.classpath.file.whenMerged 开头的第一段代码永远不会工作)。

提前致谢。

【问题讨论】:

  • 到目前为止,我还没有找到任何东西。似乎 Buildship 正在重写 .classpath.settings/* 文件以方便自己,我无法为 src/test 设置不同的输出目录。

标签: java eclipse gradle eclipse-wtp


【解决方案1】:

花了一些时间寻找可能的解决方案和问题的原因,我发现真正的问题实际上是 Buildship,它没有注意 build 中 eclipse-wtp 插件的指令.gradle,而是采用自己的方法生成 .classpath 和相关的 eclipse 配置文件。同时,到目前为止(今天刚刚发布的 Buildship 1.0.5),在构建自己的模型时(导入 Gradle 项目、打开 Eclipse 或刷新项目时,例如使用F5)。正如 Lance_Java 在 Gradle 论坛 (https://discuss.gradle.org/t/gradle-integration-for-eclipse-keeps-on-changing-classpath-file/11813/7?u=fbudassi) 中所说,将 eclipse-wtp 插件与 Buildship 一起使用是没有用的,因为两者都采用自己的方法来生成 Eclipse 配置文件。

因此,到目前为止,解决方案是从我的 Eclipse 安装中删除 Buildship,而是将其替换为直接来自 Eclipse 市场的 Gradle IDE Pack 3.6.x+0.17 插件,该插件使用构建.gradle 指令建立自己的模型,避免这种方式任何可能的冲突。

如果 Buildship 的任何人阅读了这篇文章,请给我们一些文件生成的钩子。同样,正如 Lance_Java 所建议的那样,这样的事情确实会有所帮助:

apply plugin: 'buildship' 

buildship.wtp.component.file.withXml { ... }

【讨论】:

    猜你喜欢
    • 2016-07-01
    • 2013-06-07
    • 2015-10-13
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2022-11-16
    相关资源
    最近更新 更多