Increment Version for Changed Assemblies only – Third Part

 28 Apr 2014 -  Giulio Vian -  ~4 Minutes

This is the third and last installment of the series: in the First post we examined the basics of extending the MSBuild / Visual Studio process, and in the Second part how to extend the Team Foundation Server Build, so that the minimum set of assembly is recompiled and gets the version updated. The limit of the latter solution is in the version number appearing in the assemblies: they have no relation with build identifiers. Let’s see how to solve this.

Identifiers and version numbers

The default TFS Build Identifier contains some numbers that cannot be directly used as file version numbers (e.g. MyBuild_20140409.1). Our objective is depicted in this snapshot.


File and Assembly version numbers are limited in range from 0 to 65535 for the Build and Revision parts, so the default Build number format $(BuildDefinitionName)\_$(Date:yyyyMMdd)$(Rev:.r) cannot be used. A better choice is $(BuildDefinitionName)_$(Year:yy)$(DayOfYear)$(Rev:.r): it will work until 2065 included.


Correlating identifiers in TFS 2013

The Build identifier is calculated during the build execution, in the Update Build number activity. To get this value, in TFS 2013, you can just read the value from the TF_BUILD_BUILDNUMBER environment variable, like in the following code for the autoIncrTfs2013.targets file (see previous post ).

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="">
    <Import Project="$(MSBuildExtensionsPath)\Microsoft\ExtensionPack\MSBuild.ExtensionPack.VersionNumber.targets"/> 

      <AssemblyInfoFiles Include="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"/>


    <Message Text="TF_BUILD_BUILDNUMBER $(TF_BUILD_BUILDNUMBER)" Importance="High" />
    <Message Text="TfsBuildNumberExtract $(TfsBuildNumberExtract)" Importance="High" />
    <Message Text="AssemblyFileBuildNumber $(AssemblyFileBuildNumber)" Importance="High" />
    <Message Text="AssemblyFileRevisionNumber $(AssemblyFileRevisionNumber)" Importance="High" />



Using the ugly but effective MSBuild Property Functions   , this code decompose the Build identifier and extract the number to use them for the Build and Revision part.

TFS 2012

TFS 2012 does not offer the environment variables: the only way to forward the build identifier is to customize the Build Template. For more information see Customize your build process template   .

The code must be inserted before the Try Compile block.


I use to mark my custom code with three asterisk so it is easy to spot and search.

The XAML code assumes that the targets file and Extension DLL are located in the same source control folder as the customized Build template.

<Sequence DisplayName="*** Hook CustomAfterMicrosoftCommonTargets 2010/2">
        <Variable x:TypeArguments="x:String" Name="customAfterMicrosoftCommonTargets" />
        <Variable x:TypeArguments="x:String" Name="localBuildTemplatePath" />
        DisplayName="Convert BuildTemplate path to local"
        Workspace="[Workspace]" />
    <Assign DisplayName="Assign customAfterMicrosoftCommonTargets">
            <OutArgument x:TypeArguments="x:String">[customAfterMicrosoftCommonTargets]</OutArgument>
            <InArgument x:TypeArguments="x:String" xml:space="preserve">[System.IO.Path.Combine(
    <Assign DisplayName="Append CustomAfterMicrosoftCommonTargets to MSBuildArguments">
            <OutArgument x:TypeArguments="x:String">[MSBuildArguments]</OutArgument>
            <InArgument x:TypeArguments="x:String">[String.Format(
        "{0} /p:CustomAfterMicrosoftCommonTargets={1} "
        , MSBuildArguments
        , customAfterMicrosoftCommonTargets)]</InArgument>
    <Assign DisplayName="Append TeamBuildNumber to MSBuildArguments">
            <OutArgument x:TypeArguments="x:String">[MSBuildArguments]</OutArgument>
            <InArgument x:TypeArguments="x:String">[String.Format(
        "{0} /p:TF_BUILD_BUILDNUMBER={1} "
        , MSBuildArguments
        , BuildDetail.BuildNumber)]</InArgument>

The ConvertWorkspaceItem translates the Build template path as a path on the local file system. Using that value computes the position of autoIncrTfs2013.targets, which is used to append the CustomAfterMicrosoftCommonTargets property to the MSBuild arguments.

I uploaded some sample code in .

Caveat and considerations

The build workspace conserve history of the builds, and each Build Definition and each Agent use a different workspace. In other words, building the same Changeset on different Agents, may give different results.

What else can go wrong? A lot of things:

  • if you lose the Binaries folder, i.e. the output directory, all projects gets recompiled;
  • code generation must be controlled by proper MSBuild input and outputs, otherwise too much or not enough code is regenerated when a template changes;
  • “wildcard” tools like WiX Heat or Web Site projects can pull-in files that have been deleted from version control but are still on the local workspace.

That said, incremental build could be extremely useful In some scenarios in a couple of scenarios:

  • incremental builds can boost the speed of Continuous Integration;
  • distributing hotfixes minimize download/deploy bandwidth;
  • incremental changes are deployed with minimum recompiles and maximum speed.

In my opinion these are good application of this technique and the risks are worthwhile.

Hope you enjoyed the series and can apply fruitfully the methods I exemplified.

comments powered by Disqus