09 August 2012

Creating a NuGet Package

I was recently doing some work which involved a third party dependency not available via NuGet which required an older version of another package which was available on NuGet repository. Now I could add this third party dependency and the older version of the other package, but if another developer came along and ran the NuGet package manager it wouldn't know about the dependency on the older package, so would happily update to the newer one and break the build.

A simple solution is to ignore NuGet and manually manage it, but there were a lot of references in this particular project and I like the fact that NuGet resolves package dependency which is a headache at the best of times. The solution I used was to create your own NuGet package and host it. As long as you add your package to the repository and it's path as a local cache to the NuGet source on each developer's machine then the inter-dependency should be managed effectively.

Get and API Key from NuGet
  1. Register with http://www.nuget.org
  2. Go to your account to reveal the API key.
Create your package
  1. Create a new package.
  2. Edit the Metadata to add dependencies. To make one a required version (= not >=) put square brackets around the version.
  3. Publish it (File, Publish).
Or Via Command Line:
  1. Source: Locate your dll to use as a NuGet package and creata a folder for the NuGet package creation.
  2. Download and install NuGet and insure you put add it to the Path environment variable: Control Panel, System, Advanced System Settings, Advanced Tab, Environment Variables Tab. I put mine in C:\Program Files (x86)\NuGet.
  3. Allow NuGet to download missing packages during build:
    • In Visual Studio: Tools, Options, Package Manager, tick: 'Allow Nuget to download...'
    • By Setting Environment variable EnableNuGetPackageRestore to true
  4. Read the the official instructions on how to create packages.
  5. Load Nuget and create a new package.
  6. Add stuff to the 'content' directory and other content (see Scott Hanselman's instructions)
  7. This creates the nuspec file. You will need to manually edit his file eg to add dependencies. Note the Version convention e.g.:
    • [version] Square brackets denote fixed version 
    •  (version] mix denotes Less than or equal to
    • version (no brackets) denotes greater than or equal to
    • (version 1, version 2) between versions 1 and 2
    • [version 1, version 2] greater than or equal to 1 and less than or equal to 2
    • empty = latest version
  8. Run the following to create your package:
    nuget pack [dll name].nuspec
  9. Consider publishing your package on NuGet
Add your local package repository to NuGet
Instructions here.

No comments:

Post a Comment