NuGet 是微软开发平台(包括.NET平台)的一个包管理器,这里只介绍和.NET相关的NuGet Visual Studio扩展客户端, 在VS2010 ,VS2012 ,VS2013中默认集成了NuGet 工具, 有了它,管理项目中的第三方库变得异常简单和便捷。

NuGet 提供用户两种交互方式, 一是用户界面, 二是powershell命令行. 从vs的工具->包管理器 我们可以看到这两个选项

NuGet用户界面

NuGet命令行

下面主要介绍命令行的使用:

  •  Get-Help nuget  , 打印NuGet包含的命令, 比如获得项目中引用的库文件Get-Package, 安装,卸载,更新库 Install/Uninstall/Update-Package等
PM> Get-Help nuget
TOPIC
about_NuGet SHORT DESCRIPTION
Provides information about NuGet Package Manager commands. LONG DESCRIPTION
This topic describes the NuGet Package Manager commands. NuGet is an integrated package
management tool for adding libraries and tools to .NET projects. The following NuGet cmdlets are included. Cmdlet Description
------------------ ----------------------------------------------
Get-Package Gets the set of installed packages. With -ListAvailable,
gets the set of packages available from the package source. Install-Package Installs a package and its dependencies into the project. Uninstall-Package Uninstalls a package. If other packages depend on this package,
the command will fail unless the –Force option is specified. Update-Package Updates a package and its dependencies to a newer version. Add-BindingRedirect Examines all assemblies within the output path for a project
and adds binding redirects to the application (or web)
configuration file where necessary. Get-Project Returns a reference to the DTE (Development Tools Environment)
for the specified project. If none is specifed, returns the
default project selected in the Package Manager Console. Open-PackagePage Open the browser pointing to ProjectUrl, LicenseUrl or
ReportAbuseUrl of the specified package. Register-TabExpansion Registers a tab expansion for the parameters of a command. SEE ALSO
Online documentation: http://go.microsoft.com/fwlink/?LinkID=206619
Get-Package
Install-Package
Uninstall-Package
Update-Package
Add-BindingRedirect
Get-Project
Open-PackagePage
Register-TabExpansion
  • Get-Help ,打印命令帮助, 相当于linux bash的 man / get-help ,比如我们想看Get-Package 可以干嘛,输入get-help get-package ,输出如下:
PM> get-help get-package

NAME
Get-Package SYNOPSIS
Gets the set of installed packages. Use the -ListAvailable flag to list packages available from the package source. SYNTAX
Get-Package -Source <string> [-ListAvailable] [-Updates] [-ProjectName <string>] [-Filter <string>] [-First <int>] [-Skip <in
t>] [-AllVersions] [-IncludePrerelease] [<CommonParameters>] DESCRIPTION
Gets the set of installed packages. Use the -ListAvailable flag to list packages available from the package source. RELATED LINKS
Online version: http://docs.nuget.org/
Get-Package REMARKS
To see the examples, type: "get-help Get-Package -examples".
For more information, type: "get-help Get-Package -detailed".
For technical information, type: "get-help Get-Package -full".

  NuGet命令使用PowerShell, 所以对PowerShell熟悉的同学可以很快上手使用。

  • Install-Package ,用于安装包 , 比如我们想在名称为MvcApp的 asp.net mvc项目中引用jquery ,直接输入Install-Package jquery即可安装NuGet资源中最新版jquery , 当然我们也可以指定安装某个版本的jquery, 比如1.8.2 版jquery , 安装到MvcApp中
