Creating a local NuGet repository with dependencies bundles

Posted by Marcus Hammarberg on September 1, 2011
Stats

I’m loving NuGet and it’s totally changed the way I look on brining in external dependencies to my projects. I've written about that before.

But sometimes you want to install several packages into a project. For example, when you install SpecFlow into a project you also have to install a test framework such as NUnit or MsTest. And maybe an assertion framework or a mocking framework.

But this package will contain your (or your company) preferences and maybe not be suitable to publish to NuGet.org for everyone to download.

In this post I’ll show you how to easily create a  local package where you can setup the dependencies you want. And how to use it in your solution.

I’m loving NuGet and it’s totally changed the way I look on brining in external dependencies to my projects. I've written about that before.

But sometimes you want to install several packages into a project. For example, when you install SpecFlow into a project you also have to install a test framework such as NUnit or MsTest. And maybe an assertion framework or a mocking framework.

But this package will contain your (or your company) preferences and maybe not be suitable to publish to NuGet.org for everyone to download.

In this post I’ll show you how to easily create a  local package where you can setup the dependencies you want. And how to use it in your solution.

Strategy

Our sneaky plan is to basically create a package that only has dependencies. No code. And then store it in a local NuGet Repository.

Install NuGet Package Explorer

This will to large extent be done in the .nuspec file. A XML-file that you can edit with any text editor. Or use the excellent NuGet Package Explorer. Go get it now. You want be sorry.

Create a local NuGet repository

A local NuGet repository is simply a folder with NuGet packages. You can follow the first 4 steps in this excellent blog post or just go:

  • Create a folder (mine is in C:\Dev\LocalNuGet)
  • In Visual Studio go Tools->Library Package Manager->Package Manager Settings and add your path above to the Package Sources:

adding new nuget package source

  • Double check that everything is fine, by going to the Package Manager console and try to select your package source in the drop down for Package Source:

choosing local package source

Create the NuSpec file

Open NuGet Package Explorer and fill out the basic information about your package (Id, Name, yada yada). A little gotcha is that you need to go Edit->Edit package metadata (CTRL+K) to be able to edit the package.

nuget package explorer

Scroll down a bit and you’ll find a section on dependencies.

dependencies

Press the big +-button and add to open the search dialog and find your package:

search nuget dependency

Please note that you can switch Package Source (i.e. NuGet repository). Set it to the Microsoft official NuGet repository and find your package. By doing this we will pull our dependencies from that source and don’t need to store them locally. Super-lightweight in other words.

When you’re done press OK and your package is created.

Export and test

Finally save your created package into your local package source (C:\Dev\LocalNuGet in my case). This will create a .nupkg file into that directory. Since we have no “content” – just dependencies – it will only be that solitaire file.

This file can be opened and re-edited with NuGet Package explorer, which is great if you want to update version etc.

To test your handiwork do this:

  • Open a project that you want to add your package to
  • Go to Package Manager console
  • Pick your Package Source (as above)
  • Type “Install-Package" and hit tab. This will show your local packages
  • Pick your package and hit Enter. And lo and behold:

the magic

Conclusion

Using this approach we get a lot of good stuff:

You can “Uninstall-Package –RemoveDependencies” on your bundle. That will remove it with it’s dependencies.

You can build packages that build on top of each other. For example you can have:

    • SpecFlowWithNUnit – just SpecFlow and NUnit.
    • SpecFlowWithNunitAndShould – which has a dependency to your local package SpecFlowWithNUnit and adds dependency to Should from the official source
    • SpecFlowWithNUnitAndShouldAndMoq – … you get the picture.

In this example I added dependencies to specific version of the other NuGet package. This can be done by simply removing the version number of your dependency in NuGet Package Explorer.

no version for dependency

Finally this approach can be use to share repositories for a team, a company or a group.

I hope you find this helpful.



Published by Marcus Hammarberg on Last updated