Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralCan't figure out what I'm doing wrong
redbergy
17:13 1 Dec '09  
I have followed the steps but I get this error:

Error 2 The "BuildTasks.IncrementBuildNumber" task could not be loaded from the assembly AutoIncrementBuildTask, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c77dd5dca239f8e6. Could not load file or assembly 'AutoIncrementBuildTask, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c77dd5dca239f8e6' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, and that the assembly and all its dependencies are available. Z:\adam\Visual Studio 2005\... 593 2 ....


I have put the DLL file in my project directory (same as my project file) as well as in the GAC.

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

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


Any ideas?
GeneralRe: Can't figure out what I'm doing wrong
redbergy
7:10 2 Dec '09  
My problem was that I had just recently moved my projects on to a network share. This was causing the trust issues with the DLL when it was compiled. I used the following command to fully trust my network share in Visual Studio (must be done in the VS command prompt):

caspol -addgroup 1.2 -url file:///Z:/* FullTrust


After running this command everything worked properly.
GeneralRevision number to be retained as specified
vsharma
0:37 12 May '09  
I am trying to use the -p option and passing 1.2.3.4 but the Assembly's version comes out as 1.2.3.5415. The last part (4 digits) is tied to the number of seconds elapased since midnight. I dont want this behavior and want "1.2.3.4" as the Assembly version.

I also tried...
BuildOptions="-p 1.2.3.4 -r Fixed

with the same result...

Isn't it possible to retain whatever is being passed?
GeneralThanks a lot
zzattack
13:48 18 Aug '08  
I've been using this for over a year now, and this article is still awesome and perfectly fits my needs. I noticed I still hadn't thanked you for this, but I really felt that I should. Keep it up!


Last Updated 27 Jun 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010