dotnet publishdotnet publish

本文適用于:✓ .net CORE 1.x sdk .net core 2.x sdkThis article applies to: ✓ .NET Core 1.x SDK .NET Core 2.x SDK


dotnet publish - 將應用程式與其相依性封裝至資料夾中,以部署至主機系統。dotnet publish - Packs the application and its dependencies into a folder for deployment to a hosting system.


dotnet publish [<PROJECT>] [-c|--configuration] [-f|--framework] [--force] [--manifest] [--no-build] [--no-dependencies]
    [--no-restore] [-o|--output] [-r|--runtime] [--self-contained] [-v|--verbosity] [--version-suffix]
dotnet publish [-h|--help]


dotnet publish 會編譯應用程式,讀取在其專案檔中指定的相依性,然後將產生的一組檔案發行到目錄中。dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. 此輸出包含下列資產:The output includes the following assets:

  • 組件中的中繼語言 (IL) 程式碼,副檔名為 dllIntermediate Language (IL) code in an assembly with a dll extension.
  • .deps.json 檔案,其中包含專案的所有相依性。.deps.json file that includes all of the dependencies of the project.
  • 指定應用程式預期之共用執行階段的 .runtime.config.json 檔案,以及適用於執行階段的其他設定選項 (例如記憶體回收類型)。.runtimeconfig.json file that specifies the shared runtime that the application expects, as well as other configuration options for the runtime (for example, garbage collection type).
  • 應用程式的相依性,這些相依性會從 NuGet 快取複製到輸出資料夾。The application's dependencies, which are copied from the NuGet cache into the output folder.

dotnet publish 命令的輸出已準備好部署到裝載系統 (例如伺服器、電腦、Mac、膝上型電腦) 以供執行。The dotnet publish command's output is ready for deployment to a hosting system (for example, a server, PC, Mac, laptop) for execution. 這是準備應用程式以供部署的唯一正式支援的方法。It's the only officially supported way to prepare the application for deployment. 根據專案指定的部署類型,主機系統上可能會安裝 (或不安裝) .NET Core 共用執行階段。Depending on the type of deployment that the project specifies, the hosting system may or may not have the .NET Core shared runtime installed on it. 如需詳細資訊,請參閱 .NET Core 應用程式部署For more information, see .NET Core Application Deployment. 針對已發行應用程式的目錄結構,請參閱目錄結構 (英文)For the directory structure of a published application, see Directory structure.


從 .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 publish. 它是 C#、F# 或 Visual Basic 專案檔的路徑與檔案名稱,或包含 C#、F# 或 Visual Basic 專案檔之目錄的路徑。It's either the path and filename of a C#, F#, or Visual Basic project file, or the path to a directory that contains a C#, F#, or Visual Basic project file. 如果未指定,則會預設為目前目錄。If not specified, it defaults to the current directory.


-c|--configuration {Debug|Release}

定義組建組態。Defines the build configuration. 預設值為 DebugThe default value is Debug.

-f|--framework <FRAMEWORK>

發行所指定目標架構的應用程式。Publishes the application for the specified target framework. 您必須在專案檔中指定此目標架構。You must specify the target framework in the project file.


即使最後的還原成功,仍強制解析所有相依性。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.


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


指定一或多個目標資訊清單,用以修剪應用程式發行的套件集。Specifies one or several target manifests to use to trim the set of packages published with the app. 資訊清單檔是 dotnet store 命令輸出的一部分。The manifest file is part of the output of the dotnet store command. 若要指定多個資訊清單,請為每個資訊清單新增 --manifest 選項。To specify multiple manifests, add a --manifest option for each manifest. 自 .NET Core 2.0 SDK 開始提供此選項。This option is available starting with .NET Core 2.0 SDK.


不會在發佈前建置專案。Doesn't build the project before publishing. 選項也會隱含設定 --no-restore 旗標。It also implicitly sets the --no-restore flag.


忽略專案對專案參考,並且只還原根專案。Ignores project-to-project references and only restores the root project.


執行命令時,不會執行隱含還原。Doesn't execute an implicit restore when running the command.

-o|--output <OUTPUT_DIRECTORY>

指定輸出目錄的路徑。Specifies the path for the output directory. 如果未指定,預設為 ./bin/[configuration]/[framework]/publish/ (適用於架構相依的部署) 或 ./bin/[configuration]/[framework]/[runtime]/publish/ (適用於獨立部署)。If not specified, it defaults to ./bin/[configuration]/[framework]/publish/ for a framework-dependent deployment or ./bin/[configuration]/[framework]/[runtime]/publish/ for a self-contained deployment. 如果路徑是相對的,則產生的輸出目錄會相對於專案檔案位置,而不是目前的工作目錄。If the path is relative, the output directory generated is relative to the project file location, not to the current working directory.


使用您的應用程式發行 .NET Core 執行階段,讓目標電腦不需要安裝執行階段。Publishes the .NET Core runtime with your application so the runtime doesn't need to be installed on the target machine. 如已指定執行階段識別碼,則其預設值是 trueIf a runtime identifier is specified, its default value is true. 如需不同部署類型的詳細資訊,請參閱 .NET Core 應用程式部署For more information about the different deployment types, see .NET Core application deployment.


發行所指定執行階段的應用程式。Publishes the application for a given runtime. 建立獨立性部署 (SCD) 時會使用此選項。This is used when creating a self-contained deployment (SCD). 如需執行階段識別項 (RID) 清單,請參閱 RID 目錄For a list of Runtime Identifiers (RIDs), see the RID catalog. 預設值為發行與 Framework 相依的部署 (FDD)Default is to publish a framework-dependent deployment (FDD).

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

--version-suffix <VERSION_SUFFIX>

定義要取代專案檔案的版本欄位中之星號 (*) 的版本尾碼。Defines the version suffix to replace the asterisk (*) in the version field of the project file.


發行目前目錄中的專案:Publish the project in the current directory:

dotnet publish

使用指定的專案檔發行應用程式:Publish the application using the specified project file:

dotnet publish ~/projects/app1/app1.csproj

使用 netcoreapp1.1 架構發行目前目錄中的專案:Publish the project in the current directory using the netcoreapp1.1 framework:

dotnet publish --framework netcoreapp1.1

使用 netcoreapp1.1 架構和 OS X 10.10 的執行階段來發行目前應用程式 (您必須在專案檔中列出這個 RID)。Publish the current application using the netcoreapp1.1 framework and the runtime for OS X 10.10 (you must list this RID in the project file).

dotnet publish --framework netcoreapp1.1 --runtime osx.10.11-x64

發行目前的應用程式,但不還原專案對專案 (P2P) 的參考,還原作業期間只有根專案 (.NET Core SDK 2.0 及更新版本):Publish the current application but don't restore project-to-project (P2P) references, just the root project during the restore operation (.NET Core SDK 2.0 and later versions):

dotnet publish --no-dependencies

另請參閱See also