Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you refactor Maven pom.xml files into reusable XML fragments?

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?

like image 382
Matt Quail Avatar asked May 09 '11 11:05

Matt Quail


People also ask

Can POM xml be renamed?

Maven looks for the pom. xml file, so you should rename it that for it to work. You could use another file name using the -f option. mvn -f parent-pom.

Can we have more than one POM xml?

two pom. xml files are present in the same directory. some of the dependencies in the pom files are for the same artifact, but different versions.

What can you do with POM xml?

The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc. Maven reads the pom. xml file, then executes the goal.

What is modelVersion in POM xml?

model version is the version of project descriptor your POM conforms to. It needs to be included and is set. The value 4.0. 0 just indicated that it is compatible Maven 3.


1 Answers

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 :)

like image 95
Brett Porter Avatar answered Sep 18 '22 15:09

Brett Porter