Maven Extension Demo Study

Demo of Maven Extensions:

  • classical types of extensions,
  • how to write them,
  • how to configure them into your builds.

Demo Content

Code provided here demoes one specific type of extension: Maven 3 lifecycle extension, and the different results obtained when injecting this extension in the different ways available.

Maven lifecycle participation can be provided though 3 APIs:

  1. org.apache.maven.execution.AbstractExecutionListener,
  2. org.apache.maven.AbstractMavenLifecycleParticipant,
  3. org.apache.maven.eventspy.AbstractEventSpy.

This study implements each API (see javadoc) then tests implementations in ITs that configure the extension in different ways: verification done after IT execution check expected differences (see ITs results).

Classical Types of Extensions

A Maven extension is a library that goes into Maven Core classloader, then is really in the core execution of Maven, unlike a plugin that runs in a child classloader separated from other plugins.

There are multiple reasons one wants to put his code in Maven Core classloader:

Configuring Maven Extensions

There are multiple methods available to declare a library as an extension:

  1. classical POM's project.build.extensions.extension or project.build.plugins.plugin.extensions (since Maven 2),
  2. put jars in Maven ${maven.home}/lib (since Maven 2) or ${maven.home}/lib/ext (since Maven 3),
  3. use -Dmaven.ext.class.path=[path to files] (since Maven 3.0.2, see MNG-4936),
  4. configure in .mvn/extensions.xml (since Maven 3.3.1)

Declaring an extension in POM is the most classical and flexible way to do: the only drawback is that the extension is activated after POM reading, which can be too late for some very specific use cases like EventSpy.

Installing an extension directly inside Maven installation avoids the previous limitation, but is not flexible.

-Dmaven.ext.class.path=[path to files] is a little bit more flexible, but remains not configured into the build, which is not suitable to ensure an extension is available at build time.

.mvn/extensions.xml is the ultimate solution for these use cases. Its only drawback is that it has been added only in Maven 3.3.1.