| Managing version numbers in Maven with the Maven Versions plugin |
|
|
|
| Wednesday, 18 August 2010 23:43 |
|
If you have a Maven project of any size, particularly involving many modules or large numbers of dependencies, you have probably come across issues 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:
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):
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:
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:
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!
Bookmark
Email this
Trackback(0)
Comments (4)
![]() written by Thomas COURANT, August 19, 2010
Thank you, i didn't know that this plugin handles all these useful operations !!
written by Burkhard Graves, August 20, 2010
Nice tool in principle, but unfortunately sometimes wrong, e.g.:
[INFO] The following dependencies in Dependency Management have newer versions: [INFO] c3p0:c3p0 ........................................... 0.9.1.2 -> 0.9.0 [INFO] org.springframework:spring-core ............... 3.0.4.RELEASE -> 2.5.6 written by John Ferguson Smart, August 20, 2010
Actually, that's Maven - Maven only guarantees to be able to compare version numbers in the standard 3-number format (0.9.0 is, but 0.9.1.2 isn't; 3.0.4-RELEASE would have been, but 3.0.4.RELEASE isn't). That's why the plugin behaves strangely with version numbers like this.
Write comment
|