Adding Subversion Revision Numbers to your .NET NANT Builds
Posted by Tom on 2009-12-02 21:47
If you use .NET, Subversion and NAnt as your personal constellation of technologies then you've probably found yourself wondering if you can tie the lot together and have NAnt include your Subversion revision number in as the version number of your assembly. I've tried a few different approaches over the years and settled on this one (for now!)
The clever bit of the code (the part which actually grabs the revision number and gets it into NAnt) has been leeched wholesale from Jonathan Malek, with the added twist that it creates a C# file with the required attributes in it.
<?xml version="1.0"?>
<project name="SetVersion" default="all" basedir="../../">
<target name="compile.setversion">
<property name="svn.revision" value="0"/>
<exec
program="svn"
commandline='log "${project.src.dir}" --xml --limit 1'
output="${project.src.dir}\_revision.xml"
failonerror="true" />
<xmlpeek
file="${project.src.dir}\_revision.xml"
xpath="/log/logentry/@revision"
property="svn.revision"
failonerror="true" />
<echo file="${project.src.dir}\Version.cs" append="false" message="
[assembly: System.Reflection.AssemblyVersion("1.0.${svn.revision}")]
[assembly: System.Reflection.AssemblyFileVersion("${svn.revision}")]" />
</target>
</project>
That sits in its own file. Then, whenever we need to stuff a version number into an assembly we simply make sure we're including it:
<include buildfile="etc/nant/SetVersion.build" />
and call it just before our csc task:
<property name="project.src.dir" value="project/src" />
<call target="compile.setversion" />
<csc target="library" output="${out.dir}/${project.name}.dll">
<sources>
<include name="${project.src.dir}/**/*.cs" />
</sources>
</csc>
Job done!