您可以将 Maven pom.xml 文件重构为可重用的 XML 片段吗?

发布于 2024-11-06 02:20:26 字数 844 浏览 4 评论 0 原文

在Maven中,是否可以将经常重复的片段重构为可重用的“库”(插件)?我意识到我可以编写自己的插件,但通常我想要重用的功能已经在 pom.xml 中表示为片段,我的自然倾向是重用机制应该保留这些片段 作为XML。

举个例子我一直在使用一个过程(此处部分描述) 用于从 Jersey/JAX-RS 源代码生成 WADL 文件,然后从该 WADL 和源代码自己的 Javadoc 生成开发人员文档。该页面上的过程描述了两个插件的执行,我使用第三个插件 (org.codehaus.mojo:exec-xsltproc) 和我自己的 XSL 文件将 WADL 转换为 HTML。

我已经在几个 Maven 项目中使用了这个过程。样板文件包含 100 行 XML。项目之间的变化只是相关源代码的包名称(链接样板中的com.example.myapp.rest)。因此,不可能将其移至父 pom 或任何其他不允许参数化的机制中。

我想要是将这 100 行(和一个 XST 文件)聚合、模板输出或以其他方式重构到一个公共位置。我意识到可重用的 Maven 执行是通过 Maven 插件交付的。理想情况下,我不必编写任何 Java(或 Groovy)来重新表达我已经用 XML 表达的内容。

是否可以将 Maven pom.xml 文件重构为 XML?

In Maven, is it possible to refactor commonly repeated fragments into a reusable "library" (plugin)? I realize that I can write my own plugins, but often the functionality I want to re-use is already expressed as fragments in a pom.xml, and my natural inclination is that the mechanism of re-use should preserve those fragments as XML.

Case in point I've been using a procedure (partly described here) for generating a WADL file from Jersey/JAX-RS source code, and then generating developer documentation from that WADL and the source-code's own Javadoc. The procedure on that page describes the execution of two plugins, and I am using a third plugin (org.codehaus.mojo:exec-xsltproc) and my own XSL file to turn the WADL into HTML.

I've used this procedure in several Maven projects. The boilerplate comes in at 100 lines of XML. What changes between projects is simply the package name of the source code in question (com.example.myapp.rest in the linked boilerplate). Therefore it is not possible to move this into a parent pom, or any other mechanism that does not allow parametrization.

What I want is to aggregate, template-out, or otherwise refactor those 100 lines (and one XST file) into a common location. I realize that re-usable maven executions are delivered via Maven plugins. Ideally I wouldn't have to write any Java (or Groovy) just to re-express what I have already expressed in XML.

Is it possible to refactor Maven pom.xml files as XML?

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

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

发布评论

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

评论(4

似狗非友 2024-11-13 02:20:26

你是对的,这并不像应该的那么容易。 “mixins”——准确地描述了这种功能——已经出现在 Maven 路线图上一段时间了。

您可以使用父 POM 来共享它们,假设所有这些项目都有一个共同的祖先。您可以仅配置不同的元素,它将与其余元素合并,或者您可以为它分配一个在使用它的地方定义的属性值。我知道,一般来说,这不适合此用例,因为父级描述的是结构而不是项目类型。

另一种选择是为此类项目创建原型。这允许您定义一次并生成到新项目中,但它不能直接重用。

目前最好的解决方案可能是自定义插件 - 我相信这是 Don Brown 编写 mojo-executor 插件的灵感,该插件现在位于: https://github.com/TimMoore/mojo-executor。但是,是的,您需要 Java/Groovy :)

You're correct that this isn't as easy as it should be. "mixins" - which describe exactly this capability - have been on the Maven roadmap for some time.

You can use a parent POM to share them, assuming all of those projects share a common ancestor. You can configure just the different element and it will be merged with the rest, or alternatively you can assign it a property value that is defined where it is used. I understand that in general this isn't appropriate for this use case since the parent describes the structure rather than the type of project.

Another alternative is to create an archetype for such a project. This allows you to define it once and generate into new projects, but it isn't something reused directly.

For now the best solution is probably a custom plugin - I believe this was the inspiration for Don Brown to write the mojo-executor plugin, that now lives at: https://github.com/TimMoore/mojo-executor. But yes, you then need Java/Groovy :)

沩ん囻菔务 2024-11-13 02:20:26

与您在 Maven 之旅中遇到的大多数问题一样,答案是 Ant。

创建一个完全符合您要求的 Ant 构建脚本,然后通过 maven-antrun 调用它-插件

由于 maven 可用的所有属性在目标配置中也可用,因此您可以通过 maven 属性配置构建脚本。

行长:0,
蚂蚁:

1.QED。

As with most problems you'll face on your Maven journey, the answer is Ant.

Create an Ant build script that does exactly what you want, then call this via the maven-antrun-plugin.

Because all of the properties available to maven are also available in the target configuration, you can configure the build script via maven properties.

Maven: 0,
Ant: 1.

QED.

染年凉城似染瑾 2024-11-13 02:20:26

您可能可以创建一个仅 pom 的项目,在其中添加这 100 行,然后通过 在您的项目中继承它们。标签 ..

You could probably create a pom-only project, add those 100 Lines there, and inherit them in your projects via the <parent> tag ..

明媚殇 2024-11-13 02:20:26

请参阅 Maven Tiles 插件。它对我来说效果很好。

See Maven Tiles Plugin. It works fine for me.

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