Using the Subversion Revision Number in Your Application

Problem: You want to tie the tagged subversion revision number for a build to the application you are releasing.

Issues:

  1. Subversion and Ant aren't working together.
  2. How do you incoporate the revision number into the system you are building?

Solution

The solution consists of three simple steps.

Step 1: Get the Subversion revision number

The simplest way is to create a very quick shell or bat file (for those who simply must use a Windows box). Below is an example, although you may have to tweak it slightly for your operating system. This was written for Red Hat Linux.

   $ svn info file:///opt/SVN_REPOS/MDE/DISA_MDE/source |
      grep "^Revision" | awk '{print $2}'

All this is doing is running the svn command to get the latest Subversion information (which includes the revision number). The output is then piped into grep and then awk to extract only the revision number. I have seen similar scripts with complicated Perl commands that might or might not work. I believe in the KISS principle, keeping it as simple as possible. This script should be fairly easy for anyone to understand and modify as needed.

You now have the revision number being returned from the shell script.

Step 2. Get the revision number into your Ant script

This step is just going to execute the shell script from Step 1 of the solution. The result from the script (the Subversion revision number) is then placed in the output property field with the property name "svn.revision.number".

I am also echoing it during the execution of the Ant script for verbosity. The relevant target from the Ant script is shown below:

   <target name="local-versioning" if="build.deployment">
   <exec dir="." executable="/home/source/projects/myApp/svnVersion.sh"                     
       outputproperty="svn.revision.number">
   </exec>
   <echo>SVN Revision Number ${svn.revision.number}</echo>
   </target>

Note that I explicitly hard-coded full paths in the script, which is fine for demonstration code. You can make the references relative, etc. I wanted this to be a very easy example.

Step 3. Get the revision number from your Ant script into your GUI's About dialog

This should be something most developers have seen done in Ant before. I have a properties file that is used to populate data for the About dialog. It's in typical "key=value" pairs like most property files. So I use an Ant task to set the field I use for the build number.

      <propertyfile file="web/WEB-INF/versioning.properties">
           <entry key="svn.revision.number" type="int" 
               operation="=" value="${svn.revision.number}" />
      </propertyfile>

The final Ant task looks like this:

 <target name="local-versioning" if="build.deployment">
 <exec dir="." executable="/home/source/projects/myApp/svnVersion.sh"                     
     outputproperty="svn.revision.number">
 </exec>
 <ech>>SVN Revision Number ${svn.revision.number}</echo>

 <propertyfile file="web/WEB-INF/versioning.properties">
         <entry key="svn.revision.number" type="int" operation="=" 
             value="${svn.revision.number}" />
 </propertyfile>
 </target>

Total lines of code in solution: 10 (9 Ant, 1 shell)

I hope this helps. I took a few hours hunting down different solutions and testing them out. I think this is simple enough that most developers can get it running without too much trouble. Good luck!



Comments

Steve Keener By Ade on Friday, April 27, 2012 at 06:02 PM EST

What I was looking for - perfect!!!


Leave a Comment

Comments are moderated and will not appear on the site until reviewed.

(not displayed)