Wednesday, September 24, 2014

VS: Automatically create a NuGet Package from project upon build

Here's a neat little approach I use to effortlessly build NuGet packages.

- Get NuGet.exe and put it in the project folder. I also add it to our source control.
- Create a new Build Target named "NuGet". (optional, see below)
- In the Post Build Events of the project you want to make a NuGet package from, enter this:
if $(ConfigurationName) == NuGet (
  "$(ProjectDir)nuget.exe" pack "$(ProjectDir)$(ProjectFileName)" -OutputDirectory "\\target\directory\NuGet Feed" -Prop Configuration=$(ConfigurationName)
)
To use it, you just select the NuGet target, and build the project. It'll build the package and put it in the OutputDirectory folder.

The new "NuGet" target is in fact optional and you could use "Release" in the Post Build Event condition.

Also, you might want to edit the project's Assembly Infos and set the Company, Name, and Description properties. Pay special attention to the Version property too. Those properties appear in the NuGet package manager.

For more information on NuGet and how to get it, check this out.
To learn how to make a NuGet feed, check this out. Especially the local one. It's as simple as making a new directory, and this is what I use.

Have fun!

UPDATE:
Here's a NuGet with more features to do just that:
https://www.nuget.org/packages/CreateNewNuGetPackageFromProjectAfterEachBuild/