Understand versions maven plugin basics with examples

versions-maven-plugin basics

The Versions Maven Plugin is a handy tool in the Maven project that helps manage and update application or project artifact versions.

Each artifact serves as an individual module with dependencies on other artifacts.

This is good for checking not only the latest versions of what your project directly depends on but also for parent dependencies.

For a developer, keeping track of and handling these versions can be a tedious job. This plugin makes it easier by automatically managing the versions of these building blocks in your project.

It also creates a report for your project, showing which dependencies are not using the latest version. This is really handy when you’re getting your project ready to release.

There are different goals available to report the following things.

  • Checking the plugin’s latest versions
  • Checking artifact dependencies versions update
  • Update the artifact and dependencies version with the new version
  • Update property versions to the latest.
  • Checking parent dependency with the latest versions
  • Checking snapshot version and update with release versions
  • Update the next snapshot and release versions.

Adding version plugin to pom.xml

In the pom.xml file of your artifact, include this plugin in the plugins section like this. You should provide the groupid, artifactId, and version of the plugin. This is how you use the version plugin.

 <build>
   <plugins>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>versions-maven-plugin</artifactId>
         <version>${maven.versions.plugin}</version>
      </plugin>
   </plugins>
</build>

Check the Dependency version of the maven project

With the version plugin, it verifies and prints the list of all Maven dependencies, showing both the current and latest versions of the project.

In this demonstration artifact, JUnit relies on version 3.8.1. Utilize the version plugin to inspect and show the dependency latest version in the pom.xml file.

<?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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.cloudhadoop</groupId>
   <artifactId>demo</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>app</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.versions.plugin>2.5</maven.versions.plugin>
   </properties>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.10</version>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>${maven.versions.plugin}</version>
         </plugin>
      </plugins>
   </build>
</project>

the mvn versions:display-dependency-updates is a maven command which runs in the project root directory

mvn versions:display-dependency-updates

The version plugin compares the current version with the latest version from the remote repository and displays them, highlighting the old and new versions.

[INFO] artifact junit:junit: checking for updates from central
[INFO] The following dependencies in Dependencies have newer versions:
[INFO]   junit:junit ............................................ 3.8.1 -> 4.12
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

display-plugin-updates goal

The display-plugin-updates goal examines the latest versions of the plugins and their dependencies, presenting the findings in a report on the console. Below is a sample output console log:

[INFO] Require Maven 3.0 to use the following plugin updates:
[INFO]   maven-clean-plugin ............................................ 3.1.0
[INFO]   maven-compiler-plugin ......................................... 3.8.0
[INFO]   maven-jar-plugin .............................................. 3.1.0
[INFO]   maven-resources-plugin ........................................ 3.1.0
[INFO]   org.codehaus.mojo:versions-maven-plugin ......................... 2.5

This log provides information about the current and latest versions of plugins, along with their dependencies, and indicates which versions are outdated.

versions:display-property-updates goal

The versions:display-property-updates goal assesses whether version properties require updating to the latest version. It generates a report indicating whether changes are needed for the version properties.

[INFO] --- versions-maven-plugin:2.5:display-property-updates (default-cli) @ demo ---
[INFO] Major version changes allowed
[INFO]
[INFO] The following version properties are referencing the newest available version:
[INFO]   ${maven.versions.plugin} ........................................ 2.5
[INFO] All version properties are referencing the newest version available.
[INFO]
[INFO] ------------------------------------------------------------------------

versions:set goal

versions:set goal is to update the artifact version to the new version. You need to enter via command line which will be updated in pom.xml with new version information

b:\maven-release-plugin>mvn versions:set -DnewVersion=1.0.1

INFO] Scanning for projects...
INFO]
INFO] ------------------------------------------------------------------------
INFO] Building app 1.0-SNAPSHOT
INFO] ------------------------------------------------------------------------
INFO]
INFO] --- versions-maven-plugin:2.5:set (default-cli) @ demo ---
nter the new version to set 1.0-SNAPSHOT: : 1.0.1
INFO] Searching for local aggregator root...
INFO] Local aggregation root: B:\maven-release-plugin
INFO] Processing change of com.cloudhadoop:demo:1.0-SNAPSHOT -> 1.0.1
INFO] Processing com.cloudhadoop:demo
INFO]     Updating project com.cloudhadoop:demo
INFO]         from version 1.0-SNAPSHOT to 1.0.1
INFO]
INFO] ------------------------------------------------------------------------
INFO] BUILD SUCCESS

In this example, current version 1.0-SNAPSHOT with the 1.0.1. This command updates version in the pom.xml to specificied version. You cna check pom.xml to verify changes.

versions:use-latest-versions goal versions:use-latest-versions goal checked the dependencies of an artifact with the central repository and replaces it with the latest versions.

mvn versions:use-latest-versions

This command updates the dependencies to latest versions. You can verify pom.xml, and check latest version is updated or not

Goals to modify pom.xml Here are some goals available to modify the pom.xml file of your project.

  • mvn versions:revert: this reverts the changes done to pom.xml from the git/svn repository
  • mvn versions:commit: commits the changes modified to pom.xml to a repository
  • mvn versions:update-parent:- This goal updates the parent pom to latest version.
  • mvn versions:display-plugin-updates: It shows a report of plugin updates along with their dependencies
  • mvn versions:display-property-updates: It shows a report of property updates versions.

Summarize

To summarize, Learned maven version plugin, useful to check the latest version of the project,update and commit changes to pom.xml.