MSBuild/t:pack Nuget-Package 始终具有相同的版本 [英] MSBuild /t:pack Nuget-Package has always the same Version

查看:19
本文介绍了MSBuild/t:pack Nuget-Package 始终具有相同的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 TFS-Build 中创建一个带有 /t:pack 的 nuget 包.我不能使用 Nuget-Pack-Step,因为我正在使用

I am creating a nuget-package with /t:pack in my TFS-Build. I can't the use Nuget-Pack-Step, because I am using

<TargetFramework>netstandard2.0</TargetFramework>

如何在 Nuget-Package 上应用我的 AssemblyVersion?因为我的 Assembly-Version 是正确的,但我的 Nuget 版本始终保持 1.0.0.0.

How can I apply my AssemblyVersion on the Nuget-Package? Because my Assembly-Version is right, but my Nuget version always remains 1.0.0.0.

请注意,我使用 C# 文件作为我的程序集信息而不是 .csproj 文件.

Note I am using a C# file for my assembly information instead of the .csproj file.

有没有可能?

想要分享链接.

推荐答案

MSBuild 集成的 Pack 目标从项目内的 MSBuild 属性中读取其值(PackageVersion 为具体,默认为Version,而VersionPrefix又默认为VersionSuffix后缀.

The MSBuild-integrated Pack target reads its value from MSBuild properties inside the project (PackageVersion to be specific, which is defaulted from Version, which in turn is defaulted to VersionPrefix which in turn may be suffixed by VersionSuffix).

开箱即用支持从程序集属性读取此值,因为新项目格式旨在从确定 NuGet 包元数据的相同配置生成这些程序集属性.

There is out-of-the-box support for reading this value from an assembly attribute since the new project format is meant to generate these assembly attributes from the same configuration that determines NuGet package metadata.

但是,您可以通过向 csproj 文件添加自定义目标来扩展构建,该文件在 msbuild/t:Pack 期间读取构建的程序集的标识:

However, you can extend the build by adding a custom target to the csproj file that reads the built assembly's identity during msbuild /t:Pack:

<Project>

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);ReadPackageVersionFromOutputAssembly</GenerateNuspecDependsOn>
  </PropertyGroup>

  <Target Name="ReadPackageVersionFromOutputAssembly" DependsOnTargets="Build">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="PackAssembly" />
    </GetAssemblyIdentity>
    <PropertyGroup>
      <PackageVersion>%(PackAssembly.Version)</PackageVersion>
    </PropertyGroup>
  </Target>

</Project>

请注意,此目标将仅在完整 MSBuild"上运行,即 Windows 上的 msbuild.exe(visual studio 开发人员命令提示符)或 linux/Mac 上的 mono 5.2+.这目前不适用于 dotnet pack(MSBuild 的 .NET Core 版本). 更新:这现在可以在 .NET SDKs 2.1.* 和更高版本中使用,因为 GetAssemblyIdentity 任务已在 2018 年跨平台版本的 msbuild 中添加.

Note that this target will only run on the "full MSBuild", that is msbuild.exe on windows (visual studio developer command prompt) or mono 5.2+ on linux/Mac. This currently does not work for dotnet pack (.NET Core version of MSBuild). UPDATE: This will now work in .NET SDKs 2.1.* and higher since the GetAssemblyIdentity task has been added to the cross-platform version of msbuild in 2018.

这篇关于MSBuild/t:pack Nuget-Package 始终具有相同的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