[英]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文件,并确保
BuildPackage
为true
。
<!-- 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.