How to Keep build number / assembly version the same and auto-increment

First, Thanks for Richard’s blog


A simple way to keep the build number manually is to store last build number some where. When start a build, read the file and update it.

1 create a version file to store last build version. (C:\Version.Xml on Build Machine)

<?xml version="1.0" encoding="utf-8" ?> 


2 Create a Custom Build Task to get the Version from the file.

2.1 Create a Assembly Project TFSBuildTask

2.2 Add following assembly to project Reference




2.3 Add class VersionTask with following sample code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using System.Xml.Linq;
using Microsoft.Build.Framework;

namespace TFSBuildTask
    public class VersionTask : Task
        public int Major { get; set; }

        public int Minor { get; set; }

        public int Build { get; set; }

        public int Revision { get; set; }

        public string VersionTaskError { get; set; }
        public override bool Execute()
                //Get version from file and update it
                var versionXml = XElement.Load(@"C:\Version.XML");
                Major = int.Parse(versionXml.Element("Major").Value);
                Minor = int.Parse(versionXml.Element("Minor").Value);
                Build = int.Parse(versionXml.Element("Build").Value);
                Revision = int.Parse(versionXml.Element("Revision").Value)+1;
                versionXml.Element("Revision").Value = Revision.ToString();
            catch (Exception ex)
                Major = 1;
                Minor = 0;
                Build = 0;
                Revision = 0;
                VersionTaskError = ex.Message;
            return true;


2.4 Build the project and copy the output to C:\TFSBuildTask.dll on Build Machine.

3 Install MSBuild.Community.Tasks.msi from on Build Machine

4 Edit TFSBuild.Proj like following sample script

<?xml version="1.0" encoding="utf-8"?>
<!-- DO NOT EDIT the project element - the ToolsVersion specified here does not prevent the solutions 
     and projects in the SolutionToBuild item group from targeting other versions of the .NET framework. 
<Project DefaultTargets="DesktopBuild" xmlns="" ToolsVersion="3.5">

  <!-- Do not edit this -->
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  <UsingTask AssemblyFile="C:\TFSBuildTask.dll" TaskName="TFSBuildTask.VersionTask"/>
  <Target Name="BuildNumberOverrideTarget">
      <Output TaskParameter="Major" PropertyName="Major"></Output>
      <Output TaskParameter="Minor" PropertyName="Minor"></Output>
      <Output TaskParameter="Build" PropertyName="Build"></Output>
      <Output TaskParameter="Revision" PropertyName="Revision"></Output>
      <Output TaskParameter="VersionTaskError" PropertyName="VersionTaskError"></Output>
    <Message  Text="BuildNumber=$(BuildNumber)"></Message>
    <Message  Text="VersionTaskError=$(VersionTaskError)"></Message>
  <Target Name="AfterGet">
      <AssemblyInfoFiles Include="$(SolutionRoot)\**\assemblyinfo.cs" />
    <Attrib Files="@(AssemblyInfoFiles)" Normal="true" />
    <FileUpdate Files="@(AssemblyInfoFiles)"
                ReplacementText="AssemblyVersion(&quot;$(Major).$(Minor).$(Build).$(Revision)&quot;)]" />
    <FileUpdate Files="@(AssemblyInfoFiles)"
                ReplacementText="AssemblyFileVersion(&quot;$(Major).$(Minor).$(Build).$(Revision)&quot;)]" />

    <Exec WorkingDirectory="$(SolutionRoot)" Command="&quot;C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe&quot; checkout @(AssemblyInfoFiles,' ')"></Exec>
    <Exec WorkingDirectory="$(SolutionRoot)" Command="&quot;C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe&quot; checkin /noprompt"></Exec>


posted on 2009-12-08 13:44  Ruiz  阅读(1587)  评论(0编辑  收藏  举报