Contents:


This page will take a stab on announcing breaking changes with latest upcoming Maven versions, and to discuss "best practices" for building Maven Plugins. This page assumes that reader know the general "best practices" for Java Projects (as Maven Plugins are basically Java projects).


Incoming Breaking Changes

Maven Project resources are limited we cannot cover "backward compatilibty" across two major versions. Hence, Maven 2 support is about to be removed. Below is the summary of breaking changes, that will happen in upcoming Maven 3.9.0 and 4.0.0:

Incoming Notable Changes

Maven 4.0.0 brings some new things in the play:

Minimal Set Of Best Practices

Maven Plugins are meant to be invoked by and run within Maven. Hence, one can draw a parallel between them and, for example, Java Servlets, where Servlet Container "provides" some dependencies to implementations. In this aspect: Maven is also a Container, container for Maven Plugins. Maven provides to plugins the "Maven API" classloader as parent, but to build a plugin, you still need to declare some depedencies.

A Maven Plugin aside of usually "Java project" things like Java source/target/release version needs to declare several extra things:

We can already see, that we have at least two repeating versions, so they are "potential" properties to lessen duplication:


Hence, a "minimal" Maven Plugin POM (showing elements related to Maven Plugins only) should have elements like this:


  <prerequisites>
    <maven>${mavenVersion}</maven>
  </prerequisites>
  
  <properties>
  <mavenVersion>3.6.3</mavenVersion>
    <mavenPluginToolsVersion>3.7.0</mavenPluginToolsVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>${mavenVersion}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>${mavenPluginToolsVersion}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>${mavenPluginToolsVersion}</version>
      </plugin>
    </plugins>
  </build>


Notes:

Then you can enlist your other (non-Maven) dependencies as well.


Testing Maven Plugins

Testing Maven Plugins is not trivial thing, given they are Maven Components, but not plain Eclipse Sisu or Codehaus Plexus components, but rather the plugin descriptor needs to be "translated" and installed into (any of two) container, plus the existing components in Maven Plugin JAR, if any. Hence, simplest is to use some existing frameworks for testing Maven Plugins.

This section is more like a collection of links/pointers, so below is a list of some documentation and frameworks:

Alternative frameworks supporting Junit5 or taking alternate approach: