Share via


How To: Update Assembly Version Numbers with MSBuild

UPDATE: For information on the "Y7K" or "2007" issue, see our new blog entry .  

One of our most frequently asked questions is: "How do update my assembly version numbers at build time?". Unfortunately our answer has been "you can't". Until today, that is.

I just posted a new task, AssemblyInfoTask, at GotDotNet. It provides complete support for a wide variety of version number styles, from a fixed number to numbers that change daily to numbers that increment on every build. It also supports setting other properties such as AssemblyTitle, AssemblyCopyright, etc.

Even better, we've posted the complete source code, unit tests (that run in Visual Studio Team System), and raw documentation for the task under the new Microsoft Community License. Try not to laugh too hard when you look at the code. Remember, I don't write code for a living, I'm one of those touchy-feely Program Manager people :)

If all you want to do is get Visual Studio-style build numbers, of the form 1.0.yyMMdd.revision (for example, 1.0.51111.02 for the third build today), then you simply add the following line to your project file after any existing <Imports>:

<

Import Project="$(MSBuildExtensionsPath)\Microsoft\AssemblyInfoTask\Microsoft.VersionNumber.targets"/>

If anyone has trouble getting it to work, has comments, questions, or suggestions, please send 'em our way to msbuild@microsoft.com or post to our forum.Enjoy!

[ Author: Neil Enns ]

