dotnet 包dotnet pack

本主题适用于:✓ .NET Core SDK 1.x .NET Core SDK 2.xThis topic applies to: ✓ .NET Core SDK 1.x .NET Core SDK 2.x


dotnet pack - 将代码打包到 NuGet 包。dotnet pack - Packs the code into a NuGet package.


dotnet pack [<PROJECT>] [-c|--configuration] [--force] [--include-source] [--include-symbols] [--no-build] [--no-dependencies]
    [--no-restore] [-o|--output] [--runtime] [-s|--serviceable] [-v|--verbosity] [--version-suffix]
dotnet pack [-h|--help]


dotnet pack 命令生成项目并创建 NuGet 包。The dotnet pack command builds the project and creates NuGet packages. 该命令的结果是一个 NuGet 包。The result of this command is a NuGet package. 如果 --include-symbols 选项存在,则创建包含调试符号的另一个包。If the --include-symbols option is present, another package containing the debug symbols is created.

将被打包项目的 NuGet 依赖项添加到 .nuspec 文件,以便在安装包时可以进行正确解析。NuGet dependencies of the packed project are added to the .nuspec file, so they're properly resolved when the package is installed. 项目到项目的引用不会打包到项目内。Project-to-project references aren't packaged inside the project. 目前,如果具有项目到项目的依赖项,则每个项目均必须包含一个包。Currently, you must have a package per project if you have project-to-project dependencies.

默认情况下,dotnet pack 先构建项目。By default, dotnet pack builds the project first. 如果希望避免此行为,则传递 --no-build 选项。If you wish to avoid this behavior, pass the --no-build option. 此选项在持续集成 (CI) 生成方案中通常非常有用,你可以知道代码是之前生成的。This option is often useful in Continuous Integration (CI) build scenarios where you know the code was previously built.

可向 dotnet pack 命令提供 MSBuild 属性,用于打包进程。You can provide MSBuild properties to the dotnet pack command for the packing process. 有关详细信息,请参阅 NuGet 元数据属性MSBuild 命令行引用For more information, see NuGet metadata properties and the MSBuild Command-Line Reference. 示例部分介绍了如何在不同的情况下使用 MSBuild -p 开关。The Examples section shows how to use the MSBuild -p switch for a couple of different scenarios.


从 .NET Core 2.0 开始,无需运行 dotnet restore,因为它由需有还原的所有命令隐式运行,如 dotnet builddotnet runStarting with .NET Core 2.0, you don't have to run dotnet restore because it's run implicitly by all commands, such as dotnet build and dotnet run, that require a restore to occur. 在执行显式还原有意义的某些情况下,例如 Azure DevOps Services 中的持续集成生成中,或在需要显式控制还原发生时间的生成系统中,它仍然是有效的命令。It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.

以长格式传递命令时,该命令也支持 dotnet restore 选项(例如,--source)。This command also supports the dotnet restore options when passed in the long form (for example, --source). 不支持缩写选项,例如 -sShort form options, such as -s, are not supported.



    要打包的项目。The project to pack. 它可能是 csproj 文件或目录的路径。It's either a path to a csproj file or to a directory. 如果未指定,则默认为当前目录。If not specified, it defaults to the current directory.


  • -c|--configuration {Debug|Release}

    定义生成配置。Defines the build configuration. 默认值为 DebugThe default value is Debug.

  • --force

    强制解析所有依赖项,即使上次还原已成功,也不例外。Forces all dependencies to be resolved even if the last restore was successful. 指定此标记等同于删除 project.assets.json 文件。Specifying this flag is the same as deleting the project.assets.json file.

  • -h|--help

    打印出有关命令的简短帮助。Prints out a short help for the command.

  • --include-source

    将源文件包括在 NuGet 包中。Includes the source files in the NuGet package. 源文件包括在 nupkg 内的 src 文件夹中。The sources files are included in the src folder within the nupkg.

  • --include-symbols

    生成符号 nupkgGenerates the symbols nupkg.

  • --no-build

    打包前不生成项目。Doesn't build the project before packing. 还隐式设置 --no-restore 标记。It also implicit sets the --no-restore flag.

  • --no-dependencies

    忽略项目间引用,仅还原根项目。Ignores project-to-project references and only restores the root project.

  • --no-restore

    运行此命令时不执行隐式还原。Doesn't execute an implicit restore when running the command.

  • -o|--output <OUTPUT_DIRECTORY>

    将生成的包放置在指定目录。Places the built packages in the directory specified.

  • --runtime <RUNTIME_IDENTIFIER>

    指定要为其还原包的目标运行时。Specifies the target runtime to restore packages for. 有关运行时标识符 (RID) 的列表,请参阅 RID 目录For a list of Runtime Identifiers (RIDs), see the RID catalog.

  • -s|--serviceable

    设置包中可用的标志。Sets the serviceable flag in the package. 有关详细信息,请参阅 .NET 博客:.NET 4.5.1 支持 .NET NuGet 库的 Microsoft 安全更新For more information, see .NET Blog: .NET 4.5.1 Supports Microsoft Security Updates for .NET NuGet Libraries.

  • --version-suffix <VERSION_SUFFIX>

    定义项目中 $(VersionSuffix) MSBuild 属性的值。Defines the value for the $(VersionSuffix) MSBuild property in the project.

  • -v|--verbosity <LEVEL>

    设置命令的详细级别。Sets the verbosity level of the command. 允许使用的值为 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].


默认情况下,Web 项目不可打包。Web projects aren't packable by default. 若要覆盖默认行为,请将以下属性添加到 .csproj 文件中:To override the default behavior, add the following property to your .csproj file:



  • 打包当前目录中的项目:Pack the project in the current directory:

    dotnet pack
  • 打包 app1 项目:Pack the app1 project:

    dotnet pack ~/projects/app1/project.csproj
  • 打包当前目录中的项目并将生成的包放置到 nupkgs 文件夹:Pack the project in the current directory and place the resulting packages into the nupkgs folder:

    dotnet pack --output nupkgs
  • 将当前目录中的项目打包到 nupkgs 文件夹并跳过生成步骤:Pack the project in the current directory into the nupkgs folder and skip the build step:

    dotnet pack --no-build --output nupkgs
  • 将项目的版本后缀配置为 .csproj 文件中的 <VersionSuffix>$(VersionSuffix)</VersionSuffix>,使用给定的后缀打包当前项目,并更新生成的程序包版本:With the project's version suffix configured as <VersionSuffix>$(VersionSuffix)</VersionSuffix> in the .csproj file, pack the current project and update the resulting package version with the given suffix:

    dotnet pack --version-suffix "ci-1234"
  • 使用 PackageVersion MSBuild 属性将包版本设置为 2.1.0Set the package version to 2.1.0 with the PackageVersion MSBuild property:

    dotnet pack -p:PackageVersion=2.1.0
  • 打包特定目标框架的项目:Pack the project for a specific target framework:

    dotnet pack -p:TargetFrameworks=net45
  • 打包项目,并使用特定运行时 (Windows 10) 进行还原操作(.NET Core SDK 2.0 及更高版本):Pack the project and use a specific runtime (Windows 10) for the restore operation (.NET Core SDK 2.0 and later versions):

    dotnet pack --runtime win10-x64
  • 使用 .nuspec 文件打包项目:Pack the projec using a .nuspec file:

    dotnet pack  ~/projects/app1/project.csproj /p:NuspecFile=~/projects/app1/project.nuspec /p:NuspecBasePath=~/projects/app1/nuget