我在 VS2017 中修改了一个 csproj 文件,以便在我的 .NET 4.5 项目发布构建时创建 Nuget 包。下一个自动化步骤是将此包添加到网络共享上我的私有(private)提要中。以下是我正在使用的命令:
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -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 "Configuration=Release" -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 "\\My feed server\Shared location"" />
<Exec Command="move *.symbols.nupkg "\\My feed server\Shared location\Symbols"" />
<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/