如何使用 AssemblyInfoTask 更改程序集版本号?

发布于 2024-10-09 10:12:05 字数 2429 浏览 0 评论 0原文

我正在尝试自动化为所有 DLL 设置版本的过程,在花了一些时间后我开始了解最有可能实现它的 AssemblyInfo 任务。

所以我继续安装它,特别是版本1.0.51130.0

安装后,我在 .cspoj 文件中手动添加了 AssemblyInfoTaskImport 标签(通过卸载每个项目)(解决方案有超过 35 个)项目文件)。

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

接下来,我修改了 Microsoft.VersionNUmber.Target 文件,该文件将安装在路径:C:\Program Files\MSBuild\Microsoft\AssemblyInfoTask 中,并修改了以下部分:

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

接下来,我在每个项目中将 assembleInfo.cs 文件的版本设置为 1.0.0.0。最后我保存并关闭它,重新打开解决方案并构建。它就像冠军一样工作!

现在想要的是将版本自定义为 4.0.1053.1,其中 10 是年份指示符,即 2010 年,53 表示周数,最后 1 表示修订号。

如何使用 AssemblyInfo 任务来实现此目的?我看到几篇文章称,Build Extension Pack 中提供了新版本的 AssemblyInfo Task。

我已安装 MSBuild Extension Pack 2010 年 12 月,其版本为 MSBuild Extension Pack 4.0.2.0 Installer

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木森分化 2024-10-16 10:12:05

我在 Jenkins 中通过使用 List Subversion Tags 参数类型进行参数化的包构建来执行此操作。 Subversion 标签必须遵循版本号格式(major.minor.revision.build),例如tags/2.0.0.1。然后将标签名称分配给 Jenkins 参数,例如 $VERSION 变为 2.0.0.1

我使用 WriteLinesToFile msbuild 任务写出程序集属性到 PropertyInfo.cs 旁边的第二个文件(称为 VersionInfo.cs)。当签入源代码管理时,这仅包含默认版本号:

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

构建服务器上的包构建通过 AsmVersion 参数传入版本:

/p:AsmVersion=$VERSION

.csproj 文件被修改为具有 BeforeBuild 目标 ( Visual Studio 会为您创建一个已注释掉的版本):

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

在 Visual Studio 中构建时,或者不传入 AsmVersion,您的程序集将具有默认版本 0.0.0.0。在包构建中构建时,您将获得所需的构建号。

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("$(AsmVersion)")] // 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.

何以畏孤独 2024-10-16 10:12:05

首先..使用从每个项目链接的globalassembleinfo.cs。
将其作为链接文件添加到每个项目中。
这意味着您更新一个文件,而不是 30 多个 assemblyinfo 文件...然后:

使用 MSBuild.Community.Tasks ....

然后打电话

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

假设你有类似的事情:

<Import Project=".\tasks\MSBuild.Community.Tasks.Targets"/>

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"/>
你的笑 2024-10-16 10:12:05

正如 @bruceboughton 提议的,您可以在编译期间轻松生成版本程序集文件,而无需使用 MSBuild.Community.Tasks 库:

<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("$(Version)")]
            [assembly: System.Reflection.AssemblyFileVersion("$(Version)")]
            [assembly: System.Reflection.AssemblyInformationalVersion("$(InformationalVersion)")]" />
    <ItemGroup>
        <Compile Include="$(VersionFileName)" />
    </ItemGroup>
</Target>

Properties\AssemblyInfo.cs 文件中删除您在生成的文件中指定的参数的定义。

之后,您可以通过向 msbuild 添加参数来指定版本:

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

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("$(Version)")]
            [assembly: System.Reflection.AssemblyFileVersion("$(Version)")]
            [assembly: System.Reflection.AssemblyInformationalVersion("$(InformationalVersion)")]" />
    <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
冷血 2024-10-16 10:12:05

.NET Core 样式 .csproj 文件的更新:如果您在转换为 .NET Core 使用的新 .csproj 格式后遇到此问题,您可以 只需设置 Version 属性(无需担心 MSBuild 任务)。

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

听,心雨的声音 2024-10-16 10:12:05

我最终如何让它在 MSBuild 版本 12(VS 2013)上工作。

  1. 使用 Nuget 获取 MSBuildTasks 社区包
  2. 编辑了我的 .csproj 文件并添加了导入包的路径:
<Import Project="..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets" Condition="Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.target')"/>
  1. 找出正则表达式来仅更改 AssemblyInfo.cs 文件中的修订号:
(?<=AssemblyFileVersion\("[0-9]\.[0-9]\.[0-9]\.)(\*)

它与 XML 不兼容,因此必须更改至:

(?<=AssemblyFileVersion\("[0-9]\.[0-9]\.[0-9]\.)(\*)
  1. 取消注释 部分并添加以下内容:
<Target Name="BeforeBuild">
    <FileUpdate Files="properties\AssemblyInfo.cs"
                Regex="(?<=AssemblyFileVersion\("[0-9]\.[0-9]\.[0-9]\.)(\*)"
                ReplacementText="$(Revision)" />
</Target>
  1. 运行 MSBuild 时将“Revision”属性添加到命令行,例如
msbuild.exe myProject.csproj /t:Build /p:Configuration=Release;Revision=12345

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:

(?<=AssemblyFileVersion\("[0-9]\.[0-9]\.[0-9]\.)(\*)
  1. Uncommented the <Target Name="BeforeBuild"> section and added the following:
<Target Name="BeforeBuild">
    <FileUpdate Files="properties\AssemblyInfo.cs"
                Regex="(?<=AssemblyFileVersion\("[0-9]\.[0-9]\.[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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文