Label Cloud

Sunday, October 22, 2006

MSBuild Community Tasks

I've tried to convert some of the projects from using NANT to MSBuild process, however, the lack of available build tasks was always preventing my from converting any of the advanced scripts.

Now I stumbled upon MSBuild Community Tasks. It covers a lot of the tasks supported by NAnt or NAnt contrib projects. There are still a lot of things to be desired, however, it provides some of the key tasks to make project creation easy. Together with tasks provided by MSBuild, the functionality is very comparable to NANT combined with NANT Contrib. Very, very impressive.

Here's an important yet easy example: Autoincrement version number of the build:

  • Open the .csproj file in notepad or your other favorite text editor
  • Add import directive to the one that already exists in the .csproj file
  • <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
  • Uncomment BeforeBuld and AfterBuild targets located at the end of the project
  • Add Version task to the BeforeBuild:
    <Version VersionFile="version.txt" RevisionType="Increment"> <Output TaskParameter="Major" PropertyName="Major" /> <Output TaskParameter="Minor" PropertyName="Minor" /> <Output TaskParameter="Build" PropertyName="Build" /> <Output TaskParameter="Revision" PropertyName="Revision" /> </Version>
  • Add AssemblyInfo task to the BeforeBuild target
    <AssemblyInfo CodeLanguage="CS" OutputFile="AssemblyVersion.cs" AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)" AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
  • Remove AssemblyVersion and AssemblyFileVersion from AssemblyInfo.cs file
  • Add AssemblyVersion.cs file to the project

Now compiling the project will adjust version number in version.txt, and use the new version in generating AssemblyVersion.cs

Easy and Extremely Useful

Some Related Links:


Share/Save/Bookmark

1 comment:

VST said...

Great article and to the point. I'm trying to do this but getting 0.0.0.0 and no version.txt is generated.

Any tips?

-Ali
http://www.VisionsoftOnline.net