dotnet packdotnet pack

此主題適用於:✓ .NET Core SDK 1.x .NET Core SDK 2.0This topic applies to: ✓ .NET Core SDK 1.x .NET Core SDK 2.x

名稱Name

dotnet pack - 將程式碼封裝到 NuGet 套件。dotnet pack - Packs the code into a NuGet package.

概要Synopsis

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]

說明Description

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.

您可以提供 MSBuild 屬性給 dotnet pack 命令來壓縮程序。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.

Web 專案預設無法封裝。Web projects aren't packable by default. 若要覆寫預設行為,請將下列屬性新增至您的 .csproj 檔案:To override the default behavior, add the following property to your .csproj file:

<PropertyGroup>
   <IsPackable>true</IsPackable>
</PropertyGroup>

注意

從 .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.

引數Arguments

  • PROJECT

    要封裝的專案。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.

選項Options

  • -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 implicitly 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].

範例Examples

  • 封裝目前目錄中的專案: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
    
  • 將專案針對特定目標 Framework 進行封裝: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 project using a .nuspec file:

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