Wednesday, November 24, 2010

Maven versions plugin breaks when version number is inherited from parent

In release management cycle, it is common to force a version number on to maven POM before the artifact is deployed to maven repository. This type of situation arises when, for instance, releases are managed by Continuous Integration tools, such as Cruise Control, Hudson, or Anthill Pro, and it feels redundant to have to go into source code, manually update version number in the POM and commit it. If managing entire release is not done via maven release plugin, then maven versions plugin can come in handy to just set the version number:

mvn versions:set -DnewVersion=1.0.0

There is one nuance though. In some cases, project version is inherited from parent, and project's POM uses parent POM, like this:

<parent>
    <groupId>com.company</groupId>
    <artifactId>company-parent</artifactId>
    <version>1.0</version>
    <relativePath>../company-parent/pom.xml</relativePath>
</parent> 

In this case for a local maven build, version does not have to be defined in POM as it will be 2.0, the version number of parent. However, the mvn versions:set command will result in error with the following message:


[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Project version is inherited from parent.

Thus, some version has to be added to the project. If project still has to have parent version for some reason, then just copy version from inside parent tag. Otherwise, just go with any valid version number.