I’m on a hot streak tonight… Another comon problem that I have had to solve recently is how to attach a build number to files that are being generated out of an Ant build script.
We have developed a versioning scheme (inspired by http://geekswithblogs.net/emanish/archive/2006/09/25/92219.aspx) for our releases where our release are named <major>.<minor>.<revision>.<build>. So a valid build number might be 1.18.2.1077. This means it belongs to the first major release of the software, the eighteenth minor version, it is the second attempt at a release for this minor version (usually bug fixes), and it is related to the SVN revision number 1077. Any time we are doing a build we are tagging in our Subversion repository to keep the build number tied to the tag. When I build a release, I want my artifacts to look like to look like this: app-1.18.2.1077.ear.
There are a few ways to do this, One way that I considered was to just include a property or Subversion tag in the build.properties file. But I did not want to maintain it and I have always loathed the auto tags that were available in CVS.
My second option was to use the <svn> Ant task. While this will probably work, I have varying degrees of success using the Java SVN library, especially when connecting to Subversion over SSH as we are doing for this application.
So, I decided to go with using the task in conjunction with svn info and svnversion to set the full.build.version property to the current release number. This assumes that the repository checked out is of the format svn://REPO_NAME/tags/<MAJOR>.<MINOR>.<REVISION>
<exec outputproperty="build.current.revision" executable="svnversion">
<arg line="-n -c" />
<redirector>
<outputfilterchain>
<tokenfilter>
<replaceregex pattern="^[0-9]*:?" replace="" flags="g"/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
<exec outputproperty="build.current.version" executable="svn">
<arg line="info" />
<redirector>
<outputfilterchain>
<linecontainsregexp><regexp pattern="^URL:" /></linecontainsregexp>
<tokenfilter>
<replaceregex pattern=".*\/([^\/]+)$" replace="\1" flags="g"/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
<property name="full.build.version" value="${build.current.version}.${build.current.revision}" />
I’m sure that I could have gotten the revision from the svn info rather than svnversion, saving a command, but I wasn’t inspired enough to do it. Maybe when I have more time.
Popularity: 91% [?]











[...] bookmarks tagged tagging Creating a Build Number With Ant and Subversion saved by 2 others skitty1994 bookmarked on 01/26/08 | [...]
I made a slight modification to the XML to get the current change number (svnversion -c) rather than the current repository number. This leads to more consistency.
That’s really cool, nice script.
Links and Resources…
This page is for interesting links and resources about tools and processes. Articles…
This is how we do it here. Much simpler.
(Let’s see if this formats correctly so that my snippet shows…)
This is how we do it here. Much simpler.
<exec executable=”svn” output=”svninfo.xml”>
<arg line=”info –xml”/>
</exec>
<xmlproperty file=”svninfo.xml” collapseattributes=”true”/>
<property name=”svn.revision” value=”${info.entry.revision}”/>
Love that Justin. Thanks for the suggestion on how to get the revision #. I still need the tag for the first part, but I will adopt your suggestion for the second part.
[...] (ubuntu svn subversion eclipse) Bookmark [Discover] Creating a Build Number With Ant and Subversion | Innovation On The Run http://www.innovationontherun.com/creating-a-build-number-with-ant-and-subversion/ (svn subversion [...]
How do I get the name of the tag that was checked out? ie. I want to display the tag name and revision # in my About dialog.
@rsvn…Do you mean how would you get the tag from the Ant file? the exec portion of the XML will print out the version.
I’ve created an ant task which is far easier to use in order to get the revision number.
http://code.google.com/p/svntask/
@Jon Looks like a very nice solution. Thanks.
Software Versioning…
I’m trying to settle on a sensible software versioning scheme for Potential RPG. The most familiar structure is X.Y.Z, which I’m calling Generation.Feature.Incremental.
The generation is the major version of the software. A feature release …
My suggestion if you just want to get the revision number and have no qualms to read a svn file. You need no executables an no libraries – just ant.
And here the ant xml (it was removed from my previous post).
<loadfile property=”revisionVersion” srcFile=”./.svn/entries”>
<filterchain>
<headfilter lines=”1″ skip=”3″/>
</filterchain>
</loadfile>
The filterchain example only returns the first revision number in the entries file thus it stays the same. You need to look at the last added entry for this to work.
You saved my day, epic win!
[...] line command “svnversion” directly from ANT and with a little help from here and here, I wrote a nice little macro that can be used from ANT [...]
I’ve recently switched from RCS to Subversion. Here’s how I build the values for my About dialog in Java using Subversion keywords.
// Extract revision number
StringTokenizer st = new StringTokenizer(“$Revision: 40 $”);
st.nextToken();
revision = Integer.parseInt(st.nextToken());
…
buildNumStr = props.getProperty(MyProperties.BUILD_NUMBER);
buildInfoStr = buildNumStr + ” (Rev ” + revision + “), ” +
props.getProperty(MyProperties.BUILD_DATE);
http://subclipse.tigris.org/svnant/svn.html#wcVersion
look into svnkit
svn.wcv.committed.max=3707
svn.wcv.committed.max-with-flags=3707M
svn.wcv.modified=true
svn.wcv.repository.path=/path/to/dir
svn.wcv.repository.url=http\://path/to/repo
svn.wcv.revision.max=3707
svn.wcv.revision.max-with-flags=3707M
svn.wcv.revision.range=3707M