technology from back to front

NuGet: Fetch missing or out of date packages on build

NuGet is a package manager for the .NET world. At its core is an API that will take a set of assemblies that you are interested in and download any that are new to a specified folder. There is then a Visual Studio plug-in that uses a convention-based approach to adding new packages to your project. It creates a folder called “packages” in your solution, and a packages.config file in each project that makes use of NuGet. When you use the GUI to add a new package to a project, it updates the config file, and then pulls down the binaries and adds them to the project.

And of course, if the binaries are missing at build time it looks at that config file and pulls them down again. Wait, it doesn’t do that? Not only does it not do that, it’s not at all clear (to me at least) how to even trigger a refresh of the binaries manually.

This is because using NuGet and then committing the binaries to your source control is the developers’ recommended way of using NuGet. Which is Not Good.

Anyway, it’s an easy fix. Add this line to the “Pre-build event command line” for each project that makes use of NuGet. (Oh, and make sure that NuGet is on your PATH)

nuget install $(ProjectDir)\packages.config -OutputDirectory $(SolutionDir)\packages

And you’re away!

The “Pre-build event command line” option can be found in project properties in the “Build Events” tab.

by
Martin Eden
on
03/06/13
  1. Ringo De Smet
    on 13/06/13 at 11:16 am

    There is no need to do that. Just make sure that you change the values of the following properties in NuGet.targets:

    • RestorePackages to true
    • RequireRestoreConsent to false

    I also changed the following to prevent committing NuGet itself:

    • DownloadNuGetExe to true

    Ringo

  2. Martin Eden
    on 01/07/13 at 3:01 pm

    I tried those settings but couldn’t get it to work. Maybe I am just missing something… but the approach I blogged certainly worked for me!

 
 


7 − = three

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us