Share via


成品輸出配置

在 .NET 8 和更新版本中,您可以選擇簡化組建輸出的輸出路徑和資料夾結構。 所有專案的組建輸出都會收集到通用位置,並以專案分隔。 通用位置可讓您輕鬆地使用工具來預測要在哪裡找到輸出。

根據預設,通用位置是 Directory.build.props 檔案旁名為 artifacts 的目錄。 根目錄中的 artifacts 資料夾結構如下所示:

📁 artifacts
    └──📂 <Type of output>
        └──📂 <Project name>
            └──📂 <Pivot>

下表顯示資料夾結構中每個層級的預設值。 您可以使用 Directory.build.props 檔案中的屬性來覆寫值,以及預設位置。

資料夾層級 描述 範例
輸出類型 組建輸出的類別,例如二進位檔、中繼/產生的檔案、已發佈的應用程式和 NuGet 套件。 bin, obj, publish, package
專案名稱 依每個專案區隔輸出。 MyApp
樞紐 區分不同組態、目標 Framework 和執行階段識別項的專案組建。 如果需要多個元素,元素會以底線聯結 (_)。 您可以使用 MSBuild 屬性來自訂 ArtifactsPivots debug, debug_net8.0, release, release_linux-x64

範例

下表顯示可能建立的路徑範例。

路徑 描述
artifacts\bin\MyApp\debug 當您執行 dotnet build 時,用於簡單專案的組建輸出路徑。
artifacts\obj\MyApp\debug 當您執行 dotnet build 時,用於簡單專案的中繼輸出路徑。
artifacts\bin\MyApp\debug_net8.0 多目標專案 net8.0 組建的組建輸出路徑。
artifacts\publish\MyApp\release_linux-x64 針對 linux-x64 發行時,簡單應用程式的發佈路徑。
artifacts\package\MyApp\release 為專案建立發行版本 .nupkg 的資料夾。

如何設定

若要加入加入集中式輸出路徑格式,請將下列其中一個 MSBuild 屬性新增至 Directory.Build.props 檔案:

  • 若要使用預設輸出位置,請將 UseArtifactsOutput 屬性設定為 true

    <PropertyGroup>
      <UseArtifactsOutput>true</UseArtifactsOutput>
    </PropertyGroup>
    
  • 若要設定自訂輸出位置,請新增具有值 $(MSBuildThisFileDirectory)artifactsArtifactsPath 屬性 (或任何您想要的資料夾位置)。 如果您還沒有 Directory.Build.props 檔案,則可以執行下列命令來自動產生包含 ArtifactsPath 屬性的檔案:

    dotnet new buildprops --use-artifacts
    

    產生的檔案看起來會像這樣:

    <Project>
      <PropertyGroup>
        <ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>
      </PropertyGroup>
    </Project>
    

「pivot」資料夾名稱預設為目標 Framework Moniker (TFM)、組態和執行階段識別碼 (RID) 的組合。 系統會省略任何不存在的項目。 若要自訂命名「pivot」資料夾的方式,請將 ArtifactsPivots MSBuild 屬性設定為您想要的字串。 例如:

<PropertyGroup>
  ...
  <ArtifactsPivots>$(ArtifactsPivots)_MyCustomPivot</ArtifactsPivots>
</PropertyGroup>