In this article, I will show you a quick way to use SVN revision number as a part of your software version. The small ANT target that I will show you below will enable you to extract and integrate the SVN revision number, during compile time, into your source code. This will enable you to show an updated version number of your application anywhere on your application.
Subversion (SVN) is my favorite tool for version control. After using SCCS, RCS, CVS, ClearCase, Perfoce for sometime, I realized that I'm feeling much more happier with SVN.
In this article, I will show you a quick way to use SVN revision number as a part of your software version. The small ANT target that I will show you below will enable you to extract and integrate the SVN revision number, during compile time, into your source code. This will enable you to show an updated version number of your application anywhere on your application.
One of the useful features of SVN is to be able to receive the response of each SVN action XML formatted. This will enable us to quickly parse and use the data.
Before we start, you will need to download The Andriel Tasks for ANT. Andriel Tasks provides XPath functionality for ANT. It will be used to quickly parse the revision number from the XML formated SVN result.
In order to use Andriel Tasks, we will need to add a taskdef into our ANT build.xml file:
<!-- Andriel -->
<taskdef resource="net/uworks/andariel/andariel.properties"
classpath="${build.lib}/andariel-1.2.3.jar" />
And here is our ANT target for querying SVN to get the revision number as an ANT property.
<!-- Set version -->
<target name="set-version">
<property name="svn.info" value="svn.info" />
<!-- Get last SVN revision. -->
<exec executable="svn" output="${svn.info}">
<arg value="info" />
<arg value="--revision=HEAD" />
<arg value="--xml" />
</exec>
<!-- Query revision. -->
<xpath file="${svn.info}"
expression="//entry/@revision"
outputproperty="build.revision" />
<!-- Delete temp revision file. -->
<delete file="${svn.info}" />
<!-- Set version. -->
<property name="build.version"
value="1.0.${build.revision}" />
<!-- Show version -->
<echo message="Version ${build.version}" />
</target>
Simply add this target as a dependency to your compile target. It will query SVN and it will store the revision number inside the build.version property.
You can now use it in your ANT build.xml file for any purpose. If you would like to make this information available in your application as well, then you can use the following ANT target to do a quick keyword substitution.
I will be refering to my Java files with keywords as template files. They will be named accordingly with a .tpl extension. Here is a quick example:
BuildConst.tpl:
/**
* Build constants.
*
* !!!! DO NOT EDIT THIS FILE, EDIT TPL FILE INSTEAD !!!!
*/
final class BuildConst
{
/** Server url. */
public static final String VERSION = "@build.version@";
}
During compile time, the keyword @build.version@ will be replaced with the SVN revision number, and a new file with .java extension will be created.
<target name="filter-tpl">
<echo message="Processing the templates." />
<copy todir="${build.src}" overwrite="true" verbose="true">
<fileset dir="${build.src}">
<include name="**/*.tpl" />
</fileset>
<filterset>
<filter token="build.version" value="${build.version}" />
</filterset>
<globmapper from="*.tpl" to="*.java" />
</copy>
</target>
In our application, we can refer to BuildConst.VERSION to get the SVN revision number, so that we can display it.
BuildConst.java:
/**
* Build constants.
*
* !!!! DO NOT EDIT THIS FILE, EDIT TPL FILE INSTEAD !!!!
*/
final class BuildConst
{
/** Server url. */
public static final String VERSION = "1.0.40";
}