DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Managing Version Numbers in Maven With the Maven Versions Plugin

Managing Version Numbers in Maven With the Maven Versions Plugin

John Ferguson Smart user avatar by
John Ferguson Smart
·
Aug. 19, 10 · Interview
Like (0)
Save
Tweet
Share
22.00K Views

Join the DZone community and get the full member experience.

Join For Free

If you have a Maven project of any size, particularly involving many modules or large numbers of dependencies, you have probably come across issues when updating your version numbers. Of course the Maven Release Plugin does a great job for updating version numbers as part of the automated release process, but there are times when it doesn't quite fit the bill, and version numbers are not limited to the main project versions.

The Versions Plugin very useful but not-so-well-known Maven plugin that gives you a number of tools in this direction. You should check out the website for a full list of everything this plugin can do, but here, I just want to cover a few highlights.

The Versions plugin, as the name suggests, helps you manage versions in your Maven projects. Versions of your artifacts, of course, but also versions of your dependencies and of your plugins. Let's take it for a spin.

The first thing you might want to do is to get an idea of the lay of the land, and see what dependencies in your project need updating. In large projects, the dependencies you use often become out-of-date over time, so it's nice to know when new ones are available. You can do this using the versions:display-dependency-updates command, which will list the dependencies you are currently using, and which ones are due for an update:

 

 

 

 

$ mvn versions:display-dependency-updates
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'versions'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Game of Life business logic module
[INFO] task-segment: [versions:display-dependency-updates]
[INFO] ------------------------------------------------------------------------
[INFO] [versions:display-dependency-updates {execution: default-cli}]
[INFO] The following dependencies in Dependencies are using the newest version:
[INFO] commons-cli:commons-cli .......................................... 1.2
[INFO] org.hamcrest:hamcrest-all ........................................ 1.1
[INFO]
[INFO] The following dependencies in Dependencies have newer versions:
[INFO] junit:junit ............................................... 4.4 -> 4.7
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL

Of course, plugins are in the same boat, so you might also want to run mvn versions:display-plugin-updates to check which plugins have more recent versions available. While it's at it, it will also let you know if any plugins don't have their versions specified (which is generally not a good idea):

 

 

 

 

$ mvn versions:display-plugin-updates
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'versions'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Game of Life business logic module
[INFO] task-segment: [versions:display-plugin-updates]
[INFO] ------------------------------------------------------------------------
[INFO] [versions:display-plugin-updates {execution: default-cli}]
[INFO]
[INFO] The following plugin updates are available:
[INFO] maven-checkstyle-plugin .................................. 2.2 -> 2.4
[INFO] maven-clean-plugin ....................................... 2.2 -> 2.4
[INFO] maven-deploy-plugin ...................................... 2.4 -> 2.5
[INFO] maven-surefire-report-plugin ........................... 2.4.3 -> 2.5
[INFO] org.codehaus.mojo:findbugs-maven-plugin ................ 1.2 -> 2.3.1
[INFO]
[WARNING] The following plugins do not have their version specified:
[WARNING] maven-clean-plugin .......................... (from super-pom) 2.4
[WARNING] maven-deploy-plugin ......................... (from super-pom) 2.5
[WARNING] maven-site-plugin ........................... (from super-pom) 2.1
[WARNING] maven-surefire-plugin ....................... (from super-pom) 2.5
But the Versions plugin doesn't just stop at reporting: you can also use it to actually update the version numbers in your pom.xml files. You do this via the mvn versions:set command. A bit like the Maven Release Plugin, but without the ceremony. In the following listing, we are upgrading our project version from 1.0.0-SNAPSHOT to 1.0.2-SNAPSHOT. This will update all of the versions in any modules as well, and also in any inter-module dependencies:

 

 

 

 

$mvn versions:set -DnewVersion=1.0.2-SNAPSHOT
[INFO] ----------------------------------------------------------------------------------
[INFO] Building tweeter 1.0.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:1.2:set (default-cli) @ tweeter ---
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: /Users/johnsmart/Projects/Training/coding-dojos/wellington-coding-dojo/tweeter
[INFO] Processing co.nz.codingdojo.tweeter:tweeter
[INFO] Updating project co.nz.codingdojo.tweeter:tweeter
[INFO] from version 1.0.0-SNAPSHOT to 1.0.2-SNAPSHOT
[INFO]
[INFO] Processing co.nz.codingdojo.tweeter:tweeter-core
[INFO] Updating parent co.nz.codingdojo.tweeter:tweeter
[INFO] from version 1.0.0-SNAPSHOT to 1.0.2-SNAPSHOT
[INFO]
[INFO] Processing co.nz.codingdojo.tweeter:tweeter-services
[INFO] Updating parent co.nz.codingdojo.tweeter:tweeter
[INFO] from version 1.0.0-SNAPSHOT to 1.0.2-SNAPSHOT

But you might also want to update your dependency versions while your at it. If you want to update them all in one fell swoop, you can use the mvn versions:use-latest-versions command, as shown here:

 

 

 

 

$ mvn versions:use-latest-versions
[INFO] ----------------------------------------------------------------------------------
[INFO] Building tweeter 1.0.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------------
...
[INFO] artifact javax.servlet:jstl: checking for updates from Nexus
[INFO] artifact net.sourceforge.jwebunit:jwebunit-htmlunit-plugin: checking for updates from Nexus
[INFO] Updated net.sourceforge.jwebunit:jwebunit-htmlunit-plugin:jar:null:2.1 to version 2.4
[INFO] artifact javax.servlet:servlet-api: checking for updates from Nexus
[INFO] Updated javax.servlet:servlet-api:jar:null:2.4 to version 2.5
[INFO] artifact org.springframework:spring-mock: checking for updates from Nexus
[INFO] artifact org.springframework:spring-core: checking for updates from Nexus
[INFO] Updated org.springframework:spring-core:jar:null:2.5.6 to version 3.0.3.RELEASE
[INFO] artifact com.google.inject.extensions:guice-servlet: checking for updates from Nexus
[INFO] ------------------------------------------------------------------------------------------------

Other goals worth mentioning are mvn versions:use-latest-releases, which replaces SNAPSHOT dependencies with the latest release, if they are more recent, and mvn versions:use-releases, which replaces any SNAPSHOT dependencies that have been released with the corresponding release versions. So, if you run mvn versions:use-releases, if would update from version 1.0.0-SNAPSHOT to version 1.0.0, if it is available, whereas mvn versions:use-latest-releases would bump it up to 1.0.2 if there is a 1.0.2 available. There is also versions:use-next-snapshots and versions:use-latest-snapshots, which do the same thing for SNAPSHOT versions.

Finally, once your happy with your new versions, use mvn versions:commit to set your changes in stone. This removes the backup files that the Versions plugin has been keeping, just in case. And if you finally decide it was all a horrible mistake, just run mvn versions:revert, and you will be back to the state you started with. Easy as!

If you want to learn more about the finer points of build automation with Maven, and a heap of other interesting stuff, be sure to check out the Java Power Tools bootcamps, coming up real soon in London and Canberra. Check it out!

From http://weblogs.java.net/blog/johnsmart/archive/2010/08/18/managing-version-numbers-maven-maven-version-plugin

Apache Maven

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Custom Validators in Quarkus
  • Front-End Troubleshooting Using OpenTelemetry
  • How To Build a Spring Boot GraalVM Image
  • Comparing Map.of() and New HashMap() in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: