Can you convert Maven pom.xml files to reusable XML snippets?

In Maven, is it possible to reorganize frequently repeated fragments into a reusable β€œlibrary” (plugin)? I understand that I can write my own plugins, but often the functionality that I want to reuse is already expressed as fragments in pom.xml, and my natural tendency is that the reuse mechanism should save these fragments as XML.

Case at Point I used a procedure ( partially described here ) to create a WADL file from Jersey / JAX -RS, and then generate the developer documentation from that WADL and my own Javadoc source code. The procedure on this page describes the execution of two plugins, and I use the third plugin (org.codehaus.mojo: exec-xsltproc) and my own XSL file to turn WADL into HTML.

I have used this procedure in several Maven projects. The template comes in 100 lines of XML . What changes between projects is just the name of the source package ( com.example.myapp.rest in the associated template). Therefore, it is impossible to move this to the parent pom or any other mechanism that does not allow parameterization.

What I want is to aggregate, expose templates or otherwise reorganize these 100 lines (and one XST file) into a shared folder. I understand that restarted maven executions are done through Maven plugins. Ideally, I would not have to write Java (or Groovy) to re-express what I have already expressed in XML.

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

+8
source share
4 answers

You are right that it is not as simple as it should be. The "mixins" - which describe this particular opportunity - have been on the Maven roadmap for some time.

You can use the parent POM to share them, assuming all of these projects have a common ancestor. You can only configure another element, and it will be merged with the rest, or you can assign it a property value, which is defined where it is used. I understand that in general, this is not suitable for this use case, as the parent describes the structure, not the type of project.

Another alternative is to create an archetype for such a project. This allows you to define it once and generate it in new projects, but it is not used directly.

At the moment, the best solution is probably a custom plugin. I believe this inspired Don Brown to write the mojo-executor plugin, which now lives in: https://github.com/TimMoore/mojo-executor . But yes, then you need Java / Groovy :)

+4
source

As with most of the problems you will encounter when traveling through Maven, the answer will be Ant.

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

Since all the properties available for maven are also available in the target configuration, you can configure script assembly properties via maven.

Maven: 0, Ant: 1.

QED

+3
source

Perhaps you can create a project only for pom, add these 100 lines and inherit them in your projects via the <parent> tag.

+1
source

See the Maven Tiles plugin . It works great for me.

+1
source

All Articles