25

I am trying to automate the process for setting the Version for all DLL's, after spending some time I came to know the AssemblyInfo Task with which it can most likely be achieved.

So I went ahead and installed it, specifically version 1.0.51130.0.

After Installing, I manually added the Import Tag (by unloading the each project) of AssemblyInfoTask in .cspoj files (the solution has more than 35 proj files).

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

Next I modified the Microsoft.VersionNUmber.Target file which will be installed in path: C:\Program Files\MSBuild\Microsoft\AssemblyInfoTask, and I modified the following section:

<!-- Properties for controlling the Assembly Version -->
<PropertyGroup>
    <AssemblyMajorVersion>4</AssemblyMajorVersion>
    <AssemblyMinorVersion>0</AssemblyMinorVersion>
    <AssemblyBuildNumber></AssemblyBuildNumber>
    <AssemblyRevision></AssemblyRevision>
    <AssemblyBuildNumberType>DateString</AssemblyBuildNumberType>
    <AssemblyBuildNumberFormat>01MMdd</AssemblyBuildNumberFormat>
    <AssemblyRevisionType>AutoIncrement</AssemblyRevisionType>
    <AssemblyRevisionFormat>00</AssemblyRevisionFormat>
</PropertyGroup>

<!-- Properties for controlling the Assembly File Version -->  
<PropertyGroup>
    <AssemblyFileMajorVersion>4</AssemblyFileMajorVersion>
    <AssemblyFileMinorVersion>0</AssemblyFileMinorVersion>
    <AssemblyFileBuildNumber></AssemblyFileBuildNumber>
    <AssemblyFileRevision></AssemblyFileRevision>
    <AssemblyFileBuildNumberType>DateString</AssemblyFileBuildNumberType>
    <AssemblyFileBuildNumberFormat>01MMdd</AssemblyFileBuildNumberFormat>
    <AssemblyFileRevisionType>AutoIncrement</AssemblyFileRevisionType>
    <AssemblyFileRevisionFormat>00</AssemblyFileRevisionFormat>
</PropertyGroup>

Next I set the assemblyInfo.cs file's version to 1.0.0.0 in every project. Finally I saved and close it, reopened solution, and built. It works like a champ!

Now what want is to customize the Version to 4.0.1053.1 where 10 is the part of year indicator which is 2010 and 53 denotes the week number, at last 1 denotes revision number.

How to achieve this using the AssemblyInfo Task? I came across several posts that a new version of AssemblyInfo Task is available in Build Extension Pack.

I have installed the MSBuild Extension Pack December 2010 and its version is MSBuild Extension Pack 4.0.2.0 Installer

5 Answers 5

17

I do this in Jenkins by having a package build that is parameterised using the List Subversion Tags parameter type. The Subversion tag must follow the version number format (major.minor.revision.build), e.g. tags/2.0.0.1. The tag name is then assigned to a Jenkins parameter, e.g. $VERSION becomes 2.0.0.1

I use the WriteLinesToFile msbuild task to write out the assembly attribute to a second file alongside the PropertyInfo.cs called VersionInfo.cs. As checked in to source control, this just contains a default version number:

// Do not change this. The version is set on package builds only by setting the AsmVersion MSBuild property
[assembly: System.Reflection.AssemblyVersion("0.0.0.0")] 

The package build on the build server passes in the version via the AsmVersion parameter:

/p:AsmVersion=$VERSION

The .csproj file is modified to have a BeforeBuild target (Visual Studio creates a commented out one for you):

<Target Name="BeforeBuild">
    <WriteLinesToFile 
        Condition=" '$(AsmVersion)' != '' " File="Properties\VersionInfo.cs" 
        Overwrite="True"
        Lines="[assembly: System.Reflection.AssemblyVersion(&quot;$(AsmVersion)&quot;)] // Generated by build" />   
</Target>

When building in Visual Studio, or without passing in the AsmVersion, your assembly will have a default version of 0.0.0.0. When building in the package build, you will get your desired build number.

