Tuesday, September 18

Simple Maven Plugin Inheritance

Pax Construct extends a number of other maven mojos from the archetype, compiler and eclipse plugins. Extending maven mojos is a bit of a pain at the moment, because plexus (the component manager used by maven) relies on javadoc comments to generate the plugin XML. Unfortunately when the parent mojo is provided by an external project, the javadoc isn't available to plexus.

At first I relied on duplicating parameters in the sub-mojo to trick plexus into creating the right plugin XML, then used reflection to forcibly update any private fields in the parent mojo.

Maintaining these parameters was a bit of a nightmare, so I developed a new plugin called the maven-inherit-plugin to help with the plugin XML. It attempts to merge the various plugin XML fragments and warns about any potential field clashes that would require reflection.

To use it in your plugin pom, add the following XML:




It looks for dependencies of type "maven-plugin" and will merge the XML fragments describing their goals with any matching local goals that have the following syntax:

@extendsPlugin {plugin}
@goal {local-goal}

If you want to rename a inherited goal, use this syntax:

@extendsPlugin {plugin}
@extendsGoal {inherited-goal}
@goal {local-goal}

For example, in Pax-Construct I extend the archetype goal into a number of sub-goals, such as:

* @extendsPlugin archetype
* @extendsGoal create
* @goal create-project
* @requiresProject false

Overridden fields generate a warning at compile time and must be updated using reflection. See the main pax plugin for examples of using this technique to add behaviour to existing plugins.

Source code is available from:


(updated to show new javadoc tags)

No comments: