Click here to Skip to main content
15,868,016 members
Articles / Mobile Apps
Article

Auto Increment Microsoft Build Task

Rate me:
Please Sign up or sign in to vote.
3.08/5 (5 votes)
27 Jun 2007CPOL2 min read 37.2K   329   24   5
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...

    XML
    <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:

    XML
    <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.

    XML
    <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:

VBScript
[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

XML
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer myspace
United States United States
Its a passion, why else would we be doing this? Everyday it feels like gold fever, wouldn't you agree? With the boom of the internet and an ever decreasing world, programming today feels a lot like the wild wild west of the past. Its so wide open, filled with tremendous opportunities. Its more than a passion, its a forming of the ideas that later generations will use.

With over ten years experience, I've earned the right to say that. The majority of my career has focused on enterprise application development. I earned my BS in CS from UCI, hi everybody. My fondest memories as a child was working with the 8mhz IBM AT my daddy brought home for us. Yeah, I'm a geek, and proud of it. We're at the forefront of something huge, enjoy it.

Comments and Discussions

 
QuestionCan I use the auto increment build task for fileVersion or productVersion ? Pin
bigbits5-Mar-13 5:21
bigbits5-Mar-13 5:21 
GeneralCan't figure out what I'm doing wrong Pin
redbergy1-Dec-09 16:13
redbergy1-Dec-09 16:13 
GeneralRe: Can't figure out what I'm doing wrong Pin
redbergy2-Dec-09 6:10
redbergy2-Dec-09 6:10 
GeneralRevision number to be retained as specified Pin
vsharma11-May-09 23:37
vsharma11-May-09 23:37 
GeneralThanks a lot Pin
zzattack18-Aug-08 12:48
zzattack18-Aug-08 12:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.