Automatic AssemblyInfo generation during Team Build

environment Team Foundation Server, msbuild, each projects is located below the solution file

problem Provide team build number and flavor (debug, release) for compiled assemblies from multiple projects in a solution

solution Edit your .csproj files (make sure you do a "Checkout for edit") and add

  ...
  <Import Project="..\\AssemblyInfo.proj" />
</Project>

The AssemblyInfo.proj should be placed next to the .sln file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

  <Target Name="BeforeBuild">
    <Attrib Files="Properties\AssemblyInfo.cs" Normal="true"/>

    <!-- Build := Days since StartDate; Revision := 65534 / (24*60*60) * seconds since midnight :)  -->
    <Version BuildType="Automatic" RevisionType="Automatic" StartDate="2007/01/01" Major="1" Minor="0">
      <Output TaskParameter="Major" PropertyName="Major" />
      <Output TaskParameter="Minor" PropertyName="Minor" />
      <Output TaskParameter="Build" PropertyName="Build" />
      <Output TaskParameter="Revision" PropertyName="Revision" />
    </Version>

    <AssemblyInfo CodeLanguage="CS"
        OutputFile="Properties\AssemblyInfo.cs"
        AssemblyConfiguration="$(Configuration)"
        AssemblyCompany="Company Name"
        AssemblyProduct="$(AssemblyName) $(Configuration) $(BuildNumber)"
        AssemblyCopyright="Copyright Company Name 2007"
        AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
      	AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />

    <Attrib Files="Properties\AssemblyInfo.cs" ReadOnly="true"/>
  </Target>
</Project>

You need to install msbuildtasks on your local machine AND the build server. Apart from the Team Build revision, you'll get another auto-generated assembly revision.

debugging Unload, Reload the project after every change to AssemblyInfo.proj

links MSBuild infos

Leave a Reply

You must be logged in to post a comment.