Today, after some experimenting, I found a solution for a problem I had encountered before: while using the maven versions plugin to check for updates of my dependencies, how to share my version rules among separate maven projects?

As a good maven user I have centralized my dependency and plugin management to a parent pom so every child project follows the same setup and uses the same plugins. How could I extend this to the version rules used for the versions-maven-plugin so each child used the same rules, ideally defined somewhere in the parent pom?

It turns out the solution is pretty simple: as indicated on the plugin’s documentation, you can put your version rules in a Java artifact and configure your versions plugin to look for its rules in that artifact.

To do so, start by creating a folder for your new artifact, with a layout as follows:

version-rules
   |- files
         \-version-rules.xml
   \- pom.xml

The version-rules.xml contains your version rules. The pom.xml is a very basic setup that creates a jar with the contents of files:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.example</groupId>
   <artifactId>my-version-rules</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   
   <name>My version rules</name>

   <build>
      <defaultGoal>package</defaultGoal>
      <resources>
         <resource>
            <directory>files</directory>
            <filtering>false</filtering>
            <includes>
               <include>**/*</include>
            </includes>
         </resource>
      </resources>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Now run mvn install in this folder to create the jar and install it into your local maven repository.

Go back to the pom of the project you want to use these rules in. You configure the versions-maven-plugin to depend upon your version-rules artifact and look for the version-rules.xml on its classpath:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.example</groupId>
   <artifactId>my-project</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   
   <name>My version rules</name>
   <build>
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>versions-maven-plugin</artifactId>
               <version>2.7</version>
               <configuration>
                  <rulesUri>classpath:///version-rules.xml</rulesUri>
               </configuration>
               <dependencies>
                  <dependency>
                     <groupId>org.example</groupId>
                     <artifactId>my-version-rules</artifactId>
                     <version>1.0-SNAPSHOT</version>
                  </dependency>
               </dependencies>
            </plugin>
         </plugins>
      </pluginManagement>
   </build>
</project>

Et voilà, you can now run mvn versions:display-dependency-updates in my-project using the version-rules.xml from your my-version-rules artifact. Don’t forget to run mvn install again on your my-version-rules project every time you update your version rules or the newer rules won’t be taken into account.

For my personal projects the versions-maven-plugin gets managed in a parent pom shared by all my projects. I added the version-rules artifact as a module of the parent pom, which means it always gets build and released alongside the parent. Now all child projects can do dependency checks with the same version rules and they get updated rules as soon as they inherit from the newer parent pom.

2 Replies to “Easily share maven version rules”

Leave a Reply

Your email address will not be published. Required fields are marked *