NuGet CLI 引用NuGet CLI reference

(CLI) 的 NuGet 命令行接口 nuget.exe 提供了完整的 nuget 功能,可用于安装、创建、发布和管理包,而无需对项目文件进行任何更改。The NuGet Command Line Interface (CLI), nuget.exe, provides the full extent of NuGet functionality to install, create, publish, and manage packages without making any changes to project files.

若要使用任何命令,请打开命令窗口或 bash shell,然后运行 nuget 命令和相应的选项,例如 nuget help pack (查看包命令) 的帮助。To use any command, open a command window or bash shell, then run nuget followed by the command and appropriate options, such as nuget help pack (to view help on the pack command).

本文档反映了最新版本的 NuGet CLI。This documentation reflects the latest version of the NuGet CLI. 有关所使用的任何给定版本的确切详细信息,请运行 nuget help 以获取所需的命令。For exact details for any given version that you're using, run nuget help for the desired command.

要了解如何在 nuget.exe CLI 中使用基本命令,请参阅使用 nuget.exe CLI 安装并使用包To learn how to use basic commands with the nuget.exe CLI, see Install and use packages using the nuget.exe CLI.

安装 nuget.exeInstalling nuget.exe

WindowsWindows

备注

NuGet.exe 5.0 及更高版本需要 .NET Framework 4.7.2 或更高版本才能执行。NuGet.exe 5.0 and later require .NET Framework 4.7.2 or later to execute.

  1. 请访问 nuget.org/downloads,并选择 NuGet 3.3 或更高版本(2.8.6 与 Mono 不兼容)。Visit nuget.org/downloads and select NuGet 3.3 or higher (2.8.6 is not compatible with Mono). 始终建议使用最新版。若要将包发布到 nuget.org,版本至少必须是 4.1.0。The latest version is always recommended, and 4.1.0+ is required to publish packages to nuget.org.
  2. 每次下载都直接下载 nuget.exe 文件。Each download is the nuget.exe file directly. 让浏览器将文件保存到选定文件夹。Instruct your browser to save the file to a folder of your choice. 此文件不 是安装程序;如果直接在浏览器中运行,就不会看到任何内容。The file is not an installer; you won't see anything if you run it directly from the browser.
  3. 将文件夹添加到 nuget.exe 中放置 PATH 环境变量的位置,这样就可以从任意位置使用 CLI 工具。Add the folder where you placed nuget.exe to your PATH environment variable to use the CLI tool from anywhere.

macOS/LinuxmacOS/Linux

行为可能因 OS 分发版本略有不同。Behaviors may vary slightly by OS distribution.

  1. 安装 Mono 4.4.2 或更高版本Install Mono 4.4.2 or later.

  2. 在 shell 提示符处,执行下列命令:Execute the following command at a shell prompt:

    # Download the latest stable `nuget.exe` to `/usr/local/bin`
    sudo curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
    
  3. 通过将以下脚本添加到 OS 的相应文件来创建别名(通常为 ~/.bash_aliases~/.bash_profile):Create an alias by adding the following script to the appropriate file for your OS (typically ~/.bash_aliases or ~/.bash_profile):

    # Create as alias for nuget
    alias nuget="mono /usr/local/bin/nuget.exe"
    
  4. 重载 shell。Reload the shell. 通过输入 nuget(而不使用任何参数)来测试安装。Test the installation by entering nuget with no parameters. 应该会看到 NuGet CLI 帮助。NuGet CLI help should display.

提示

若要使 NuGet CLI 在 Visual Studio 的包管理器控制台内可用,请参阅 在控制台中使用 nuget.exe CLITo make the NuGet CLI available within the Package Manager Console in Visual Studio, see Using the nuget.exe CLI in the console.

可用性Availability

有关详细信息,请参阅 功能可用性See feature availability for exact details.

  • 所有命令都在 Windows 上可用。All commands are available on Windows.
  • 所有命令都适用于在 Mono 上运行的 nuget.exe,但指示、和的情况除外 pack restore updateAll commands work with nuget.exe running on Mono except where indicated for pack, restore, and update.
  • pack restore delete locals push Mac 和 Linux 上,通过 dotnet CLI 还可以使用、、、和命令。The pack, restore, delete, locals, and push commands are also available on Mac and Linux through the dotnet CLI.

命令和适用性Commands and applicability

