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

First, Thanks for Richard’s blog http://richardsbraindump.blogspot.com/2007/07/versioning-builds-with-tfs-and-msbuild.html

 

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" ?> 
<Version>
  <Major>1</Major> 
  <Minor>0</Minor> 
  <Build>0</Build> 
  <Revision>0</Revision> 
</Version>

 

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

                 Microsoft.Build.Framework

                 Microsoft.Build.Tasks

                 Microsoft.Build.Utilities.v3.5

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
    {
        [Output]
        public int Major { get; set; }

        [Output]
        public int Minor { get; set; }

        [Output]
        public int Build { get; set; }

        [Output]
        public int Revision { get; set; }

        [Output]
        public string VersionTaskError { get; set; }
        public override bool Execute()
        {
            try
            {
                //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();
                versionXml.Save(@"C:\Version.XML");
            }
            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 http://msbuildtasks.tigris.org/ 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="http://schemas.microsoft.com/developer/msbuild/2003" 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">
    <VersionTask>
      <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>
    </VersionTask>
    <PropertyGroup>
      <BuildNumber>$(Major).$(Minor).$(Build).$(Revision)</BuildNumber>
    </PropertyGroup>
    <Message  Text="BuildNumber=$(BuildNumber)"></Message>
    <Message  Text="VersionTaskError=$(VersionTaskError)"></Message>
  </Target>
  <Target Name="AfterGet">
    <ItemGroup>
      <AssemblyInfoFiles Include="$(SolutionRoot)\**\assemblyinfo.cs" />
    </ItemGroup>
    <Attrib Files="@(AssemblyInfoFiles)" Normal="true" />
    <FileUpdate Files="@(AssemblyInfoFiles)"
                Regex="AssemblyVersion\(&quot;.*&quot;\)\]"
                ReplacementText="AssemblyVersion(&quot;$(Major).$(Minor).$(Build).$(Revision)&quot;)]" />
    <FileUpdate Files="@(AssemblyInfoFiles)"
                Regex="AssemblyFileVersion\(&quot;.*&quot;\)\]"
                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>

 
  </Target>
</Project>

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

导航