Feb 022013
 
Share...Tweet about this on Twitter2Share on Facebook0Share on Google+1Share on StumbleUpon1Share on LinkedIn5Flattr the authorPin on Pinterest0Share on Reddit0Share on Tumblr

If you, like me, are using Subversion as your primary Version System and want to embed the SVN revision number directly in the assembly, but don’t know how, this post will try to explain it.

Embedd_Revision_In_Assembly_Process

What do you need?

Before proceeding any further, you would need to download the following two packages (free!)

I assume that you are familiar with MsBuild and how to work with SVN.

What is the process to follow

In the explanation I assume the build process is made out of a Build Server (Hudson, Bamboo, Team City, or others..) but exactly the same process applies if the compilation is done manually.

In the image above, I am showing two different steps:

  1. Developer, committing the code in the SVN repository
  2. In my case, a Continuous Integration server that compiles the project. Steps that the Build Service has to perform are:
    • Download the latest version of the source code
    • Change the AssemblyInfo.cs file with the Repository Revision number. I will show how to do this, just keep reading.
    • Compile the solution.

It’s pretty much clear and simple to follow this steps.

Preprocessing the AssemblyInfo.cs in MsBuild

The main point of the whole process in the building (compilation) of the project.
The following MsBuild Target will help you with the “pre-processing”, which means, changing the AssemblyInfo.cs file before the code is compiled. In this way we are able to embed the revision number.

Two tasks are used from the MsBuild Community Tasks

  • SvnVersion
  • FileUpdate

so on top of your MsBuild file you would need to include the following declaration:

<UsingTask TaskName="SvnVersion" 
           AssemblyFile="MSBuild.Community.Tasks.dll"/>
                  
<UsingTask TaskName="FileUpdate"
           AssemblyFile="MSBuild.Community.Tasks.dll"/>

and use this target to be executed BEFORE the main compilation.
For brevity I am including only the Target itself, as your build process could vary.

<!-- ******************************
@@ PRE-BUILD PROCESSING
@@ Sets the revision version for the assemblies.
*********************************** -->
<Target Name="PreBuild">             
  <ItemGroup>
      <!-- find all the AssemblyInfo.cs files in the solution-->
      <AssemblyInfoFiles 
               Include="$(BuildPath)\*\Properties\AssemblyInfo.cs"/>
  </ItemGroup>
       
  <PropertyGroup>
      <SVNToolPath><!--Path to the svnVersion.exe--></SVNToolPath>
  </PropertyGroup>
       
  <!-- Getting the revision number from svn by using the svnVersion-->       
  <SvnVersion LocalPath="$(BuildPath)" ToolPath="$(SVNToolPath)" >
      <Output TaskParameter="Revision" PropertyName="Revision" />
  </SvnVersion>
  <Message Text="Revision: $(Revision)"/>

  <!--uploading the AssemblyInfo.cs files-->
  <FileUpdate Files="@(AssemblyInfoFiles)"
              Regex='(d+).(d+).(d+).(d+)'
              ReplacementText="$1.$2.$3.$(Revision)" />
</Target>

Pretty much simple. Isn’t it?
In my case the revision number is 5683 and you may see the end result in the picture below:
File-Version

Conclusion

I believe that this is a very effective and simple way of including the revision number directly in the assembly, which makes the revision tracking simpler overall.
If you think there are other and more effective ways, please let me know!

zoran maksimovic
About the author:
My name is Zoran Maksimovic. I'm a passionate programmer and interested in everything about Software Development, Object-Oriented Design and Software Architecture. Feel free to contact me or to know more about me in the about section.
Share...Tweet about this on Twitter2Share on Facebook0Share on Google+1Share on StumbleUpon1Share on LinkedIn5Flattr the authorPin on Pinterest0Share on Reddit0Share on Tumblr

  7 Responses to “Embedding SVN revision number at compile time with MsBuild”

  1. Hi Zoran,

    I’ve tried to do this but i don’t have svnVersion.exe. I’m using tortoise SVN 1.7.

    Thank you!

  2. Hi Zoran,

    I get the message:
    27208:27210M
    Revision: 27210
    Updating File “c:\work\Projects\trunk\Gr\Lisp\Prac\MyProject\Properties\AssemblyIn
    fo.cs”.
    Replaced matches with “$1.$2.$3.27210″.

    but I see no changes in the AsseemblyInfo.cs:
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]

    What is wrong?

    Thanx,
    Paul

  3. Nice post!

    We use MSBuild Versioning (versioning.codeplex.com) to perform the pre-build processing.

    At the end the build date information is in the 3d group and the svn-revision number in the 4th digit group, as in your case. Since our revision numbers are still low enough (3 digits), we use the 4th digit to signal commit status. If the assembly was built using non committed code (dirty) it ends with 1, otherwise wit 0.

    Regards,
    Dalibor

    • Hi Dalibor,
      I checked the versioning.codeplex.com that you mentioned, and indeed is a good tool and probaly we will try to integrate it. I think that the solution here expressed mimics pretty much what that tool does.
      I have to add that what I’ve described in the above post, works well when it comes to run the process in an CI environment. I noticed one small annoyance when building the solution on the workstation. Before the compilation starts, the assemblyinfo.cs is changed, and then code get’s compiled. What is missing in this process is to re-change again the AssemblyInfo.cs files to what they’ve been before the compilation process, otherwise you have to delete those files manually and reload them from SVN. Nothing serious but worth investigating.

      Cheers,
      Zoran

Leave a Reply

royals_mariko@mailxu.com