Maven Prerequisites?

Starting with Maven 3.3.1 it is possible to define several important configuration elements within your project. For example JVM parameters or some kind of build extensions. Unfortunately using such things may result is wrong or failing builds cause those informations are not read by older Maven versions. How can you prevent such situations?

For a long time the prerequisites exists which defines the minimum Maven version to build with. I have to emphasize the build time.

One thing would be to define a prerequisites for your project which uses the new Maven 3.3.1 features like this in your pom.xml:

 1<project xmlns="http://maven.apache.org/POM/4.0.0"
 2  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 4                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5  ...
 6  <prerequisites>
 7    <maven>3.3.1</maven>
 8  </prerequisites>
 9  ...
10</project>

The problem with prerequisites is that it had been marked deprecated for a long time (starting with Maven 3.0 MNG-4840, MNG-5297, MNG-5501) and the intention of prerequisites was to prevent maven plugins running with the wrong Maven versions. I supose you are not writing a plugin but you would like to prevent people from trying to build your project with the wrong Maven version which might fail or produces weird results. So the real questions comes up. What to do?

The simple solution for this problem is the maven-enforcer-plugin. By using the following in your pom file:

 1<build>
 2  <plugins>
 3    ...
 4    <plugin>
 5      <groupId>org.apache.maven.plugins</groupId>
 6      <artifactId>maven-enforcer-plugin</artifactId>
 7      <version>1.4</version>
 8      <executions>
 9        <execution>
10          <id>enforce-maven</id>
11          <goals>
12            <goal>enforce</goal>
13          </goals>
14          <configuration>
15            <rules>
16              <requireMavenVersion>
17                <version>3.3.1</version>
18              </requireMavenVersion>
19            </rules>
20          </configuration>
21        </execution>
22      </executions>
23    </plugin>
24    ...
25  </plugins>
26</build>

you will prevent anyone from using an earlier version of Maven than 3.3.1. This solutions will also prevent Maven 2.2.1 from trying to start building such a project.

A best practice is to define such enforcer rule into the company parent pom to define the minimum Maven versions you would like to support with your builds.

The conclusion is to use the maven-enforcer-plugin rule only to define the minimum maven version to build with.