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
AutoIncrement
searches its input for a .NET AssemblyVersion
attribute and calculates a new version number using one of several algorithms.
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.
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.
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.
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
-
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.
-
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>
-
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.
-
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
.