PM> Install-Package jquery -Version 1.8.2 -ProjectName MvcApp
'jQuery 1.8.2' already installed.
Adding 'jQuery 1.8.2' to MvcApp.
Successfully added 'jQuery 1.8.2' to MvcApp.

  安装好后,你去mvc项目的Scripts目录就可以看到添加的jquery引用(包括智能提示文件和min文件及未经压缩的文件),另外你可以看到packages.config文件里面新增了一条引用记录:

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetOpenAuth.AspNet" version="4.1.4.12333" targetFramework="net45" />
<package id="DotNetOpenAuth.Core" version="4.1.4.12333" targetFramework="net45" />
<package id="DotNetOpenAuth.OAuth.Consumer" version="4.1.4.12333" targetFramework="net45" />
<package id="DotNetOpenAuth.OAuth.Core" version="4.1.4.12333" targetFramework="net45" />
<package id="DotNetOpenAuth.OpenId.Core" version="4.1.4.12333" targetFramework="net45" />
<package id="DotNetOpenAuth.OpenId.RelyingParty" version="4.1.4.12333" targetFramework="net45" />
<package id="EntityFramework" version="5.0.0" targetFramework="net45"></package>
<package id="jQuery" version="1.8.2" targetFramework="net45" />
<package id="knockoutjs" version="2.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="4.0.30506" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages.Data" version="2.0.20710.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages.OAuth" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages.WebData" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Modernizr" version="2.6.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="System.Spatial" version="5.2.0" targetFramework="net45" />
<package id="WebGrease" version="1.3.0" targetFramework="net45" />
</packages>

另外如果引用的包存在依赖, NuGet会包含下载所有的依赖文件。

  • Update-Package ,更新包, 比如我们要使用1.9版的jquery,如下
PM> Update-Package jquery -Version 1.9 -ProjectName MvcApp
Updating 'jQuery' from version '1.8.2' to '1.9.0' in project 'MvcApp'.
Removing 'jQuery 1.8.2' from MvcApp.
Successfully removed 'jQuery 1.8.2' from MvcApp.
Adding 'jQuery 1.9.0' to MvcApp.
Installing 'jQuery 1.9.0'.
Successfully installed 'jQuery 1.9.0'.
Successfully added 'jQuery 1.9.0' to MvcApp.
Uninstalling 'jQuery 1.8.2'.
Successfully uninstalled 'jQuery 1.8.2'.

  我们之前装的1.8.2被删除, 1.9.0版的jquery被引用, 同时packages.config文件也会更新。

  • Uninstall-Package ,卸载包, 比如我们不用jquery了, 删除之 (如果有其他包引用jquery ,那么应该先删除依赖jquery的包,不然会报错。)
PM> Uninstall-Package jQuery
Removing 'jQuery 1.9.0' from MvcApp.
Successfully removed 'jQuery 1.9.0' from MvcApp.
Uninstalling 'jQuery 1.9.0'.
Successfully uninstalled 'jQuery 1.9.0'.

  同样,packages.config会删除jquery记录。

  • Get-Package ,获得项目引用的包列表,或NuGet资源中心的包列表,比如获得MvcApp引用了那些包
PM> Get-Package -ProjectName MvcApp

