Manage Nuget Packages With Source Control

Nuget is a Visual Studio plugin that allows you to download and install libraries directly into your solution. It's another adapted technology by Microsoft to save time for searching and installing libraries in your projects. If you're not aware of Nuget then visit Nuget.org.

Why should I know about Nuget?

For example if you are using Entity Framework in your project in Visual Studio 2012 then you'll notice that when you add the ADO.Net Entity Data Model then your project will take a little time to get the latest release of Entity Framework through Nuget. Then in your solution directory you'll find a Packages folder and a package.config file added to your project. Similary, there are many projects who on the Nuget server ready for download, install and use in your projects. For e.g. MVC scaffolding, Log4Net, Rhino mocks, jQuery, Json.NET, Castle Windsor etc. and many more.

So if you're on VS2010 or an earlier version just get the VS plugin available for your version of Visual Studio and prepare to have all these libraries to install in your project.

Should I check in the Packages folder in the Source Control to make them available for peers?

The answer is "NO". You should not checkin packages folders since this will increase the size of the repository and it will become overhead when taking the latest (since the size of the packages folder is in the MBs).

Now if you want to have your installed libraries available to all the peers then here's the tip to use to overcome the burden of putting packages into the source control repository.

Install the Nuget package manager

If you're connected to the internet then you can download the Nuget package manager from the Online gallery from Tools -> Extension Manager.

In the Extension Manager dialog box, select the Online Gallery tab, and enter "nuget" in the search box to find the NuGet Package Manager extension.

Select NuGet Package Manager and then click Download.

If you want to download the Nuget packages installer separately then it's available in the Visual Studio Gallery.

Download and install your desired package/library from Nuget.

Now before proceeding for checkin you must enable the setting for restoring the missing packages from Visual Studio 2012.

NuGet1.jpg

This will add the following files in your solution.

NuGet2.jpg

Finally it'll add the following setting in the Nuget.Config file that will prevent the packages from being added to the source control.

NuGet3.jpg

Here's the complete documentation for the above steps.

Note: This setting will instruct the MSBUILD to download the files before building the project. But there's another option you need to enable to get the packages restored while building.

Go to Tools -> Options -> Package manager

And check the check box for "Allow Nuget to download the missing packages during builds".

There are other options available if you don't have VS installed and you need to enable this option for Build server e.g. CCnet etc. Just set the environment variable EnableNuGetPackageRestore to "true".

If you are working with .Net 2.0 or so you can readout this to get the setting enabled restoring packages during build requires explicit consent from the user.

And you're done. Next time you don't need to carry those library files with your source controls. And this keeps your repository neat and clean.
 

Up Next
    Ebook Download
    View all
    Learn
    View all