Creating NuGet packages from a solution
Alternate title: Packaging multiple projects into one NuGet package
[Updated Thursday, March 13th, 2014: Added Troubleshooting]
Background
For a recent client project, to make it easier for my developers to create certain types of projects, I created specialized NuGet packages that contained the most commonly referenced infrastructure pieces that they would need. For example, if they were to create a portable Application Service component, they would want to reference the shared Foundation library, followed by a Application Services Foundation library and some Common Data Types. I also knew that they would be dependent on a number of NuGet packages, prominently the Microsoft.Bcl package (which, in turn, references Microsoft.Bcl.Build).Let's walk through how this is done!
To create a singular NuGet package containing all these references, create a new, Blank Visual Studio solution, add a Solution Folder called "Package Source" and a Portable Class Library Project (for packages targeting PCL):
Add a .nuspec file to the project (I usually rename a text file):
If you need to reference any NuGet packages, just right-click the project and click Manage NuGet Packages as usual.
The Post-Build Step
nuget pack "$(ProjectPath)" -IncludeReferencedProjects -OutputDir "$(SolutionDir)..\..\Packages"
Given all of the configuration ceremony above, to create a NuGet package from our project, we invoke NuGet Pack, passing in the full path to our project. Since we have a .nuspec-file with the same name as our project in the project directory, NuGet will pick up the .nuspec file and configure the package accordingly. The flag IncludeReferencedProjects ensures that the projects referenced by our PCL are included in the package. If these projects have .nuspec files themselves, the resulting package will be referenced instead. Finally, point the OutputDir to a path where you want the package to end up.
Automate all the things!
With a Visual Studio solution set up, we can now integrate the packaging process in our CI builds and be merry.
Good luck!
Comments