Id                             Version              Description/Release Notes
-- ------- -------------------------
DotNetOpenAuth.AspNet 4.1.4.12333 Enables web site visitors to log into an ASP.NET web application via popular identity provi...
DotNetOpenAuth.Core 4.1.4.12333 This package contains shared code for other NuGet packages, and contains no public API in a...
DotNetOpenAuth.OAuth.Consumer 4.1.4.12333 Improve usability, reliability, conversion rates and security simultaneously by simply aski...
DotNetOpenAuth.OAuth.Core 4.1.4.12333 This package contains shared code for other NuGet packages, and contains no public API in a...
DotNetOpenAuth.OpenId.Core 4.1.4.12333 This package contains shared code for other NuGet packages, and contains no public API in a...
DotNetOpenAuth.OpenId.Relyi... 4.1.4.12333 Increase conversion rates to your web site by lowering the bar to create an account with yo...
EntityFramework 5.0.0 Entity Framework is Microsoft's recommended data access technology for new applications.
knockoutjs 2.2.0 A JavaScript MVVM library to help you create rich, dynamic user interfaces with clean maint...
Microsoft.AspNet.Mvc 4.0.30506.0 This package contains the runtime assemblies for ASP.NET MVC. ASP.NET MVC gives you a power...
Microsoft.AspNet.Mvc.FixedD... 1.0.0 This package contains a workaround for a bug affecting mobile view caching in ASP.NET MVC 4...
Microsoft.AspNet.Razor 2.0.30506.0 This package contains the runtime assemblies for ASP.NET Web Pages. ASP.NET Web Pages and t...
Microsoft.AspNet.Web.Optimi... 1.0.0 ASP.NET Optimization introduces a way to bundle and optimize css/js files.
Microsoft.AspNet.WebApi 4.0.30506.0 This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API i...
Microsoft.AspNet.WebApi.Client 4.0.30506.0 This package adds support for formatting and content negotiation to System.Net.Http. It inc...
Microsoft.AspNet.WebApi.Core 4.0.30506.0 This package contains the core runtime assemblies for ASP.NET Web API. This package is used...
Microsoft.AspNet.WebApi.OData 4.0.30506 This package contains everything you need to create OData endpoints using ASP.NET Web API a...
Microsoft.AspNet.WebApi.Web... 4.0.30506.0 This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API i...
Microsoft.AspNet.WebPages 2.0.30506.0 This package contains core runtime assemblies shared between ASP.NET MVC and ASP.NET Web Pa...
Microsoft.AspNet.WebPages.Data 2.0.20710.0 This package contains the runtime assemblies for ASP.NET Web Pages. ASP.NET Web Pages and t...
Microsoft.AspNet.WebPages.O... 2.0.30506.0 This package contains the runtime assemblies for ASP.NET Web Pages. ASP.NET Web Pages and t...
Microsoft.AspNet.WebPages.W... 2.0.30506.0 This package contains the runtime assemblies for ASP.NET Web Pages. ASP.NET Web Pages and t...
Microsoft.Data.Edm 5.2.0 Classes to represent, construct, parse, serialize and validate entity data models. Targets ...
Microsoft.Data.OData 5.2.0 Classes to serialize, deserialize and validate OData payloads. Enables construction of ODat...
Microsoft.jQuery.Unobtrusiv... 2.0.30506.0 jQuery plugin that unobtrusively sets up jQuery.Validation.
Microsoft.Net.Http 2.0.20710.0 This package provides a programming interface for modern HTTP applications. This package in...
Microsoft.Web.Infrastructure 1.0.0.0 This package contains the Microsoft.Web.Infrastructure assembly that lets you dynamically r...
Modernizr 2.6.2 Modernizr adds classes to the <html> element which allow you to target specific browser fun...
Newtonsoft.Json 4.5.11 Json.NET is a popular high-performance JSON framework for .NET
System.Spatial 5.2.0 Contains a number of classes and canonical methods that facilitate geography and geometry s...
WebGrease 1.3.0 Web Grease is a suite of tools for optimizing javascript, css files and images.

 

以上几个命令是NuGet管理包的几个常用命令,当然我们也可以选择在用户界面中进行相应操作,有兴趣探索更多命令和功能的同学可以参考NuGet官方网站 http://docs.nuget.org/

总结:

有了NuGet, 包和包之间的依赖管理从此变得异常简单和便捷, 在团队开发中, 大家只用共享一份相同的packages.config 配置文件即可避免库引用错乱的局面。 我们甚至可以编写一小段powershell代码,为所有项目自动引用需要的包。

