开发者

How to change Assembly Version Number using AssemblyInfoTask?

开发者 https://www.devze.com 2023-02-02 00:05 出处:网络
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.

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>
    <AssemblyFileBuildNumbe开发者_高级运维rType>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


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"/>


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.


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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号