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) in开发者_StackOverflow社区to 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?
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.
You could probably create a pom-only project, add those 100 Lines there, and inherit them in your projects via the <parent> tag ..
See Maven Tiles Plugin. It works fine for me.
精彩评论