简体   繁体   English

如何设置Visual Studio来构建NuGet包?

[英]How do I set Visual Studio to build a NuGet package?

如何在构建时让Visual Studio为我的库组件构建NuGet包?

I'm using a Portable Class Library as the example project. 我正在使用可移植类库作为示例项目。

  • Ensure the NuGet.exe file in .nuget folder is latest. 确保NuGet.exe文件夹中的.nuget文件是最新的。

  • Default values come from AssemblyInfo.cs , so clean that up. 默认值来自AssemblyInfo.cs ,因此请进行清理。

  • Add a NuGet package reference if you do not reference any, preferably something simple like JSON.NET. 如果不引用任何内容,则最好添加一个NuGet包引用,最好是像JSON.NET这样的简单引用。 Often, PCL projects have no external dependencies, in which case no NuGet refs and without any NuGet refs, the required MSBuild config won't get set properly, so we need to add a 'dummy'. 通常,PCL项目没有外部依赖性,在这种情况下,没有NuGet引用,也没有任何NuGet引用,所需的MSBuild配置将无法正确设置,因此我们需要添加一个“虚拟”。

  • Enable NuGet Package Restore. 启用NuGet软件包还原。

  • Edit the NuGet.targets file and ensure BuildPackage is true . 编辑NuGet.targets文件,并确保BuildPackagetrue

     <!-- Property that enables building a package from a project --> <BuildPackage Condition=" '$(BuildPackage)' == '' ">true</BuildPackage> 
  • Edit your .csproj file and add this to the first, default PropertyGroup 编辑您的.csproj文件并将其添加到第一个默认PropertyGroup

     <BuildPackage>true</BuildPackage> 
  • Rebuild your project and then search in the Output for nupkg and confirm creation and location of the package file. 重建您的项目,然后在输出中搜索nupkg并确认包文件的创建和位置。

  • Remove the dummy NuGet package reference and build again and check the Output. 删除虚拟的NuGet软件包引用,然后再次构建并检查输出。

  • To further customize the package creation, you can stick a MyProjectName.nuspec file next to your .csproj file. 若要进一步自定义程序包的创建,可以将MyProjectName.nuspec文件粘贴在.csproj文件旁边。

See http://docs.nuget.org/docs/reference/nuspec-reference for more on the NuSpec format. 有关NuSpec格式的更多信息,请参见http://docs.nuget.org/docs/reference/nuspec-reference You can also pull one from an existing package (its just a zip file) and have a nose around, see how it was done. 您还可以从现有软件包(仅是一个zip文件)中提取一个文件,并仔细看看它是如何完成的。

Add a post-build event like this: 添加这样的构建后事件:

$(SolutionDir)\.nuget\nuget.exe pack "$(MSBuildProjectFullPath)" -p Configuration=Release -o "$(MSBuildProjectDirectory)\bin\Release" -symbols"

And download and place nuget.exe in the .nuget folder alongside your solution file. 然后下载nuget.exe.nuget解决方案文件旁边的.nuget文件夹中。

You can use nuget update -self to keep the .exe fresh. 您可以使用nuget update -self保持.exe的nuget update -self

Note 注意

nuget.exe pack has a bug currently where it'll see a packages.config file and try to look for the packages it mentions in your solution but it fails to find them if the packages folder is in a strange place, eg if your solution file isn't a level up from the project. nuget.exe pack当前存在一个错误,在该错误中它将看到一个packages.config文件,并尝试查找您的解决方案中提到的软件包,但是如果packages文件夹位于一个陌生的地方(例如,如果您的解决方案),则无法找到它们文件不是项目的上一级。

To workaround this, add another post build event to copy the packages folder into the project folder. 要解决此问题,请添加另一个构建后事件,以将packages文件夹复制到项目文件夹中。

The repositorypath config setting seems to do nothing for me. repositorypath配置设置似乎对我无济于事。

See GitHub reports: 查看GitHub报告:

https://github.com/NuGet/Home/issues/5316 https://github.com/NuGet/Home/issues/5316


So funny. 真有趣 I was having problems with my usual way of auto-building a package on build when I arrived at this new way. 当我采用这种新方法时,我在自动构建构建包的惯常方法上遇到了问题。 So I looked for a suitable SO question to answer with my new post-build method when I came across my own question here! 因此,当我在这里遇到自己的问题时,我正在寻找一个合适的SO问题来回答我的新的构建后方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在未安装 Nuget 或 Visual Studio 的 Web 服务器上安装、更新、注册 Nuget package? - How do I Install, Update, Register a Nuget package on the Web Server that does not have Nuget or Visual Studio installed? Visual Studio 未在生成时创建 nuget package - Visual Studio not creating nuget package on build 如何使Visual Studio正确地将Nuget包中的DLL放置在UWP应用中? - How do I get Visual Studio to properly place DLLs from Nuget Package in UWP apps? 在Visual Studio中,如何有效地使用NuGet包源代码 - In Visual Studio, how do I efficiently work with NuGet package source code 如何在Visual Studio 2008生成中打印SET命令? - How do I print a SET command in a Visual Studio 2008 build? 不使用Visual Studio构建类库(包)NuGet包项目 - Build Class Library (Package) NuGet Package Projects Without Visual Studio 如何在visual studio中调试nuget包 - How to debug nuget package in visual studio 如何在Visual Studio中提供Nuget包的引用 - How to give reference of Nuget package in Visual Studio 如何在没有Visual Studio的情况下安装Nuget包? - How to install Nuget Package without Visual Studio? 如何在NuGet发布后更新Visual Studio Team Services上的NuGet包? - How can I update a NuGet package on Visual Studio Team Services after NuGet Publish?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM