Skip to content
Arthur Naseef edited this page Feb 17, 2016 · 2 revisions

Maven Enforcer Parent Version Rule

Rule for the Maven Enforcer Plugin that verifies the versions of module parents match the version of the module itself to ensure consistency across a multi-module project.

Motivation

While merging a highly active project against a local copy containing new artifacts, a challenging issue took several hours of diagnosis to track down. Running a java program out of Intellij, the program reported a ClassNotFoundException during spring context initialization. After spending hours tracking the problem down to missing artifacts in the classpath, and a POM that correctly called out the dependency, it turned out that Intellij didn't properly add the new artifact to the classpath.

The root cause was a version mismatch. The newly updated parent POM (and the rest of the upstream POMs) were updated to a newer version, while the locally added modules were still using an older version.

After searching for a way to automatically detect this mismatch, and failing to find one, this plugin was written.

Updating the POM

Example Rule Definition

Here is a snippet of the rule definition:

<parentVersionRule implementation="com.artnaseef.ParentVersionRule">
    <ignore>
        <value>my-project-parent</value>
    </ignore>
</parentVersionRule>

Example Dependency Specification for Enforcer Plugin

Here is a snippet of the dependency section needed within the enforcer plugin section of the POM:

<dependencies>
    <dependency>
        <groupId>com.artnaseef</groupId>
        <artifactId>maven-enforcer-parent-version-rule</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

Full Maven Enforcer Plugin Example Setup

Here is a complete example of the Maven Enforcer Plugin using the plugin.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>${enforcer.plugin.version}</version>
    <dependencies>
        <dependency>
            <groupId>com.artnaseef</groupId>
            <artifactId>maven-enforcer-parent-version-rule</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>enforce-java</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <requireJavaVersion>
                        <version>[1.8,1.9)</version>
                    </requireJavaVersion>

                    <parentVersionRule implementation="com.artnaseef.ParentVersionRule">
                        <ignore>
                            <value>my-project-parent</value>
                        </ignore>
                    </parentVersionRule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>