ams
ams

Reputation: 62782

How to use the maven versions plugin to pick the next version number?

I have maven project structure that looks something like this

project/
   pom.xml  // parent and aggregator pom
   module1/
      pom.xml
   module2/
      pom.xml

What I want to do is to change the project version starting with the pom.xml and working down to the child project.

I can use the versions plugin mvn versions:set -DnewVersion=1.0.3-SNAPSHOT to explicitly specify the new version and it does the right thing of changing the version number of the project to the explicitly specified version.

Is there a way to use the versions plugin so that it picks the next version number automtically based on the current version number.

Upvotes: 0

Views: 1137

Answers (1)

Eldad Assis
Eldad Assis

Reputation: 11055

I had a similar need, and I decided to implement an external bash script that manages this.

We don't use the maven-release plugin as we don't work with SNAPSHOTs and don't use the SCM connection.

I load the current version from the pom.xml

CURRENT_VERSION=`echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /`

and run some logic on it to set the new version. Then, I run

mvn versions:set -DnewVersion=$MY_NEW_VALUE

After that, I run the build.

Here is a generic example of what I do

#!/bin/bash
# Increment an existing version in pom.xml and run a new build with it

# Error message and exit 1
abort()
{
    echo;echo "ERROR: $1";echo
    exit 1
}

# Accept a version string and increment its last element (assume string is passed correctly)
incrementVersionLastElement()
{
    IN=$1

    VER1=`echo $IN | awk -F\. 'BEGIN{i=2}{res=$1; while(i<NF){res=res"."$i; i++}print res}'`
    VER2=`echo $IN | awk -F\. '{print $NF}'`
    VER2=`expr $VER2 + 1`
    OUT="$VER1.$VER2"

    echo $OUT
}

# Getting project version from pom.xml
PROJECT_VERSION=`echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:version/text()' | xmllint --shell pom.xml | grep -v /`
echo Current project version: $PROJECT_VERSION

NEW_PROJECT_VERSION=`incrementVersionLastElement $PROJECT_VERSION`

# Setting the new version
mvn versions:set -DnewVersion=$NEW_PROJECT_VERSION
if [ "$?" != "0" ]
then
    abort "maven failed"
fi

# Run the maven main build
mvn clean install
if [ "$?" != "0" ]
then
    abort "maven failed"
fi

It's rough, but it works well for us for over a year.

I hope this helps.

Upvotes: 1

Related Questions