NuGet Package Management for .NET
Άρθρο από George Chatzimanolis Wed, 17/11/2010 - 19:15
Most of us use third-party libraries when developing a web project, so you can easily understand how difficult is sometimes to find the latest version of the library you need, keep it updated or even uninstall it easily without having to spend two hours in order to remove dependencies and delete unnecessary code. If these are your needs then you should check NuGet.
NuGet is a free open source package manager, created as a Visual Studio 2010 extension, which enables you to install, uninstall and upgrade open source packages into your application very easily. So for example if you want to add Ninject or ELMAH in your project, you only have to use NuGet and add the package to your project without even leaving Visual Studio 2010.
NuGet (formerly known as NuPack) is a free, open source developer focused package management system for the .NET platform.
You can download the .vsix extension at http://nuget.codeplex.com or you can install ASP.NET MVC 3 RC which installs NuGet as part of the setup.
After installing it you can start adding packages to your project in two ways. The first way is from Visual Studio Solution Explorer while the second way is from Package Manager Console.
NuGet Package Manager UI
This is the "easiest" way to add packages. So if you want to use Visual Studio GUI you have to right click References folder in the Solution Explorer and select the Add Library Package Reference… option.
This will bring you a dialog where you can find and install the package you want. In this dialog you can see all available online packages or you can you use the Filter option (on the top right textbox) to filter the list and find the package you want to use. After finding the desired package you can click the install button and it will be installed to your project.
You can also remove a package from your project from the same window. All installed packages are displayed in the first tab and you can uninstall them by clicking the uninstall button which is located next to the package you would like to remove.
Another great feature of NuGet is that you can create and use your own feed of packages. So for example you can have the default NuGet package feed that comes with the installation and another one with packages that your company uses. You can add your own package source by clicking on Tools > Options. After installing NuGet you will see a new node in the options tree, Package Manager. Under the General settings you can see a list of package sources. These are the sources that NuGet will look through to find packages and present them in the Gallery or via the List-Packages cmdlet (we will explain this in the Package Manager Console section below).
NuGet Package Manager Console
You can also view all available packages, install new packages or uninstall packages from a PowerShell command window that you can find in View > Other Windows.
You can manage your packages by using the following commands:
- List-Package - Lists the set of packages available from the package source. By default, lists installed packages.
- Install-Package - Installs a package and its dependencies into the project. For project packages, this installs the package and applies it to a specific project. This copies contents to the project, adds assembly references, updates web.config, etc.
- Uninstall-Package - Used to uninstall a package. If other packages depend on this package, the command will fail unless the –Force option is specified.
- Update-Package - Used to update a package and its dependencies. If the new version of the package has new dependencies, then they will get installed. If the updated version no longer depends on a package, then the dependency is uninstalled if no other packages depend on it.
- New-Package - Creates a new package when supplied with a package specification file
- Get-Project - Returns a reference to the DTE (Development Tools Environment) for the active or specified project.
Check out the Package Manager Console Command reference here on the wiki.
One final tip is that you can use the Tab key to auto-complete commands and parameters.
Another cool feature of NuGet is that it handles dependency management between libraries. So for example if you want to add nHibernate to your project you should not worry for all dependencies as they are automatically handled by NuGet.
NuGet is a fantastic open source tool that came to solve many problems. If you start using it you will definitely thought of “How you lived without it”.