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 (62)

  1. Phil Wright says:

    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

  2. Jason Kemp says:

    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?

  3. msbuild says:

    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

  4. msbuild says:

    Phil,

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

    Neil

  5. Steinar Herland says:

    Nice work. I’m considering changing the version numbering we use in our products, and really like the Visual Studio-style build numbers. (1.0.yyMMdd.revision) However, when I looked into the System.Version class it became obvious that this style will only work for about a year(!)

    The problem is that System.Build, .Major, .Minor and .Revision are System.Int32 :(

    January 1. 2007 => "1.0.70101.0"

    70101 > 65534 => The specified version ‘1.0.70101.0’ is invalid.

    (Interestingly the error only occurs if set using the AssemblyVersion attribute. When removing the AssemblyVersion, and only using AssemblyFileVersion, VS is not giving compilation errors, but AssemblyFileVersion("1.0.70101.0")] gives the assembly File Version 1.0.4565.0.0 and Product Version 1.0. 70101.0)

    Do you know which build-number-style that will be used on the next version of VS? – Assuming it won’t be released next year. :)

    Steinar

  6. Bob Builder says:

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

  7. msbuild says:

    Steinar, I’m not sure what we’re planning to do about that problem. I guess it’s a year 2010 bug :)

    Neil

  8. msbuild says:

    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

  9. <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

  10. Steinar Herland says:

    It’s not a 2010, its a 2007 bug :)

  11. Valkyrie-MT says:

    Wait a minute… I don’t think there is a Year 2007 problem. Int32 has a max value of 2,147,483,647. I think you are confusing this with UInt16. So turn that frown upside down! :) Because it should work for a million years! We may indeed have a Million year problem though…

    -Valkyrie-MT

    Steinar said:

    The problem is that System.Build, .Major, .Minor and .Revision are System.Int32 :(

    January 1. 2007 => "1.0.70101.0"

    70101 > 65534 => The specified version ‘1.0.70101.0’ is invalid.

  12. Tobias says:

    The max build number is actually System.UInt16.MaxValue – 1 (65,534) and not Int32 (2,147,483,647). This is true for VS2005.

    [assembly: AssemblyVersion("1.0.65534.0")]

    Works

    [assembly: AssemblyVersion("1.0.65535.0")]

    Produces error: Error emitting ‘System.Reflection.AssemblyVersionAttribute’ attribute — ‘The version specified ‘1.0.65535.0’ is invalid’

    An alternative versioning is [major.minor.yymm.ddrrr] as long as you produce no more than 1000 builds on any given day.

    Tobias.

  13. Tobias says:

    Actually the alt versioning system won’t work because of leading zero issue.

    Tobias.

  14. Leo says:

    Hi, How can I use this on a Web Project ?

    Thanks

  15. Jon says:

    Hi

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

    Jon

  16. Sean Perkin says:

    How about changing the version number format to:

    1.0.0.0

    major.minor.patch.build_increment

  17. sacrosancttayyar@gmail.com says:

    hi everybody,

    i am new at VS (studying c#) and i eonder that how can i open and COPiLE VS2005 projects withs VS 2003 (on a computer that have not NETFRAMEWORK 2.X  only have 1.1)

    maybe some assembly works but i really woder this..

    in my school computers dont have  netframework2.x only 1.1. and VS2003 so my projects (created by VS2005) cant be opened in my schools computers..????

    :(

    maybe you say :MSBuild Toolkit bla bla ..

    thanks for now.

    [sorry for my bad english– you are not babysitter so you have not to answer this topic :) ]

  18. RajM says:

    How to implement versioning for native VC++ projects that are part of a sln.

  19. bobw says:

    I echo Leo’s request: will this work on web projects?

  20. Jocke says:

    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!

  21. Ian says:

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

    thanks

  22. matt law says:

    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 :(

  23. Beefeater says:

    I can’t seem to find the source code on the Microsoft Community License page.   Could you please post the link?

  24. Corina says:

    Hello! I want to update version number in AssemblyInfo.cs but I don’t want this format: 1.0.yyMMdd.revision. I tried using <Import Project="$(MSBuildExtensionsPath)MicrosoftAssemblyInfoTaskMicrosoft.VersionNumber.targets"/> put in the end of the .csproj file and then, as a test, I tried using command line to execute:

    MSBuild.exe <testProject>Test.csproj /t:Rebuild /p:Assembl

    yRevisionType="NoIncrement" /p:AssemblyFileRevisionType="NoIncrement" /p:AssemblyVersion="0.1.38.*" /p:AssemblyFileVersion="0.1.38.*" but in the AssemblyInfo file I got [assembly: AssemblyVersion("1.0.061025.05")] which is not what I want. Can you help me, please?

  25. Woundering says:

    Why do I need this util? Why cant I specify the assembly version in assembly.vb? Currently my build environment is automated to do that, and I havn’t found any drawbacks, yet.

  26. I’m very proud of Visual Studio Team System and what Microsoft have done here – I’ve blogged about it

  27. Jon says:

    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

  28. Jon says:

    Ok, once I updated these properties in Microsoft.VersionNumber.targets, it works perfect.

    Thanks.

  29. Michael S. Combs says:

    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.

  30. Joey says:

    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.

  31. Steve says:

    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.

  32. Chris says:

    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?

  33. Gavin says:

    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>

  34. TLoomos says:

    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.

  35. ckw says:

    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?

  36. ckw says:

    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?

  37. frosted says:

    hehe, quite funny :)

    I’ll be waiting for a new official update, I can work around with this 2007 problem for now.

  38. sur says:

    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??

  39. Danny says:

    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

  40. I’m going to stop writing new tasks for a couple of entries while I take some time to clean things up.

  41. lextm says:

    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

  42. Dalo says:

    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

  43. Michael says:

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

  44. mjdgddy says:

    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.

  45. Amit Pabari says:

    Actully i need the same solution for .NET 2003 and i dont know how can i do that..???

    Can any one help me…….??????

  46. There were over 80 responses to my recent post asking for feedback on where MSBuild should be heading

  47. enough_already... says:

    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???

  48. Roberto says:

    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.

  49. ychen says:

    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.

  50. jiro says:

    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

  51. Richard J Foster says:

    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?

  52. Jandost says:

    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,

  53. Jandost says:

    Hi,

    I am facing a problem using AssemblyTaskInfo in my Subversion source Control. After adding IMPORTS line to my project file, I am continuously getting UNAUTHORIZED ACCESS (ACCESS DENIED to SVNtxt-basesAssembly***.cs file)

    Have you seen this error before? Any workaround?

  54. FreeToDev says:

    A lightweight alternative to versioning: http://freetodev.spaces.live.com/blog/cns!EC3C8F2028D842D5!547.entry

  55. dB. says:

    Here’s a more generic alternative that applies to C#, C++, etc. with MSBuild Community Tasks – http://code.dblock.org/ShowPost.aspx?id=19

  56. Alucardz says:

    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.

  57. Ravi Yellasiri says:

    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.

  58. kiquenet says:

    any updates in 2012 ? where is full source code  AssemblyInfoTask, ?