Wednesday, 15 February 2012

Using NuGet with a central repository

It may be suited  to your development process to use a central repository where all the NuGet packages you use in different projects are kept. That way you don’t have various packages scattered around in your pc or if you archive references you can keep doing so or if you don’t have internet connection you can still use existing packages from the central repository.


You need the NuGet package explorer to browse for the packages you are interested and get the package information but you will not use this tool to download the packages.You will need the nuget console program to download the packages. Both can be downloaded from the NuGet codeplex site.

Create a folder called “Packages” where you want to keep the repository of packages.

ie C:\References\Packages

Steps to follow

Now in your project, select to add a new item, select the Application configuration file but rename it to “packages.config”. By doing this ensures that you will get a file with .config extension and in the projects directory in one go.

add the following pre-built event in your project properties(keep the quotes and change the paths to your needs):

"C:\PathTo\nuget.exe" i "$(ProjectDir)packages.config" o "C:\PathTo\Packages"

Modify the “packages.config” and add the packages you need. You can get the id and the version using the NuGet package explorer. You should set something like this in the “packages.config”:

<?xml version="1.0" encoding="utf-8"?>
  <package id="Rx-Main" version="1.0.10605" />
  <package id="MEFedMVVM" version="1.2"/>

Tip: you can create a visual studio item template for creating the packages.config file

Now, if you rebuild the project, the packages will be downloaded and placed in your central repository of packages if they are not already there. Now you can manually add the references from the packages to your project . It would be nice for the references to be added automatically but they dont.

Tip: instead of using the pre-built event you can create a vs external tool which you can use at any time before building your project

The manual work you do any time you create a new project is that of adding the pre-built event and the “packages.config”. Any time you need a new package in your project you just modify the “packages.config” and then rebuild. The packages are kept in a central repository only once and downloaded if dont exist.

It would have been nice for the visual studio extension of NuGet to have an option where you can set the repository of the packages, in your solution or in another(common/central repository) folder. Then you would not need the setup above, the nuget console, the pre-built event and the references and all dependencies of a package could be added automatically. It would also save bandwidth off the NuGet’s site!

No comments:

Post a Comment