简体   繁体   中英

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.

  • Default values come from AssemblyInfo.cs , so clean that up.

  • Add a NuGet package reference if you do not reference any, preferably something simple like 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'.

  • Enable NuGet Package Restore.

  • Edit the NuGet.targets file and ensure BuildPackage is 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

     <BuildPackage>true</BuildPackage> 
  • Rebuild your project and then search in the Output for nupkg and confirm creation and location of the package file.

  • Remove the dummy NuGet package reference and build again and check the Output.

  • To further customize the package creation, you can stick a MyProjectName.nuspec file next to your .csproj file.

See http://docs.nuget.org/docs/reference/nuspec-reference for more on the NuSpec format. You can also pull one from an existing package (its just a zip file) and have a nose around, see how it was done.

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.

You can use nuget update -self to keep the .exe fresh.

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.

To workaround this, add another post build event to copy the packages folder into the project folder.

The repositorypath config setting seems to do nothing for me.

See GitHub reports:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM