Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Auto Increment Microsoft Build Task

0.00/5 (No votes)
27 Jun 2007 1  
Auto Increment Assembly Version in a Microsoft Build Task

Introduction

This article describes how to auto increment the build/revision numbers in your assembly as a build task.

Background

I originally got the source code from an article on this site by Dave Gallant. He did an excellent job of implementing a solution that worked I originally read from this blog. Dave's original code worked as stated, but I wanted more. Specifically, I used to use a project called UpdateVersion some years ago that did the same thing but used the command line. I wanted to combine the power of options included with UpdateVersion into a MSBuild Task. The results of that are included here.

Overview

  1. AutoIncrement searches its input for a .NET AssemblyVersion attribute and calculates a new version number using one of several algorithms. 
  2. AutoIncrement calculates and outputs new version numbers using one of several algorithms. You can use it with Visual Studio .NET to update your AssemblyInfo.* file on every build.
  3. AutoIncrement will calculate a new revision number only or it can calculate a new build number and a new revision number at the same time.
  4. AutoIncrement can calculate the build number by incrementing the existing build number or it can calculate the build number based on the project start date. 
  5. AutoIncrement can calculate the revision number by incrementing the existing revision number or it can calculate the revision number based on the number of seconds since midnight.

Using the Code

  1. Install the AutoIncrementBuildTask.dll into your GAC. That way you don't have to copy it into the same folder that your project file is in.

  2. In your project file (vbproj or csproj), add this line...

    <UsingTask TaskName="BuildTasks.IncrementBuildNumber" 
      AssemblyName="AutoIncrementBuildTask, Version=1.0.0.0, Culture=neutral,
      PublicKeyToken=c77dd5dca239f8e6" />  

    ... between the <Project> tag and the first <PropertyGroup> tag. For example, the first three lines of your project should look like this:

    <Project DefaultTargets="Build" 
    	xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="BuildTasks.IncrementBuildNumber" 
      AssemblyName="AutoIncrementBuildTask, Version=1.0.0.0, Culture=neutral,
      PublicKeyToken=c77dd5dca239f8e6" />
        <PropertyGroup>
  3. Now find the <Target Name="BeforeBuild"></Target> tags and add this between them.

    <IncrementBuildNumber 
      AssemblyFileLocation="$(MSBuildProjectDirectory)\AssemblyInfo.cs"
      BuildOptions="-s 2006-05-31 -b MonthDay -r Fixed"/> 

    IMPORTANT! - Make sure the path to your assembly file is CORRECT!

    $(MSBuildProjectDirectory) means the folder your project file is in, so it will be relative to that. In my case, it's in the root folder, and since it's a C# app, it's the AssemblyInfo.cs file.

  4. Save the project and load it. The major and minor numbers will stay the same, and the Build and Revision will be updated, depending on your options above.

Points of Interest

I default on fixing the revision (-r Fixed) and increment the build (-b Increment). In my projects, a lot of assemblies reference each other and are copied into a common build folder.
A common build folder plays nicely with Visual Studio, but you have to add the path to the registry like this:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\LIB_NET1.1]
@="C:\\usr\\Build\\Lib\\NET-1.1" 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727\
					AssemblyFoldersEx\LIB_NET2.0]
@="C:\\usr\\Build\\Lib\\NET-2.0" 

Here my default build directories are c:\usr\Build\Lib\Net-2.0 or Net-1.1 depending.

Examples

<IncrementBuildNumber  BuildOptions="-b Increment">
<IncrementBuildNumber  BuildOptions="-b Increment -r Increment">
<IncrementBuildNumber  BuildOptions="-b MonthDay -s 2002-11-23">
<IncrementBuildNumber  BuildOptions="-b MonthDay -s 11/23/2002">
<IncrementBuildNumber  BuildOptions="-p 1.2.3.4">
<IncrementBuildNumber  BuildOptions="-b BuildDay">
<IncrementBuildNumber  BuildOptions="-b Increment -r Fixed">
<IncrementBuildNumber  BuildOptions="-b BuildDay">  

History

  • 1.0.0.0 Initial release which includes code from UpdateVersion v1.2 and Dave Gallant's original implementation. Great job to both Dave and the authors of UpdateVersion.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here