使用NuGet管理项目类库引用的更多相关文章

  1. NuGet管理

    使用NuGet管理项目类库引用   NuGet 是微软开发平台(包括.NET平台)的一个包管理器,这里只介绍和.NET相关的NuGet Visual Studio扩展客户端, 在VS2010 ,VS2 ...

  2. 【转】使用 NuGet 管理项目库-Phil Haack

    原文地址:https://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 ...

  3. 使用 NuGet 管理项目库

    使用 NuGet 管理项目库 Phil Haack 本文转载自:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Micros ...

  4. [转载]使用 NuGet 管理项目库

    原文:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 Mi ...

  5. 【VS开发】使用 NuGet 管理项目库

    NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 Microsoft 在全球的员工人数接近 90,000, ...

  6. 在Visual Studio中使用NuGet管理项目库

    NuGet是用来管理项目中引用的各个组件插件什么什么东西的东西,最近使用以后发现对于项目引用的维护非常方便. 暂时转一篇MSDN的文章,其实这个文章的内容就够了: http://msdn.micros ...

  7. 如何在android项目中引用project作为类库引用

    前言: 在我们开发项目的时候,存在很多多个项目共有一个资源.逻辑代码的情况,这种情况一般我们采用在开发项目中导入别的项目作为引用的类库.资源等. 操作: 1.  新建一个android项目common ...

  8. Nuget 命令 NuGet 管理项目库

    因为可视化库程序包管理器的局限性,有很多需要的功能在界面中无法完成. 以下技巧均需要在"程序包管理器控制台"中使用命令来完成. 一.改变项目目标框架后,更新程序包 当改变项目的目标 ...

  9. Nuget管理自己的项目库

    Nuget是什么 Nuget 是一种 Visual Studio 扩展工具,它能够简化在 Visual Studio 项目中添加.更新和删除库(部署为程序包)的操作.(官方地址)相信大家对这个应该还是 ...

随机推荐

  1. 初学python里的yield send next

    今天看书的时候突然看到这个想起来一直没有怎么使用过send和next试了一下 发现了一个诡异的问题 import math def get_primes(start): while 1 : if is ...

  2. 解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译)

    解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译) http://improve.dk/orcamdf-rawdatabase-a-swiss-a ...

  3. hadoop+hive使用中遇到的问题汇总

    问题排查方式  一般的错误,查看错误输出,按照关键字google 异常错误(如namenode.datanode莫名其妙挂了):查看hadoop($HADOOP_HOME/logs)或hive日志 h ...

  4. 一个空行引起的阿里云负载均衡上部署https证书的问题

    今天在阿里云上购买了WoSign的https证书,在证书签发后,在控制台下载证书文件,一共有2个文件,一个是.key文件(私钥文件),一个是.pem文件(证书文件). 然后在阿里云负载均衡“证书管理” ...

  5. 【腾讯Bugly干货分享】微信终端跨平台组件 mars 系列(一) - 高性能日志模块xlog

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ff5932cde42f1f03de29b1 本文来源: 微信客户端开发团队 ...

  6. 设计模式之美:Null Object(空对象)

    索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Null Object 的示例实现. 意图 通过对缺失对象的封装,以提供默认无任何行为的对象替代品. Encapsulate t ...

  7. ECMAScript 6 简介

    ECMAScript 6 是JavaScript的下一个标准,正处在快速开发之中,大部分已经完成了,预计将在2014年正式发布.Mozilla将在这个标准的基础上,推出JavaScript 2.0. ...

  8. 浅谈Excel开发:六 Excel 异步自定义函数

    上文介绍了Excel中的自定义函数(UDF ),它极大地扩展了Excel插件的功能,使得我们可以将业务逻辑以Excel函数的形式表示,并可以根据这些细粒度的自定义函数,构建各种复杂的分析报表. 普通的 ...

  9. Senparc.Weixin.MP SDK 微信公众平台开发教程(十六):AccessToken自动管理机制

    在<Senparc.Weixin.MP SDK 微信公众平台开发教程(八):通用接口说明>中,我介绍了获取AccessToken(通用接口)的方法. 在实际的开发过程中,所有的高级接口都需 ...

  10. underscore源码阅读记录

    这几天有大神推荐读underscore源码,趁着项目测试的空白时间,看了一下. 整个underscore包括了常用的工具函数,下面以1.3.3源码为例分析一下. _.size = function(o ...