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):


In the Package Source, add the projects that you want included in this package. After adding the project, reference them from the PCL you created.

Add a .nuspec file to the project (I usually rename a text file):


The .nuspec file can read a lot of its properties from the source project. As a quick reminder, the assembly level attributes are, by default, located in the Properties\AssemblyInfo 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!

Troubleshooting

Using these packages in some of our projects caused issues. Some project couldn't find types we knew were in the NuGet packages and some projects claimed that they were referencing NuGet packages that were not available. The projects in questions were those who were loaded from several different solutions. After much pain and agony, I ran across this blog, which helped me get through: NuGet Reference Paths for Projects in Multiple Solutions by Mr. Jason Stangroome (). Thanks! :-)

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints