Setting a version for an assembly with MSBuild

When you have multiple customers and common libraries, it is very important that you can track down the assemblies. You want to be able to tell at all time what the assemblies exactly contain.
Setting versions to an assembly helps a lot with that.
The version of an assembly is stored in the assembly.info file. You can find this file under the Properties folder in your project.
You can also set some meta data of the assembly in this file (like company name, copyright, etc).
The assembly version is stored in the attribute AssemblyVersion

[assembly: AssemblyVersion("1.0.0.0")]

For me, the  ideal situation of the version number should be as follows:

{major version}.{minor version}.{build number}.{revision}

The major and minor versions are defined by the user.
The build number is defined by the build server, a number which is incremented every time a build has run.
The revision is defined by source control;  this is the revision number of the latest commit in this build.

This makes it possible to determine when this build is created and what the source of the assemblies is.

I created a msbuild task for this, which sets the version number. I have used MsBuild Community Tasks for this. This has an AssemblyInfo Task which makes it very easy to set an assembly version:


<Import Project="tasks\MSBuild.Community.Tasks.Targets"/>

<PropertyGroup>
 <AssemblyInfoFile>.\Properties\AssemblyInfo.cs</AssemblyInfoFile>
 <AssemblyMajorVersion>1</AssemblyMajorVersion>
 <AssemblyMinorVersion>0</AssemblyMinorVersion>
 <AssemblyVersion>$(AssemblyMajorVersion).$(AssemblyMinorVersion).$(BUILD_NUMBER).$(SVN_REVISION)</AssemblyVersion>
<PropertyGroup>

<Target Name="SetAssemblyVersion">
 <AssemblyInfo CodeLanguage="CS"
 OutputFile="$(AssemblyInfoFile)"
 AssemblyCompany="Your company here"
 AssemblyProduct="Your product here"
 AssemblyCopyright="Your copyright here"
 ComVisible="false"
 AssemblyVersion="$(AssemblyVersion)"
 AssemblyFileVersion="(AssemblyVersion)" />
</Target>

Note that the BUILD_NUMBER and SVN_REVISION variables are environment variables set by the build server (Hudson in this case).

When you want to set the same version number for multiple assemblies, just simply add the assmblies as a link to the one assembly which will have the correct version number.

The following two tabs change content below.
I'm a software developer from Utrecht. Interested in DDD, continuous delivery, new technologies & frameworks.

Latest posts by Vincent Keizer (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *