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

Msbuild 如何使用AssemblyInfoTask更改程序集版本号?,msbuild,msbuild-task,Msbuild,Msbuild Task,在花了一些时间了解了AssemblyInfo任务之后,我试图自动设置所有DLL的版本 所以我继续安装了它,特别是版本1.0.51130.0 安装后,我在.cspoj文件中手动添加了Import标记(通过卸载每个项目)的AssemblyInfoTask(解决方案有35个以上的项目文件) 接下来,我在每个项目中将assemblyInfo.cs文件的版本设置为1.0.0。最后,我保存并关闭了它,重新打开了解决方案,并构建了。它就像一个冠军 现在,我们需要将版本定制为4.0.1053.1,其中10是年份

在花了一些时间了解了
AssemblyInfo
任务之后,我试图自动设置所有DLL的版本

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

安装后,我在
.cspoj
文件中手动添加了
Import
标记(通过卸载每个项目)的
AssemblyInfoTask
(解决方案有35个以上的项目文件)

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

现在,我们需要将版本定制为
4.0.1053.1
,其中
10
是年份指标的一部分,即2010年,
53
表示周数,最后
1
表示修订号

如何使用
AssemblyInfo
任务实现这一点?我在几篇文章中看到,Build Extension Pack中提供了新版本的
AssemblyInfo
Task


我已于2010年12月安装了MSBuild扩展包,其版本是MSBuild扩展包4.0.2.0安装程序。。使用从每个项目链接的globalassemblyinfo.cs。 将其作为链接文件添加到每个项目。 这意味着您要更新一个文件,而不是30多个assemblyinfo文件…然后:

使用

然后打电话

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

我在Jenkins中通过使用List Subversion Tags参数类型参数化包构建来实现这一点。Subversion标记必须遵循版本号格式(主版本、次版本、修订版、构建版),例如
标记/2.0.0.1
。然后将标记名分配给Jenkins参数,例如,
$VERSION
变为
2.0.0.1

我使用
WriteLinesToFile
msbuild任务将程序集属性写入名为VersionInfo.cs的PropertyInfo.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为您创建一个注释掉的目标):



在Visual Studio中生成时,或在不传递AsmVersion的情况下,程序集的默认版本为0.0.0.0。在包构建中进行构建时,您将获得所需的构建编号。

我是如何最终将其用于MSBuild版本12(VS 2013)的

  • 使用Nuget获取MSBuildTasks社区包
  • 编辑了my.csproj文件并添加了导入包的路径:
  • 
    
  • 计算正则表达式以仅更改AssemblyInfo.cs文件中的修订号:

  • (?更新.NET Core样式的.csproj文件:如果您在转换到.NET Core使用的新.csproj格式后遇到此问题,您可以(无需费心处理MSBuild任务).

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

    你能提供一个链接到人们可以在哪里找到
    MSBuild.Community.Tasks
    ?有一些文档吗?以前它有一个.chm。我喜欢这种方式,有点灵活,但不需要社区任务,我不明白为什么程序集的版本控制是如此的不明显。我从来没有在启动预构建之前得到目标…至少没有证据表明它use VersionInfo.cs从未出现过。我发现这篇博客文章中有一个更简单的VersionInfo.cs应用程序。这对我很有用。不要在构建之前使用
    作为任务名称,因为它可以被同名的MSBuild内置任务覆盖。为了避免这种情况,可以将任务定义为:
    。您可以查看
    /p:AsmVersion=$VERSION
    
    <Target Name="BeforeBuild">
        <WriteLinesToFile 
            Condition=" '$(AsmVersion)' != '' " File="Properties\VersionInfo.cs" 
            Overwrite="True"
            Lines="[assembly: System.Reflection.AssemblyVersion(&quot;$(AsmVersion)&quot;)] // Generated by build" />   
    </Target>
    
    <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>
    
    msbuild /property:Version=1.2.3 /property:InformationalVersion=1.2.3-dev~commithash .\SolutionFile.sln