DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

MSBuild.Community.Tasks To Format Assembly Version As Current Date With Build Number So Far Today

  • submit to reddit
        This will format your assembly version as <YEAR>.<MONTH>.<DAY>.<NumberOfBuildsToday>.

To do this, we grab the Year, Month, and Day from a Time task (to correspond with Major, Minor, Build).  Then we use a Version task to automatically create a Build number and Revision number.  You would think that we could get by specifying the Build number as created by the Time task, but as it turns out, the Version task doesn't seem to check the Build number for the BuildIncrement unless the Version task is actually generating the Build number as well.

  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
  <Target Name="BeforeBuild">
      <Output TaskParameter="Year" PropertyName="TMajor" />
      <Output TaskParameter="Month" PropertyName="TMinor" />
      <Output TaskParameter="Day" PropertyName="TBuild" />
    <!-- Need version to auto-generate its Build property so that the revision will automatically update between days -->
    <Version Major="$(TMajor)" Minor="$(TMinor)" BuildType="Automatic" RevisionType="BuildIncrement" VersionFile="Properties\version.txt">
      <Output TaskParameter="Revision" PropertyName="VRevision" />
    <AssemblyInfo CodeLanguage="CS" OutputFile="Properties\VersionInfo.cs" AssemblyVersion="$(TMajor).$(TMinor).$(TBuild).$(VRevision)" AssemblyFileVersion="$(TMajor).$(TMinor).$(TBuild).$(VRevision)" AssemblyInformationalVersion="$(TMajor).$(TMinor).$(TBuild) alpha" />

This will create a file in Properties\version.txt that the Version task will use to keep track of the number of builds in a given day, and it will create Properties\VersionInfo.cs that will contain your assembly version declarations -- so you'll need to delete those lines from AssemblyInfo.cs.