dotnet packdotnet pack

本主題適用於:✓ .NET Core 1.x SDK 和更新版本This topic applies to: ✓ .NET Core 1.x SDK and later versions

名稱Name

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

概要Synopsis

dotnet pack [<PROJECT>|<SOLUTION>] [-c|--configuration] [--force] [--include-source] [--include-symbols] [--interactive] 
    [--no-build] [--no-dependencies] [--no-restore] [--nologo] [-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 套件(也就是nupkg檔案)。The result of this command is a NuGet package (that is, a .nupkg file).

如果您想要產生包含 debug 符號的封裝,您有兩個可用的選項:If you want to generate a package that contains the debug symbols, you have two options available:

  • --include-symbols-它會建立符號套件。--include-symbols - it creates the symbols package.
  • --include-source-它會建立符號套件,並src在其中包含來源檔案的資料夾。--include-source - it creates the symbols package with a src folder inside containing the source files.

封裝專案的 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 run)隱含執行。Starting with .NET Core 2.0, you don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet build and dotnet run. 它在執行明確還原有意義的某些情況下仍然是有效的命令,例如 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 | SOLUTION

要包裝的專案或方案。The project or solution to pack. 它是.csproj檔案、方案檔或目錄的路徑。It's either a path to a csproj file, a solution file, or to a directory. 如果未指定,命令會在目前的目錄中搜尋專案或方案檔。If not specified, the command searches the current directory for a project or solution file.

選項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. 自 .NET Core 2.0 SDK 起可用的選項。Option available since .NET Core 2.0 SDK.

  • -h|--help

    印出命令的簡短說明。Prints out a short help for the command.

  • --include-source

    除了輸出目錄中的一般 NuGet 套件外,還包含 debug 符號 NuGet 套件。Includes the debug symbols NuGet packages in addition to the regular NuGet packages in the output directory. 來源檔案會包含在符號封裝src內的資料夾中。The sources files are included in the src folder within the symbols package.

  • --include-symbols

    除了輸出目錄中的一般 NuGet 套件外,還包含 debug 符號 NuGet 套件。Includes the debug symbols NuGet packages in addition to the regular NuGet packages in the output directory.

  • --interactive

    允許命令停止並等候使用者輸入或動作 (例如完成驗證)。Allows the command to stop and wait for user input or action (for example, to complete authentication). 自 .NET Core 3.0 SDK 起提供使用。Available since .NET Core 3.0 SDK.

  • --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. 自 .NET Core 2.0 SDK 起可用的選項。Option available since .NET Core 2.0 SDK.

  • --no-restore

    執行命令時,不會執行隱含還原。Doesn't execute an implicit restore when running the command. 自 .NET Core 2.0 SDK 起可用的選項。Option available since .NET Core 2.0 SDK.

  • --nologo

    不要顯示程式啟始橫幅或著作權訊息。Doesn't display the startup banner or the copyright message. 自 .NET Core 3.0 SDK 起提供使用。Available since .NET Core 3.0 SDK.

  • -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. 自 .NET Core 2.0 SDK 起可用的選項。Option available since .NET Core 2.0 SDK.

  • -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