TFS Build Number Tasks


UPDATE: 13 July 08 — These tasks are now available in the FreeToDev MSBuild Tasks Suite

UPDATE (15 May08): See this post for TFS 2008 support

I’ve added two simple tasks to the FreeToDev.MSBuild.Tasks assembly, both relate to build numbers in TFS. I’ve previously modified and used the AssemblyInfo task to get assembly versions consistent across all assemblies produced by a team build. I have a few pet hates with regard to this task though.

  1. It’s very easy to get into ‘unable to determine the workspace’ hell
  2. You typically have to change the code to suite your build number format
  3. It updates the assemblyinfo files in source control
  4. It crashes if any files it processes do not contain versioning attributes.

The following tasks are intended to provide a lightweight and flexible solution to getting your assemblies versioned consistently in a team build.

  • GetBuildNumber – This can be used in two ways. It will base the build part on either the number of days that have elapsed since a given date or on a date format, eg.

<GetBuildNumber BuildType=”$(BuildType)” BuildNumber=”$(BuildNumber)” Format=”Elapsed” StartDate=”1 Jan 2007” ZeroPadding=”4″ Major=”2″ Minor=”1″>
        <Output TaskParameter=”AssemblyFileVersion” PropertyName=”ElapsedBuildNo” />
</GetBuildNumber>
<GetBuildNumber BuildType=”$(BuildType)” BuildNumber=”$(BuildNumber)” Format=”Date” DateFormat=”MMdd” Major=”2″ Minor=”1″>
        <Output TaskParameter=”AssemblyFileVersion” PropertyName=”DateBuildNo” />
</GetBuildNumber>

  • SetBuildNumber – this is used to set the build number in a given collection of files.

Team Build Sample

<?xml version=”1.0″ encoding=”utf-8″?>
<Project DefaultTargets=”DesktopBuild” xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″>
    <Import Project=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v8.0\TeamBuild\Microsoft.TeamFoundation.Build.targets”/>
    <Import Project=”FreeToDev.MSBuildTasks.tasks”/>
    <ItemGroup>
        <FilesToVersion Include=”C:\POC\AssemblyInfo.cs”/>
    </ItemGroup>
    <!– Team Build Code omitted for brevity –>
    <Target Name=”BuildNumberOverrideTarget” Condition=”‘$(IsDesktopBuild)’!=’true'”>
        <ShowBuildStep TeamFoundationServerUrl=”$(TeamFoundationServerUrl)” BuildURI=”$(BuildUri)” BuildStep=”TARGET OVERRIDE: BuildNumberOverrideTarget”/>
        <GetBuildNumber BuildType=”$(BuildType)” BuildNumber=”$(BuildNumber)” Format=”Elapsed” StartDate=”1 Jan 2007″ ZeroPadding=”4″ Major=”2″ Minor=”1″>
            <Output TaskParameter=”AssemblyFileVersion” PropertyName=”ElapsedBuildNo”/>
        </GetBuildNumber>
        <OnError ExecuteTargets=”OnError”/>
    </Target>
    <Target Name=”AfterGet” Condition=”‘$(IsDesktopBuild)’!=’true'”>
        <ShowBuildStep TeamFoundationServerUrl=”$(TeamFoundationServerUrl)” BuildURI=”$(BuildUri)” BuildStep=”TARGET OVERRIDE: AfterGet”/>
        <SetBuildNumber Files=”%(FilesToVersion.Identity)” BuildNumber=”$(ElapsedBuildNo)” Encoding=”UTF8″/>
        <!– add any addition processing required –>
        <OnError ExecuteTargets=”OnError”/>
    </Target>
    <!– Team Build Code omitted for brevity –>
</Project>

Download the latest version with samples here….

Advertisements

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