Comments

  • Anonymous
    November 11, 2005
    This is just what I needed. I was just about to write a custom task myself to set the version number and thought I would do a search just in case this common requirement was already done. Thanks!

    P.S. I'll let you know if I find any bugs!

    Phil Wright
    Free VS2005 GUI Controls...
    http://www.componentfactory.com

  • Anonymous
    November 11, 2005
    Great! I was looking for something like this.

    I quickly glanced at the code and I have a quick question about something I noticed: why did you create your own Version class when there is System.Version built into the BCL? Is it 'cause yours does something it doesn't?

  • Anonymous
    November 12, 2005
    Good question, Jason. I must admit when I started with the task I didn't actually realize System.Version existed. At one point during the development I did try and switch to using the .NET Framework class.

    The difference is in the property types. The .NET Version class stores everything as ints, and my class uses strings. This is to support leading numbers when doing custom formatting. For example, it's customary in Visual Studio-land for our revisions to be "01", "02", etc., rather than being "1", "2" and so on. If this gets stored as an int the custom formatting is lost. You can see this at work in the UpdateVersionProperty() method.

    In hindsight I might be able to everything as ints in the Framework class and then do the formatting when I actually spit out the version number to the file. Perhaps in v1.1 :)

    Neil

  • Anonymous
    November 12, 2005
    Phil,

    You also have to let me know if it works :)

    Neil

  • Anonymous
    November 17, 2005
    The comment has been removed

  • Anonymous
    November 29, 2005
    If I have a .sln (converted to a msbuild-proj-xml file), how can I in one place set the version of every assembly?

  • Anonymous
    November 30, 2005
    Steinar, I'm not sure what we're planning to do about that problem. I guess it's a year 2010 bug :)

    Neil

  • Anonymous
    November 30, 2005
    Bob,

    Excellent question. You should be able to insert the necessary target call into your msbuild project XML file at the start of the build process. I don't have a converted file handy at the moment to mess with this (no VS2005 at home!). Can you shoot me mail at msbuild@microsoft.com to ask your question so I don't forget? That way I can look at it on Friday (tomorrow's swamped) and I'll get back to you in email as well as on the blog.

    Neil

  • Anonymous
    December 07, 2005
    <Import Project="$(MSBuildExtensionsPath)MicrosoftAssemblyInfoTaskMicrosoft.VersionNumber.targets"/>

    This is working perfectly fine with the Projects having AssemblyInfo.cs file.

    But how can I update the Version Numbers of the Assemblies generated by the <AspNetCompiler> task for a Website.
    All the assemblies generated here show Version as 0.0.0.0

    How can I generate the AssemblyInfo.cs file for the Website in advance.

    I am using Visual Web Developer and do not have VS 2005.

    Please suggest..

    Thanks in advance
    --Manish

  • Anonymous
    December 22, 2005
    It's not a 2010, its a 2007 bug :)

  • Anonymous
    January 11, 2006
    The comment has been removed

  • Anonymous
    January 30, 2006
    The comment has been removed

  • Anonymous
    February 01, 2006
    Actually the alt versioning system won't work because of leading zero issue.

    Tobias.

  • Anonymous
    February 13, 2006
    Hi, How can I use this on a Web Project ?

    Thanks

  • Anonymous
    February 13, 2006
    Hi

    I was wondering if we had resolved the 2007 bug yet or not? (other than changing the formating of build version number)

    Jon

  • Anonymous
    March 07, 2006
    How about changing the version number format to:

    1.0.0.0

    major.minor.patch.build_increment

  • Anonymous
    April 11, 2006
    The comment has been removed

  • Anonymous
    May 18, 2006
    How to implement versioning for native VC++ projects that are part of a sln.

  • Anonymous
    June 02, 2006
    I echo Leo's request: will this work on web projects?

  • Anonymous
    September 05, 2006
    Does not work on mobile projects since ".NET Compact Framework" is differs from ".NET Framework" and System.Reflection namespace does not contain AssemblyFileVersion class.

    Following error occurred:
    =======================
    Unable to update the AssemblyFileVersion for PropertiesAssemblyInfo.cs: No stub entry for AssemblyFileVersion was found in the AssemblyInfo file.
    =======================

    Please help!

  • Anonymous
    September 07, 2006
    I need to charge the custom buiild number so that it contains my local time (Copenhagen) , can i use this to do it

    thanks

  • Anonymous
    September 14, 2006
    Won't work full stop with a project under Subversion control...

    Unable to update the AssemblyFileVersion for _svntext-baseAssemblyInfo.cs.svn-base: No stub entry for AssemblyFileVersion was found in the AssemblyInfo file.

    Shame :(

  • Anonymous
    September 21, 2006
    I can't seem to find the source code on the Microsoft Community License page.   Could you please post the link?

  • Anonymous
    September 27, 2006
    PingBack from http://edgardorossetto.net/2006/05/06/como-incrementar-el-numero-de-version-de-los-assemblies-con-msbuild/

  • Anonymous
    October 24, 2006
    The comment has been removed

  • Anonymous
    November 19, 2006
    The comment has been removed

  • Anonymous
    December 05, 2006
    I'm very proud of Visual Studio Team System and what Microsoft have done here - I've blogged about it

  • Anonymous
    December 11, 2006
    I installed the downloaded file and created an C# project to test it. I found the following properties seems not work for me:    <AssemblyMajorVersion>2</AssemblyMajorVersion>    <AssemblyMinorVersion>0</AssemblyMinorVersion>    <AssemblyFileMajorVersion>2</AssemblyFileMajorVersion>    <AssemblyFileMinorVersion>0</AssemblyFileMinorVersion> I added these properties to the project file, after built it each time the Revision number getting increased and the major and minor numbers were keeping not changed, always "1.0". I am using VS 2005 + TFS. Can you point out what's wrong with my steps? Thanks, Jon

  • Anonymous
    December 11, 2006
    Ok, once I updated these properties in Microsoft.VersionNumber.targets, it works perfect. Thanks.

  • Anonymous
    December 23, 2006
    Hi, I'm trying to get this to work along side with my SVN plugin for vs2005, but it seems that this wants to change the values of an AssemblyInfo.cs.svn-base file. This file is located inside a hidden directory called .svn Maybe an update for this could ignore files inside hidden folders.

  • Anonymous
    January 01, 2007
    Just wondered if any one had come up with a workaround to the 2007 build number issue. I have just tried to build a project 1 Jan 2007 and it threw the error mentioned in the comments above.

  • Anonymous
    January 01, 2007
    Same 2007 problem here... all of our nightly builds have failed.  I hope we haven't followed Microsoft into a brick wall with their versioning standard.

  • Anonymous
    January 01, 2007
    I have also come into the office this morning to find that the last two overnight builds have failed with the "2007 problem". Has anyone made any progress with this problem?

  • Anonymous
    January 02, 2007
    Same '2007' problem with us as well.... Oops! We've changed our scripts to add the 4 'DateString' and 'MMdd' lines below. i.e. remove the YY part. It'll get us through the next few days.  <PropertyGroup>    <!-- Assembly version properties. Add others here -->    <AssemblyMajorVersion>2</AssemblyMajorVersion>    <AssemblyMinorVersion>1</AssemblyMinorVersion>    <AssemblyFileMajorVersion>2</AssemblyFileMajorVersion>    <AssemblyFileMinorVersion>1</AssemblyFileMinorVersion>  <AssemblyBuildNumberType>DateString</AssemblyBuildNumberType>    <AssemblyBuildNumberFormat>MMdd</AssemblyBuildNumberFormat>    <AssemblyFileBuildNumberType>DateString</AssemblyFileBuildNumberType>    <AssemblyFileBuildNumberFormat>MMdd</AssemblyFileBuildNumberFormat>    <!-- TF.exe -->    <TF>&quot;$(TeamBuildRefPath)..tf.exe&quot;</TF>    <!-- AssemblyInfo file spec -->    <AssemblyInfoSpec>AssemblyInfo.cs</AssemblyInfoSpec>  </PropertyGroup>

  • Anonymous
    January 02, 2007
    I implemented a new versioning scheme as described in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1058284&SiteID=1.  It keeps with the spirit of the version numbering scheme but it's far less convenient than yymmdd.  This implementation sets the build number to the 2 digit year followed by the 3 digit day of the year. This scheme will work until 2065 :) To implement this, follow these steps:

  1. Get the AssemblyInfoTask source from http://www.gotdotnet.com/codegallery/releases/viewuploads.aspx?id=93d23e13-c653-4815-9e79-16107919f93e
  2. Open AssemblyInfoTask.cs and make the following changes:    Go to the end of the file and add another entry to the IncrementMethod enum - I used DayOfYear = 3    Search for "Switch" in the file and add another case for the new increment method.  Copy the DateString case but change the newVersionNumber to be: string newVersionNumber = DateTime.Now.Year.ToString().Substring(2) + String.Format("{0:000}",DateTime.Now.DayOfYear);
  3. Compile the app (remember it needs to be strong named if you want it in the GAC)
  4. Install the assembly into the GAC
  5. Open <program files>MSBuildMicrosoftAssemblyInfoTaskMicrosoft.VersionNumber.targets in Notepad - look for the task import (usingTask) and change the PublicKeyToken to match the one from your new assembly.
  • Anonymous
    January 02, 2007
    This seems like the best solution (much better than resetting the year to 1 which is an option I have seen suggested elsewhere). Are the (strongly named) binaries going to be reposted on GotDotNet?

  • Anonymous
    January 02, 2007
    This seems like the best solution (much better than resetting the year to 1 which is an option I have seen suggested elsewhere). Are the (strongly named) binaries going to be reposted on GotDotNet?

  • Anonymous
    January 03, 2007
    hehe, quite funny :) I'll be waiting for a new official update, I can work around with this 2007 problem for now.

  • Anonymous
    January 03, 2007
    The comment has been removed

  • Anonymous
    February 25, 2007
    can someone give me suggestions. i'm facing the following problem aseemblyinfo.cs file of main project is getting updated but not for the depended projects .these depended projects are defined in the .sln file of main project. sud i call this target "update assemblyinfo "for all depended projects separately or is there any other method??

  • Anonymous
    March 27, 2007
    It seems that    <AssemblyMajorVersion>1</AssemblyMajorVersion>    <AssemblyMinorVersion>0</AssemblyMinorVersion> only work in the targets file, not in the project file. But this file is shared for all projects. How can I set these so they are specific to each project? Thanks Danny

  • Anonymous
    May 11, 2007
    I'm going to stop writing new tasks for a couple of entries while I take some time to clean things up.

  • Anonymous
    May 20, 2007
    Danny, You can copy the targets file to a local folder and import the local version instead of the one located in MSBuildExtensions. Then each solution or project use a corresponding local copy. ============== It seems that   <AssemblyMajorVersion>1</AssemblyMajorVersion>   <AssemblyMinorVersion>0</AssemblyMinorVersion> only work in the targets file, not in the project file. But this file is shared for all projects. How can I set these so they are specific to each project? Thanks Danny

  • Anonymous
    June 06, 2007
    Hi, I'm using AssemblyInfoTask to update the Assembly info with MSBuild <Import Project="$(MSBuildExtensionsPath)MicrosoftAssemblyInfoTaskMicrosoft.VersionNumber.targets"/> My problem is that when it updates <Assembly: AssemblyCompany("The Company á")> it removes the character á.  Any ideas on how I get AssemblyInfoTask to recognise accents etc...? Thanks

  • Anonymous
    August 12, 2007
    Seems that godotnet does not exist anymore, but i cannot find the Task on CodePlex either. Where to get it?

  • Anonymous
    August 12, 2007
    For those who can't find where to download it now like me, it's now aviable at http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=5C455590-332C-433B-A648-E368B9515580

  • Anonymous
    November 15, 2007
    I'm really surprised no one has suggested using a year indicator with a Julian day of year instead of the month/day.  This would save one digit and get us past this issue.

  • Anonymous
    November 21, 2007
    Actully i need the same solution for .NET 2003 and i dont know how can i do that..??? Can any one help me.......??????

  • Anonymous
    November 30, 2007
    There were over 80 responses to my recent post asking for feedback on where MSBuild should be heading

  • Anonymous
    December 08, 2007
    Isn't it time ~somebody~ came up with a decent way to control version numbers associated with the build process???????? I have run into several different companies all ~home-brewing~ their own version incrementing scheme... let's get it together people... how long have version numbers been around now???

  • Anonymous
    February 19, 2008
    When searching the URL http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=5C455590-332C-433B-A648-E368B9515580 , i received the message "The GotDotNet site has been shut down". I tried to search the new site, but was unable due to security restrictions at my enterprise. Please, could anyone post the new site for the URL above? Thanks.

  • Anonymous
    March 18, 2008
    Hi,    I'm facing a problem as described in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2989436&SiteID=1    Could anyone help me solve that problem? Thanks.

  • Anonymous
    March 18, 2008
    Ok. while YYDDD is convinient. i really dont want to download the source file and modify them. personal reasons and just want to see a clear yymmdd in there. instead i used major.minorYY.MMDD.Revisions since i manually input the minor revisions. might as well edit it yearly so i formated it in my project as minorYY sample 1.108.0319.05 major -1 minor -1 (with year 08) build - mmdd revision - auto increment

  • Anonymous
    May 22, 2008
    I'm also looking for the new URL (assuming this potentially useful task didn't go the same way as the GotDotNet site). Can anyone help?

  • Anonymous
    July 10, 2008
    This seems to be the new URL: http://www.codeplex.com/tfsbuildversionsync

  • Anonymous
    July 22, 2008
    Hi, AssemblyInfoTask is not longer in the link you provided on GotDotNet. I found it anyways by searching internet from other sources. It will be great if you can update the link for other people to find it easier. Regards,

  • Anonymous
    July 22, 2008
    The comment has been removed

  • Anonymous
    July 30, 2008
    A lightweight alternative to versioning: http://freetodev.spaces.live.com/blog/cns!EC3C8F2028D842D5!547.entry

  • Anonymous
    October 26, 2008
    Here's a more generic alternative that applies to C#, C++, etc. with MSBuild Community Tasks - http://code.dblock.org/ShowPost.aspx?id=19

  • Anonymous
    December 28, 2008
    This questions has been asked twice already, and still no answer: Is there any way to apply this mechanism to a Web Project where no 'AssemblyInfo' and no .csproj / .vbproj files exist. There's a custom task approach described here: http://weblogs.asp.net/bradleyb/archive/2005/12/02/432150.aspx But it applies to WebDeployment Projects, which means that something needs to be done to the originally built dlls (app_web_xxxxxx.dll etc) since WebDeployment builds simply adds an 'asp_merge -copyattr' call to the original build of the Web Project.

  • Anonymous
    April 28, 2010
    I had kind of similar issue. Here is the format I cooked Major.Minor.NNNNN.HHMM Where NNNNN is number of days from 1970 Jan 01 (Pseudo UNIX EPOC timestamp model) currently it is 14727 (on 04/28/2010) and to reach the max UInt16 (65534) we have 139 more years to go. HHMM => Current Hour and Min. with this model only the problem is, you will get the same version if you build within the same minute. Which is very unlikely to happen ;-) This is 'a' solution.

  • Anonymous
    August 16, 2012
    any updates in 2012 ? where is full source code  AssemblyInfoTask, ?