Simple assembly versioning with Team Build 2010
There are many more sophisticated ways of achieving this but none seemed to give me exactly what I wanted, so here I present yet another way of doing assembly versioning with Team Build 2010. I wanted my Team Build number to exactly match that of my assembly versions (and not be derived), like this:
So here’s how I do it. To start off with I customise the BuildNumber format within my build definition:
In my case, I decided to customise it so that the Major and Minor version numbers “0.1” were added explicitly. This lets me control the first two parts of the version number which is what I want to achieve. I also added the macros $(Month)$(DayOfMonth) with a 1 in front of it. For the 2nd May 2010 this would generate a number 10502. (The reason I don’t use the full year here is that for today it would generate a build number of 100502 and a file version number cannot be higher than 65335).
When I decide to work on version 0.2, 0.3 or 1.0 all I have to do is increment the Build Number here and save the definition, I’m also happy to increment the number when the year changes. I said it was simple!
The final part of the build number format was left as-is (i.e. the Revision number that increments by 1 with each build on that day and resets for the next day).
Now all we need to do is retrieve this version number when MSBuild is run against the solution, split the version number and take the numeric portion into the Properties\AssemblyVersion.cs file (you will need to comment out the AssemblyFileVersion line in that file first and check it in).
Here’s the fragment that you’ll need to insert in your .csproj file (you’ll have to check it out then open it in Notepad or your favourite text editor).
<UsingTask TaskName="Microsoft.TeamFoundation.Build.Tasks.GetBuildProperties" AssemblyFile="$(MSBuildProgramFiles32)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\Microsoft.TeamFoundation.Build.ProcessComponents.dll" Condition="' $(BuildUri) '!=' '"/> <Target Name="BeforeBuild" Condition="' $(BuildUri) '!=' '"> <GetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)"> <Output TaskParameter="BuildNumber" PropertyName="BuildNumber" /> </GetBuildProperties> <PropertyGroup> <BuildNumberSplitLocation>$([MSBuild]::Add($(BuildNumber.LastIndexOf('_')),1))</BuildNumberSplitLocation> </PropertyGroup> <ItemGroup> <AssemblyVersionLines Include="[assembly:AssemblyFileVersion("$(BuildNumber.Substring($(BuildNumberSplitLocation)))")]" /> </ItemGroup> <Exec Command="attrib -r "$(ProjectDir)\Properties\AssemblyInfo.cs"" ContinueOnError="false" /> <Message Text="Lines being added: @(AssemblyVersionLines)" Importance="high" /> <WriteLinesToFile File="$(ProjectDir)\Properties\AssemblyInfo.cs" Lines="@(AssemblyVersionLines)" /> </Target>


Marcus said,
Great! This is exactly what I was looking for. Many thanks for this explanation! :)
Simon Hart said,
Nice. Thanks for this.
Simon
Bob Hardister said,
Hi,
Nice work. I have a similar approach except that it does not require modification to the .csproj files. See http://agilescmtalk.com/node/42.
Cheers,
Bob
Larry said,
Thanks for a great post!. I’m using a VB project and had to modify the fragment a bit. For anyone who could use it I’m copying it below.
Larry
$([MSBuild]::Add($(BuildNumber.LastIndexOf(‘_’)),1))
Add A Comment