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

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


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.