How to change Assembly Version Number using AssemblyInfoTask?

喜夏-厌秋 提交于 2019-11-30 08:11:59
James Woolfenden

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.

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

user8483789

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!