我在VS2017中修改了一个 csproj 文件,以便在我的.NET 4.5项目发布时创建一个Nuget包。下一个自动化步骤是将此包添加到网络共享上的私有Feed中。以下是我正在使用的命令:
<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 令牌的值(这是我的猜测)。
我一直在努力寻找MSBUILD令牌的参考(这本身对我们来说很有用),但我真正需要知道的是版本的正确MSBUILD令牌/变量/属性 - 这就是与生成的Nuget包相同的值。
答案 0 :(得分:1)
我探索了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://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties https://docs.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#add
答案 1 :(得分:0)
您可以使用此宏获取程序集版本。我在postbuild事件之前调用它,例如
<!--Macro to get the version -->
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
</GetAssemblyIdentity>
<ItemGroup>
<VersionNumber Include="%(CurrentAssembly.Version)" />
</ItemGroup>
</Target>
<!-- override PostBuildEvent to call PostBuildMacros -->
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
...
</PostBuildEvent>
</PropertyGroup>