将模块导出到Eclipse .ClassPath文件

发布于 2025-01-18 12:20:22 字数 1522 浏览 3 评论 0 原文

我有一个较旧的项目,该项目需要在Eclipse的 .classPath 文件中具有模块导出,以便它可以从该模块中解析一些类。如果我通过Eclipse的Build Path Editor生成类似的classPath条目:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/">
    <attributes>
        <attribute name="module" value="true"/>
        <attribute name="add-exports" value="java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED"/>
    </attributes>
</classpathentry>

自然,我想拥有Gradle生成的条目,我终于设法做到了:

eclipse.classpath.file {
    whenMerged {    // remove any JRE containers
        entries.findAll{ it.path ==~ '.*JRE_CONTAINER.*' }.each { entries.remove(it) }
    }
    withXml {       // add one with the required export
        def node = it.asNode()
        def cpe = new Node(node, 'classpathentry', [kind: 'con', path: 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/'])
        def attrs = new Node(cpe, 'attributes')
        new Node(attrs, 'attribute', [name: 'module', value: 'true'])
        new Node(attrs, 'attribute', [name: 'add-exports', value: 'java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED'])
    }
}

但是这似乎是粗俗而冗长的。是否有一种更简单的方法可以做这样的事情?

更新:另外,这仅在运行 gradle eclipse 时起作用,但在使用BuildShip进行“刷新Gradle Project”时不行 - AS whermerged 中创建一个容器,然后添加属性,而我无法做到。

I have an older project which needs to have a module export in Eclipse's .classpath file, so that it can resolve some classes from this module. The classpath entry looks like this if I generate it via Eclipse's build path editor:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/">
    <attributes>
        <attribute name="module" value="true"/>
        <attribute name="add-exports" value="java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED"/>
    </attributes>
</classpathentry>

Naturally, I'd like to have that entry generated by Gradle and I have finally managed to do that:

eclipse.classpath.file {
    whenMerged {    // remove any JRE containers
        entries.findAll{ it.path ==~ '.*JRE_CONTAINER.*' }.each { entries.remove(it) }
    }
    withXml {       // add one with the required export
        def node = it.asNode()
        def cpe = new Node(node, 'classpathentry', [kind: 'con', path: 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/'])
        def attrs = new Node(cpe, 'attributes')
        new Node(attrs, 'attribute', [name: 'module', value: 'true'])
        new Node(attrs, 'attribute', [name: 'add-exports', value: 'java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED'])
    }
}

But this seems crude and overly verbose. Is there a simpler way to do something like this?

Update: Also, this only works when running gradle eclipse, but not when doing a "Refresh Gradle Project" with Buildship - as that doesn't consider withXml. So I'd need to create a Container in whenMerged and add attributes to it, which I haven't been able to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2025-01-25 12:20:22

我找到了解决方案,属性节点可以通过 entryAttributes AbstractClassEntry 类的字段

这样,我就可以做......

eclipse.classpath.file {
    whenMerged {
        entries.find{ it.path ==~ '.*JRE_CONTAINER.*' }.each { 
            it.entryAttributes['module'] = true
            it.entryAttributes['add-exports'] = 'java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED'
        }
    }
}

并且它也将被Buildship应用。

I've found the solution, the attribute(s) nodes are accessible via the entryAttributes field of the AbstractClassEntry class.

This way, I can just do...

eclipse.classpath.file {
    whenMerged {
        entries.find{ it.path ==~ '.*JRE_CONTAINER.*' }.each { 
            it.entryAttributes['module'] = true
            it.entryAttributes['add-exports'] = 'java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED'
        }
    }
}

...and it will be applied by Buildship, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文