Tuesday, March 6, 2007

Setting the Build Number in AssemblyInfo.cs from Subversion using MSBuild

VS05 introduces msbuild, an xsd for the xml in the VS05 project files which allows developers to write their own tasks or use standard tasks to control the building of .net projects.

The following msbuild xsd xml was added to a c# project file (*.csproj) to produce a build revision number in its AssemblyInfo attributes based on the current subversion version number. The AssemblyInfo.cs file is auto-generated and should not be placed under source code control.

<import project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets">
<propertygroup>
<major>1</major>
<minor>0</minor>
<build>0</build>
<revision>0</revision>
</propertygroup>
<target name="Version">
<svnversion localpath="$(MSBuildProjectDirectory)">
<output taskparameter="Revision" propertyname="Revision">
</svnversion>
<message text="Revision: $(Revision)">
<message text="Project File Name = $(MSBuildProjectFileName)">
<message text="Project Extension = $(MSBuildProjectExtension)">
<message text="Project Directory = $(MSBuildProjectDirectory)">
<message text="Bin Path = $(MSBuildBinPath)">
<message text="Extensions Path = $(MSBuildExtensionsPath)">
<message text="Version: $(Major).$(Minor).$(Build).$(Revision)">
<assemblyinfo codelanguage="CS" outputfile="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs" assemblytitle="Your Assembly Title" assemblydescription="" assemblyconfiguration="" assemblycompany="Your Company" assemblyproduct="Your Product" assemblycopyright="Copyright © Your Company 2007" assemblytrademark="" assemblyculture="" comvisible="false" guid="Your Guid" assemblyversion="$(Major).$(Minor).$(Build).$(Revision)" assemblyfileversion="$(Major).$(Minor).$(Build).$(Revision)">
<createproperty value="$(Major).$(Minor).$(Build).$(Revision)">
<output taskparameter="Value" propertyname="ApplicationVersion">
</createproperty>
<createproperty value="$(Revision)">
<output taskparameter="Value" propertyname="ApplicationRevision">
</createproperty>
<message text="ApplicationVersion: $(ApplicationVersion)">
</target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.-->
<target name="BeforeBuild">
<calltarget targets="Version">
</target>
<target name="AfterBuild">
<message text="ApplicationVersion: $(ApplicationVersion)">
</target>

The xml makes use of the following standard tasks that ship with msbuild:-

  1. Message
  2. CreateProperty
  3. CallTarget

Further information on these tasks can be found in the MSBuild Task Reference.

In addition, msbuild community tasks, an open source set of tasks for msbuild, is required, specifically the following tasks:-

  1. SvnVersion
  2. AssemblyInfo

The first line of code (the Import item) is used to include these tasks. The msbuild community tasks, together with their documentation, can be installed with the MSBuild.Community.Tasks.msi installer.

In order for the SvnVersion task to work the SubVersion command line tools must be installed.

The AfterBuild Target calls the Message task to output the ApplicationVersion at the end of the build. In order to see this message you must increase the verbosity of msbuild. In VS05 go to tools/options/projects and solutions/build and run and change the msbuild verbosity to normal to see the application version message at the end of the build output.

Add a new About Box class (included in the VS05 installed templates) to your project and instantiate an object of this type and call its ShowDialog() method in the click event handler for the help menu about item. The assembly build revision number shall be displayed by default in the dialog.

1 comment:

Anonymous said...

Thanks for your article. Exactly what I was looking for.
In order to get this snippet to work one has to make snippet XML conform (close tags, CamelCase, etc.) - but after doing this it worked like a charm.

thanks,
Tom