Monday, August 6, 2018

Understand versions maven plugin basics with examples

versions-maven-plugin basics

maven plugin version

Versions Maven Plugin is a plugin in maven project used to update the versions of application/artifact of a project. Each artifact is a single module which has a dependency on other artifacts. Always Latest versions are stable with fixing bugs. This has advantages to check the latest versions of dependencies as well as parent dependencies. Keeping and managing this version is a tedious task for a developer. This plugin automates this manging version of dependencies in an application. This generates a report of a project which contains all dependencies that are not using the latest version. This will be very helpful during release management of your project.

There are different goals available to report the following things
  • Checking plugin latest versions
  • Checking artifact dependencies versions update
  • Update artifact and dependencies version with the new version
  • Update property versions to latest.
  • Checking parent dependency with latest versions
  • checking snapshot version and update with release versions
  • Update next snapshot and release versions

Adding version plugin to pom.xml

 In artifact pom.xml, add this plugin in the plugins section as like below. you need to provide groupid,artifactId and version of this plugin. This is the way of using version plugin

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

Check Dependency version of maven project.

In this demo artifacts, JUnit has a dependency with version 3.8.1. Using this version plugin to check dependency version update pom.xml

<?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 below is a command which runs in project root directory
mvn versions:display-dependency-updates
This checks update from remote repository from central and displays them with 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 

This goal checks the latest versions of the plugins and their dependencies and displays the report to the console. Here is 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

versions:display-property-updates goal 

This goal gives the report of version properties that need to update to latest version or not
[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 

This 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
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

versions:use-latest-versions goal
this goal checked dependencies of a artifact with the central repositor and replaces with latest versions.
Goals to modify pom.xml
Here are some goals available to modify pom.xml file of your project
mvn versions:revert - this revert the changes done to pom.xml from git/svn repository
mvn versions:commit - this commits the changes modified to pom.xml to git
 repository
mvn versions:update-parent  - This goal updates the parent pom version with the new version.
That's all about version maven plugin. Please share/comment on facebook/twitter for any questions.


EmoticonEmoticon

Note: Only a member of this blog may post a comment.