How to link the svn revision with maven pom.xml project version

For a while now we are using tortoise svn to maintain our code repository, no question asked it is an excellent tool.
Now we also have nexus to store our generated artifacts/jar, but I can’t seem to find a way to identify from which svn revision this particular jar file was generated.

Also all of us are quite lazy to go up to the version node in the pom.xml and change it, and for months and months we are usually generating the same version, which is 1.0-SNAPSHOT.

I did a bit of digging around and tried something very simple..
STEP 1: Added a placeholder in pom.xml and property field in settings.xml
STEP 2: Made sure the mvn.bat performs a svn update at mvn build and supplies the property value to it.

STEP 1 goes something like this : pom.xml with variable ${svn.version}


Just to make sure the editor(netbeans) build doesn’t complain, I added a placeholder in settings.xml


STEP 2 was a bit trickier, keep a backup in case you break it, it is updating the mvn/mvn.bat
The dirty fix is to edit mvn.bat and at the top just add this bit above the Apache license declaration.

@REM ----------------------------------------------------------------------------
@REM Farlin : Adding a bit of logic to update project version number with svn revision
@REM ----------------------------------------------------------------------------
:; Turn echo off (above) to keep it clean
:; Clear any previous variables set, and set your desired property name
SET PropertiesName=svn.version
SET SvnCurrentRev=
:; Get the output from the "svn up" and trim to revision number
SET temp=
FOR /F "tokens=*" %%i in ('svn up') do SET temp=%%i
FOR /F "tokens=3 delims= " %%A IN ("%temp%" ) DO (
SET temp=%%A
FOR /F "tokens=1 delims=." %%A IN ("%temp%" ) DO (
SET SvnCurrentRev=%%A
:;ECHO %SvnCurrentRev%
:; create a mvn commandline switch with svn revision number
SET MAVEN_CMD_LINE_BUILD_VERSION=-D%PropertiesName%=%SvnCurrentRev%
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one

and also locate the place where Start MAVEN2 is configured in the mvn.bat file

%MAVEN_JAVA_EXE% %MAVEN_OPTS% -classpath %CLASSWORLDS_JAR% "-Dclassworlds.conf=%M2_HOME%\bin\m2.conf" "-Dmaven.home=%M2_HOME%" org.codehaus.classworlds.Launcher %MAVEN_CMD_LINE_ARGS% %MAVEN_CMD_LINE_BUILD_VERSION%
if ERRORLEVEL 1 goto error
goto end

This should do the trick and append the revision number when you do a mvn build.
To check, go to your checked out version and do :

mvn clean install

Want to check if this worked? Just browse inside your *.jar/*.war and inspect the file called
you will see :

#Generated by Maven
#Fri Jul 09 15:05:59 EST 2010

And when you deploy your artifact to nexus, it will automatically create a folder for your jar with respective build number. Hope this helps!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s