Insert SVN version and Build number in your C# AssemblyInfo file
February 3, 2009 — Florent ClairambaultSoftware version number is quite important. It helps you track what versions have your users when they report something. And when it’s linked to an SVN version number, it’s even better.
Well, with MSBuild Community Task, you can easily automatically generate smart version numbers, you have to:
- Download MSBuildCommunityTasks
- Make sure your “svn.exe” binary is in C:\program files\subversion\bin
- Add this at the end of your .csproject file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <!-- Import of the MSBuildCommunityTask targets --> <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" /> <!-- to manage version number --> <Target Name="Version"> <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> </Target> <!-- to generate our personnal version info --> <Target Name="AssemblyInfo"> <SvnVersion LocalPath="$(MSBuildProjectDirectory)"> <Output TaskParameter="Revision" PropertyName="Build" /> </SvnVersion> <AssemblyInfo CodeLanguage="CS" OutputFile="Properties\AssemblyInfo.cs" AssemblyTitle="Title" AssemblyDescription="Description" AssemblyCompany="Company" AssemblyProduct="product" AssemblyCopyright="Copyright © 2009" ComVisible="false" Guid="88812638-9547-4480-9bf4-4fe25103b35c" AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)" AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" Condition="$(Revision) != '0' "/> </Target> <!-- We launch these two targets --> <Target Name="BeforeBuild"> <CallTarget Targets="Version" /> <CallTarget Targets="AssemblyInfo" /> </Target> <Target Name="AfterBuild"> </Target> |
You should only have a “</Project>” field left…
Then, you just have to open your project and build your project, it will fail once (missing version.txt file) and then work forever. This will generate your Assembly & AssemblyFile versions like this: Major.Minor.SvnVersion.BuildVersion
In your C# code, to get your version, you just have to add something like that:
1 2 3 4 5 | public static String Version { get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } |
GD Star Rating
loading...
loading...
March 5, 2009 at 11:31 am
Hi, thanks for this article,
but does there is a way to compile the application even if SVN is not installed ?
Regards
loading...
March 8, 2009 at 10:33 am
The easiest way I could find is to add a condition on the SvnVersion tag :
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" Condition="Exists('$(ProgramFiles)\subversion\bin\svn.exe')">Your build number will be incremented and your SVN revision number won’t be fetched.
loading...
March 9, 2009 at 11:49 pm
Thanks, just what I was looking for!
I made a little change on configuration because my SVN binaries are located elsewhere.
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(ProgramFiles)\CollabNet Subversion Client">loading...
March 19, 2009 at 2:50 am
Interesting. But what do you do if the svn revision number goes over 65535 (i.e. the maximum for any of the 4 version number components) ?
loading...
March 19, 2009 at 9:38 am
Well, I really don’t know. I have never worked in a so big SVN Repository.
I think it might just generate a number with the $svnRev%65536.
loading...
April 27, 2009 at 1:19 pm
[...] Insert SVN version and Build number in your C# AssemblyInfo file [...]
June 15, 2009 at 12:25 am
Most of the people of my blog are coming for this post. I should really find a better solution. Something between the solution of “theKindOfMe” and mine.
It would be great to extract build number and svn revision number and just update the AssemblyVersion file without having to specify each property in the “csproj” file.
I should work on a clean solution to that problem. But that really isn’t fun to do because MSBuild XML language is not really comfortable.
loading...
February 8, 2010 at 7:12 pm
where can I find MSBuild.Community.Tasks.Targets?
loading...
February 14, 2010 at 11:20 pm
You have to download MsBuild Community Tasks here : http://msbuildtasks.tigris.org/
loading...