Studio构建NuGet程序包

Studio构建NuGet程序包

本文介绍了从Visual Studio构建NuGet程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过NuGet软件包和一个私有资源共享公司内部的程序集.该程序集面向.NET Framework 4.6.1.我希望这些NuGet程序包在发行版本期间从Visual Studio自动打包.我看到可以将<GeneratePackageOnBuild>true</GeneratePackageOnBuild>添加到.csproj.我不确定这是否是.NET Standard特有的属性,但它似乎部分起作用.但是,在构建时,我会得到

I'm trying to share an internal company assembly via NuGet packages and a private source. This assembly targets .NET Framework 4.6.1. I want these NuGet packages to pack automatically from Visual Studio during the release build. I see I can add <GeneratePackageOnBuild>true</GeneratePackageOnBuild> to .csproj. I'm not sure if this is a .NET Standard-specific property but it seems to partially work. However, when I build, I get

error MSB4044: The "GetPackOutputItemsTask" task was not given a value for the required parameter "PackageOutputPath".

我一直在尝试学习如何从Visual Studio中传递此参数,但是除了从命令行手动调用参数外,我没有看到很多关于参数的文档.有没有一种简单的方法可以在Visual Studio中执行此操作?我要解决这个错误吗?

I've been trying to learn how to pass this parameter from within Visual Studio but I don't see a lot of documentation on parameters except when calling it from the command line manually. Is there an easy way to do this from within Visual Studio? Am I going about this wrong?

这是使用.NET Framework类库.我可以从命令行运行pack命令,并使用/p:PackageOutputPath="path\here"为其提供所需的参数.看来这可能是为.NET Core和Standard项目设计的,而Visual Studio可能无法处理打包.NET Framework项目.

This is using a .NET Framework class library. I can run the pack command from the command line giving it the required parameters with /p:PackageOutputPath="path\here". It seems this might have been designed for .NET Core and Standard projects and Visual Studio might not handle packing .NET Framework projects.

推荐答案

对于.net framework项目,此GeneratePackageOnBuild属性不是必需的.创建一个新的.net Standard.net Core类库项目,右键单击该项目,您将看到一个 Pack 命令(不适用于.net框架).

This GeneratePackageOnBuild property is not something for .net framework projects. Create a new .net Standard or .net Core class library project, right-click the project you'll see a Pack command(not available for .net framework).

如果单击按钮,VS将把该程序集打包到nuget程序包中.如果某人不想每次都手动单击该按钮,请转到Project=>Properties=>Package,我们会看到Generate Nuget Packages on build复选框.启用它,然后将在每次构建后创建nuget软件包.

If we click the button, VS will pack that assembly into a nuget package. And if someone doesn't want to click that button every time manually, go Project=>Properties=>Package we can see the Generate Nuget Packages on build checkbox. Enable it, and then the nuget package will be created after every build.

实际上,在VS中启用该复选框会将<GeneratePackageOnBuild>true</GeneratePackageOnBuild>语句添加到xx.csproj,但是Project=>Properties中的 Pack 按钮或 Package 选项卡不适用于.net框架项目.

Actually, enabling that checkbox in VS will add statement <GeneratePackageOnBuild>true</GeneratePackageOnBuild> to xx.csproj, but the Pack button or Package tab in Project=>Properties are not available for .net framework projects.

所以我怕答案是肯定的,您不应该将该属性用于.net框架项目.(我在VS2017和VS2019中测试了该属性,但它们什么都没做,无法重现提到的部分工作在您的问题中

So I'm afraid the answer is negative, you should't use that property for .net framework projects.(I tested the property in VS2017 and VS2019, it all just did nothing, can't reproduce the partial work mentioned in your question)

您需要使用nuget pack命令来执行此操作,如 Lex Li 所说.为了在VS中自动执行此操作,您可以考虑在构建后事件,或使用自定义的构建后目标来运行该命令.

You need to use nuget pack command to do that as Lex Li says. And to do this in VS automatically, you can consider configuring that command in Post-Build-Event or using custom after-build target to run that command.

因为您希望这些NuGet程序包在发行版本期间从Visual Studio自动打包.您可以尝试将此脚本添加到您的xx.csproj文件中:

Since you want these NuGet packages to pack automatically from Visual Studio during the release build. You can try adding this script into your xx.csproj file:

  <Target Name="CustomPack" AfterTargets="build" Condition="'$(Configuration)'=='Release'">
    <Message Text="Custom Pack command starts ..." Importance="high"/>
    <Exec Command="nuget pack $(MSBuildProjectFile) -Properties Configuration=Release"/>
  </Target>

要成功运行此脚本,您需要下载nuget.exe 并添加以下路径环境变量.或使用完整路径,例如"C:\SomePath\Nuget.exe" pack ...

To run this script successfully, you need to download the nuget.exe and add the path of it to Environment Variables. Or use the full path like "C:\SomePath\Nuget.exe" pack ...

如果发布模式下的构建成功,您将在项目文件夹中看到一个xx.nupkg文件.

If the build in release mode succeeds, you'll see a xx.nupkg file in project folder.

此外:

1.有关nuget pack命令的更多详细信息,请参见此文档.

1.For more details about nuget pack command please see this document.

2.并且不要忘记在项目文件夹中创建一个xx.nuspec文件,以避免遇到诸如NU5115(未指定xxx)之类的警告.类似的问题,请参见此处.

2.And don't forget to create a xx.nuspec file in project folder to avoid encountering warnings like NU5115(xxx was not specified). Similar issue see here.

这篇关于从Visual Studio构建NuGet程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:06