Build Stamping: Embedding Build Numbers in Application Assemblies

When errors occur in applications developers use stack traces to to help them figure out where in the code something went wrong. The stack trace reports the the class and method from which an error originated, and depending on availability of debug symbols which line in the source file each of those class and methods up the call stack are associated.

Sometimes however it can be challenging to find the correct set of source files that were used to compile up the application.

Team Build helps by generating a unique build number whenever it produces a build, and placing all the build outputs into a specific drop folder, but it can be hard to trace back from a single DLL in production to the build number that it came from.

This is where build stamping comes in. Build stamping (not sure if this is what everyone calls it) is a technique where you embed some kind of identifier into the assembly metadata. A lot of people use a standard .NET version string (w.x.y.z) but I prefer to use the build number from Team Build (e.g. TrunkHelloWorld_20070830.5) and put it in something like the assembly description.

The purpose behind this post is to explain one possible way of performing this build stamping using Team Build, MSBuild and the MSBuild Community Tasks.

First, download and install the latest build of the MSBuild Community Tasks mentioned above. This set of libraries and custom build targets. Next you need to add the following code (Import element, and Target element) to the TFSBuild.proj file under the Team Build Type definition.

image

This will, then, during a build update all the AssemblyInfo.vb (or AssemblyInfo.cs if you change it accordingly) files under the solution root and replace the text “DeveloperUseOnly” in those files with the build number. All you need to do is put that text inside the metadata element where you want the information to appear and bingo – you are now stamping the builds with the Team Build build number.

I’d recommend putting it in the description because it shows up in the list of properties when you right mouse click the file in Windows Explorer.

5 thoughts on “Build Stamping: Embedding Build Numbers in Application Assemblies

  1. Paul Stovell

    I would have liked it if TFS could provide some kind of mapping for regular version numbers (a.b.c.d) and TFS Build Numbers so that you could map the assembly version back to a build.

    For TrialBalance back in the day I created a custom build task that called a Web Service, passing in the Team Build Number and getting back a version number – it then mapped them up in a database for me. That way I could stamp the AssemblyVersion attributes in my AssemblyInfo files with a version number, and map that version number back to a build if I needed to.

    Of course, mucking around writing stuff like that is probably the reason I never finished the application🙂

  2. Paul Stovell

    I’d seen that target override before actually when I was looking through the overridable targets list. I’m not sure how safe it would be to replace with a flat version number though – if you were building a number of applications, you could have a number of builds labelled “1.0.0.0”. I don’t think this would affect the build URI though so perhaps it would be safe.

  3. William Bartholomew

    Paul,

    If you allocate multiple builds the same build number it will fail… What I do inside the BuildNumberOverrideTarget is:

    1. Generate a unique version number (I use the Version task from msbuildtasks.tigris.org)
    2. Put this into a variable called VersionNumber
    3. Set BuildNumber = $(BuildType)_$(VersionNumber)
    4. Override the BeforeCompile target to update the AssemblyInfo.* files and set the AssemblyVersion and AssemblyFileVersion to $(VersionNumber)

    William

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s