可用命令和对包创建、包使用和/或将包发布到主机的适用性:Available commands and applicability to package creation, package consumption, and/or publishing a package to a host:

常见命令Common Commands 适用的角色Applicable Roles NuGet 版本NuGet Version 说明Description
packpack 创建Creation 2.7+2.7+ .nuspec 或项目文件创建 NuGet 包。Creates a NuGet package from a .nuspec or project file. 在 Mono 上运行时,不支持从项目文件创建包。When running on Mono, creating a package from a project file is not supported.
pushpush 发布Publishing 全部All 将包发布到包源。Publishes a package to a package source.
configconfig 全部All 全部All 获取或设置 NuGet 配置值。Gets or sets NuGet configuration values.
help or ?help or ? 全部All 全部All 显示命令的帮助信息或帮助。Displays help information or help for a command.
localslocals 消耗Consumption 3.3 +3.3+ 列出 全局包http 缓存临时 文件夹的位置,并清除这些文件夹的内容。Lists locations of the global-packages, http-cache, and temp folders and clears the contents of those folders.
restorerestore 消耗Consumption 2.7+2.7+ 还原使用中的包管理格式所引用的所有包。Restores all packages referenced by the package management format in use. 在 Mono 上运行时,不支持使用 PackageReference 格式还原包。When running on Mono, restoring packages using the PackageReference format is not supported.
setapikeysetapikey 发布,消耗Publishing, Consumption 全部All 当包源需要访问密钥时,保存给定包源的 API 密钥。Saves an API key for a given package source when that package source requires a key for access.
specspec 创建Creation 全部All .nuspec如果从 Visual Studio 项目生成文件,则使用标记生成一个文件。Generates a .nuspec file, using tokens if generating the file from a Visual Studio project.
辅助命令Secondary Commands 适用的角色Applicable Roles NuGet 版本NuGet Version 说明Description
addadd 发布Publishing 3.3 +3.3+ 使用分层布局将包添加到非 HTTP 包源。Adds a package to a non-HTTP package source using hierarchical layout. 对于 HTTP 源,请使用 pushFor HTTP sources, use push.
deletedelete 发布Publishing 全部All 从包源中删除或取消列出包。Removes or unlists a package from a package source.
initinit 创建Creation 3.3 +3.3+ 使用分层布局,将文件夹中的包添加到包源。Adds packages from a folder to a package source using hierarchical layout.
installinstall 消耗Consumption 全部All 将包安装到当前项目中,但不修改项目或引用文件。Installs a package into the current project but does not modify projects or reference files.
listlist 消耗,可能正在发布Consumption, perhaps Publishing 全部All 显示来自给定源的包。Displays packages from a given source.
mirrormirror 发布Publishing 3.2 + 中弃用Deprecated in 3.2+ 将包及其依赖项从源存储库镜像到目标存储库。Mirrors a package and its dependencies from a source to a target repository.
searchsearch 消耗Consumption 5.8 +5.8+ 使用提供的查询字符串搜索给定的源。Searches a given source using the query string provided.
sourcessources 消耗,发布Consumption, Publishing 全部All 管理配置文件中的包源。Manages package sources in configuration files.
updateupdate 消耗Consumption 全部All 将项目的包更新为最新的可用版本。Updates a project's packages to the latest available versions. 在 Mono 上运行时不受支持。Not supported when running on Mono.

不同的命令使用各种 环境变量Different commands make use of various Environment variables.

按适用角色的 NuGet CLI 命令:NuGet CLI commands by applicable roles:

角色Role 命令Commands
消耗Consumption config, help, install, list, locals, restore, search, setapikey, sources, updateconfig, help, install, list, locals, restore, search, setapikey, sources, update
创建Creation config, help, init, pack, specconfig, help, init, pack, spec
发布Publishing add, config, delete, help, list, push, setapikey, sourcesadd, config, delete, help, list, push, setapikey, sources

例如,仅关注使用包的开发人员只需了解 NuGet 命令的子集。Developers concerned only with consuming packages, for example, need only understand that subset of NuGet commands.

备注

命令选项名称不区分大小写。Command option names are case-insensitive. 不推荐使用的选项不包括在此引用中,如 NoPrompt (替换为 NonInteractive) 并 Verbose (替换 Verbosity) 。Options that are deprecated are not included in this reference, such as NoPrompt (replaced by NonInteractive) and Verbose (replaced by Verbosity).