3
  • 7
    I like this way, a bit fiddly but does not require Community Tasks, I don't get why Versioning of an assembly is so non-obvious May 20, 2014 at 18:54
  • I never got the Target BeforeBuild to fire... at least no evidence of it because VersionInfo.cs never came into existence. I found this blog article that has a simpler application of VersionInfo.cs. That worked for me. codeproject.com/Articles/328977/…
    – user922020
    Mar 26, 2015 at 22:01
  • Don't use BeforeBuild as a name of the task, because it can be overridden by MSBuild built-in task with the same name. To avoid that you can define the task as: <Target Name="GenerateVersionFile" BeforeTargets="BeforeBuild">. You can look at my answer
    – PolarBear
    Oct 6, 2020 at 14:46
16

First.. use a globalassemblyinfo.cs that is linked from each project. Add its as linked file to each project. This means you update one file, not 30+ assemblyinfo files...then:

use MSBuild.Community.Tasks....

Then call

<AssemblyInfo CodeLanguage="CS"
         OutputFile="$(VersionFile)"
         AssemblyCompany="Company"
         AssemblyProduct="Product"
         AssemblyCopyright="Copyright © Company 2011"
         ComVisible="false"
         AssemblyVersion="$(BUILD_NUMBER)"
         AssemblyFileVersion="$(BUILD_NUMBER)" />

Assuming you have something like:

<Import Project=".\tasks\MSBuild.Community.Tasks.Targets"/>
3
  • Could you provide a link to where one could find MSBuild.Community.Tasks? Oct 15, 2012 at 15:13
  • Is there some documentation? Apr 18, 2013 at 14:44
  • Used to to have a .chm with it. Apr 18, 2013 at 15:19
6

As @bruceboughton proposed, you can easily generate a version assembly file during compilation without using MSBuild.Community.Tasks library:

<PropertyGroup>
    <Version>0.0.0</Version>
    <InformationalVersion>0.0.0-dev~commithash</InformationalVersion>
    <VersionFileName>$(BaseIntermediateOutputPath)Version.cs</VersionFileName>
</PropertyGroup>
<Target Name="GenerateVersionFile" BeforeTargets="BeforeBuild">
    <WriteLinesToFile
        File="$(VersionFileName)"
        Overwrite="True"
        Lines="
            [assembly: System.Reflection.AssemblyVersion(&quot;$(Version)&quot;)]
            [assembly: System.Reflection.AssemblyFileVersion(&quot;$(Version)&quot;)]
            [assembly: System.Reflection.AssemblyInformationalVersion(&quot;$(InformationalVersion)&quot;)]" />
    <ItemGroup>
        <Compile Include="$(VersionFileName)" />
    </ItemGroup>
</Target>

Remove definitions of the parameters you specify in the generated file from Properties\AssemblyInfo.cs file.

After that you can specify version by adding a parameter to the msbuild:

msbuild /property:Version=1.2.3 /property:InformationalVersion=1.2.3-dev~commithash .\SolutionFile.sln
3

Update for .NET Core style .csproj files: If you've come upon this question after having transitioned to the new .csproj format used by .NET Core, you can just set the Version property (no need to to bother with MSBuild tasks).

2

How I finally got this to work MSBuild version 12 (VS 2013).

  1. Used Nuget to get MSBuildTasks Community package
  2. Edited my .csproj file and added a path to the import the package:
<Import Project="..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets" Condition="Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.target')"/>
  1. Figured out the Regex to change just the Revision number in the AssemblyInfo.cs file:
(?<=AssemblyFileVersion\("[0-9]\.[0-9]\.[0-9]\.)(\*)

which is not XML compatible, so has to be changed to:

(?&#60;=AssemblyFileVersion\(&#34;&#91;0-9]\.&#91;0-9]\.&#91;0-9]\.)(\*)
  1. Uncommented the <Target Name="BeforeBuild"> section and added the following:
<Target Name="BeforeBuild">
    <FileUpdate Files="properties\AssemblyInfo.cs"
                Regex="(?&#60;=AssemblyFileVersion\(&#34;&#91;0-9]\.&#91;0-9]\.&#91;0-9]\.)(\*)"
                ReplacementText="$(Revision)" />
</Target>
  1. When running MSBuild added the "Revision" property to the command line e.g.
msbuild.exe myProject.csproj /t:Build /p:Configuration=Release;Revision=12345

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.