msbuild - 在 msbuild 中检索 nupkg 版本号

标签 msbuild nuget csproj

我在 VS2017 中修改了一个 csproj 文件,以便在我的 .NET 4.5 项目发布构建时创建 Nuget 包。下一个自动化步骤是将此包添加到网络共享上我的私有(private)提要中。以下是我正在使用的命令:

<Exec Command="nuget.exe pack -Properties &quot;Configuration=Release&quot; -Symbols $(ProjectName).csproj" />
<Exec Command="nuget.exe add $(ProjectName).$(ProductVersion).nupkg -source \\MYSERVER\Nuget packages" />

第 1 行工作并生成 productname.nn.nn.nn.nn 形式的 nupkg 文件。

但是,第 2 行没有返回 ProductVersion token 的值(这是我的猜测)。

我一直在努力寻找 MSBUILD token 的引用(这本身就很有用),但我真正需要知道的是版本的正确 MSBUILD token /变量/属性 - 这就是与生成的 Nuget 包相同的值。

最佳答案

我探索了 Martin Ullrich 建议的$(PackageVersion),但即使安装了 Nuget 4.10,它也不适用于较旧的项目。另外,我无法让 Troopers 样本按照我想要的方式工作,最终我对其中一篇文章 here 进行了修改。 。我对其进行了调整,以提供程序集版本而不是文件版本。

  <UsingTask TaskName="GetVersionParts" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <AssemblyPath ParameterType="System.String" Required="true" />
      <MajorVersion ParameterType="System.Int32" Output="true" />
      <MinorVersion ParameterType="System.Int32" Output="true" />
      <BuildVersion ParameterType="System.Int32" Output="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System.Reflection" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
        Version v = AssemblyName.GetAssemblyName(this.AssemblyPath).Version;

        this.MajorVersion = v.Major;
        this.MinorVersion = v.Minor;
        this.BuildVersion = v.Build;    
      ]]>
      </Code>
    </Task>
  </UsingTask>
  <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
    <Message Text="**** After-build process starting ****" />
    <Exec Command="nuget.exe pack -Properties &quot;Configuration=Release&quot; -Symbols $(ProjectName).csproj" />
    <GetVersionParts AssemblyPath="$(OutputPath)$(AssemblyName).dll">
      <Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
      <Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
      <Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
    </GetVersionParts>
    <Exec Command="nuget.exe add $(MSBuildProjectName).$(MajorVersionNumber).$(MinorVersionNumber).$(BuildVersionNumber).nupkg -source  &quot;\\My feed server\Shared location&quot;" />
    <Exec Command="move *.symbols.nupkg &quot;\\My feed server\Shared location\Symbols&quot;" />
    <Message Text="**** After-build process completed ****" />
  </Target>

虽然这可行,但我不禁觉得它应该更容易。

进一步阅读

https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#add

关于msbuild - 在 msbuild 中检索 nupkg 版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43874823/

相关文章:

visual-studio-2012 - 在预构建事件中修改 .csproj

c# - c# 项目中没有 .sln 文件

sql-server - TFS 构建服务器上的 headless 构建 .sqlproj 文件

reflection - 单独 AppDomain 中的内联 MS 构建任务

.net - 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物理文件位置

msbuild - 如何在 nant 脚本中指定 MSBuild 版本?

.net-core - 如何防止 nuget 在 Visual Studio 2017 中打开解决方案后冗余恢复包

.net - 如何确保我维护的库/包向后兼容?

c# - 取决于另一个项目的 MSBuild 自定义任务

c# - Nuget 安装包成功但未向 csproj 添加引用