Team Build and the AssemblyInfo Task

Gautam Goenka, dev lead for Team Build v1, wrote a blog post way back in January of 2004 that laid out an approach for using the AssemblyInfo task to update version numbers with Team Build.  I refer people to this post all the time, and have never had anyone report any issues with the post...  As such, I never really looked at it too closely, and didn't really understand how it worked. 

Earlier this week, however, a customer was running into some trouble here and I figured the time had finally come for me to dig a bit deeper. 

The idea with the AssemblyInfo task is to (a) import Microsoft.VersionNumber.targets, (b) populate the AssemblyInfoFiles item group with the list of AssemblyInfo.* files to be updated, and (c) set whatever properties you want to modify - AssemblyMajorVersion, AssemblyMinorVersion, etc.  Internally, this all works due to the following bit of markup in Microsoft.VersionNumber.targets:

 <!-- Re-define CoreCompileDependsOn to ensure the assemblyinfo files are updated before compilation. -->

This adds the UpdateAssemblyInfoFiles target, which executes the AssemblyInfo task, to the dependencies of the CoreCompile target.  By convention, each of the various project types built by MSBuild declare their CoreCompile targets as follows:

   <Target Name="CoreCompile" DependsOnTargets="$(CoreCompileDependsOn)">

This is true of Microsoft.CSharp.targets, Microsoft.VisualBasic.targets, etc.  Crucially, it is also true of Microsoft.TeamFoundation.Build.targets, which defines the Team Build build process.

So - if you want to use the AssemblyInfo task to update the version numbers for an individual C# project, you can follow the above steps in your *.csproj file.  In this case, the individual C# project's AssemblyInfo.cs file would get updated just before the project was compiled.

If, on the other hand, you want to update the version numbers for all of the projects built during your Team Build build, you can follow the above steps in your TfsBuild.proj file, as laid out in Gautam's blog post.  In this case, all of the AssemblyInfo.* files included in the AssemblyInfoFiles item group would get updated just before Team Build starts the compilation of all of the solutions in the SolutionToBuild item group.