使用NuGet管理項目類庫引用

 NuGet 是微軟開發平臺(包括.NET平臺)的一個包管理器,這裏只介紹和.NET相關的NuGet Visual Studio擴展客戶端, 在VS2010 ,VS2012 ,VS2013中默認集成了NuGet 工具, 有了它,管理項目中的第三方庫變得異常簡單和便捷。javascript

 NuGet 提供用戶兩種交互方式, 一是用戶界面, 二是powershell命令行. 從vs的工具->包管理器 咱們能夠看到這兩個選項css

 

 NuGet用戶界面html

NuGet命令行java

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

  •  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熟悉的同窗能夠很快上手使用。linux

  • 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文件裏面新增了一條引用記錄:web

<?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會包含下載全部的依賴文件。shell

  • 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文件也會更新。bash

  • 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記錄。mvc

  • 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代碼,爲全部項目自動引用須要的包。

相關文章
相關標籤/搜索