自訂組建系統Customizing the build system

Microsoft Build Engine 是用來建立應用程式的平臺。The Microsoft Build Engine is a platform for building applications. 引擎(也稱為 MSBuild)是由 Microsoft 所開發,可讓您建立 .NET 應用程式。The engine, which is also known as MSBuild, was developed by Microsoft and allows for the building of .NET applications. 而 Mono 架構也有它自己的 Microsoft Build Engine 實作,稱為 xbuildThe Mono framework also has its own implementation of Microsoft's Build Engine, called xbuild. 不過,目前已將 xbuild 改為在所有作業系統上使用 MSBuild。At this time, however, xbuild has been phased out in favor of using MSBuild on all operating systems.

MSBuild 會當做 Visual Studio for Mac 中專案的組建系統使用,並藉由取得一組輸入(例如來源檔案)並將其轉換為輸出(例如可執行檔)來運作。MSBuild is used as the build system for projects in Visual Studio for Mac and works by taking a set of inputs, such as source files, and transforms them to outputs, such as executables. 它會透過叫用編譯器等工具來達成此輸出。It achieves this output by invoking tools such as the compiler.

MSBuild 檔案MSBuild file

MSBuild 會使用稱為專案檔的 XML 檔案,以定義屬於專案一部分的「項目」** (例如影像資源),以及建置專案所需的「屬性」**。MSBuild uses an XML file, called a project file, that defines the Items that are part of your project (such as image resources), and the Properties required to build your project. 這個專案檔一律是以副檔名 proj 結尾,例如 C# 專案的 .csprojThis project file will always have a file extension ending in proj, such as .csproj for C# projects.

檢視 MSBuild 檔案Viewing the MSBuild file

您可以滑鼠右鍵按一下專案名稱,然後選取 [在搜尋工具中顯示]**** 來找到 MSBuild 檔案。Locate the MSBuild file by right-clicking on your project name and selecting Reveal in Finder. 搜尋工具視窗會顯示與您專案相關的所有檔案和資料夾,包括 .csproj 檔案,如下圖所示:The finder window displays all the files and folders related to your project, including the .csproj file, as illustrated in the following image:

搜尋工具中的 csproj 位置

若要在 Visual Studio for Mac 中使用新的索引標籤顯示 .csproj,請以滑鼠右鍵按一下專案名稱,並瀏覽至 [工具] > [編輯檔案]****:To display the .csproj in a new tab in Visual Studio for Mac, right-click on your project name and browse to Tools > Edit File:

在來源編輯器中開啟 csproj

組合 MSBuild 檔案Composition of the MSBuild file

所有的 MSBuild 檔案都包含必要的根 Project 元素,例如:All MSBuild files contain a mandatory root Project element, like so:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

一般來說,專案也會匯入 .targets 檔案。Typically, the project will also import a .targets file. 這個檔案包含許多描述如何處理和建置各種檔案的規則。This file contains many of the rules that describe how to process and build the various files. 該匯入通常會顯示在 proj 檔案的底端,而對於 C# 專案其外觀則如下:The import usually appear towards the bottom of your proj file, and for C# projects look something like this:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

目標檔案是另一個 MSBuild 檔案。The targets file is another MSBuild file. 此檔案包含多個專案重複使用的 MSBuild 程式碼。This file contains MSBuild code that is reusable by multiple projects. 例如,在 MSBuildBinPath 屬性 (或變數) 所代表的目錄中找到的 Microsoft.CSharp.targets 檔案,包含從 C# 原始程式檔建置 C# 組件的邏輯。For example, the Microsoft.CSharp.targets file, which is found in a directory represented by the MSBuildBinPath property (or variable), contains the logic for building C# assemblies from C# source files.

項目和屬性Items and properties

MSBuild 中有兩種基本資料類型:項目屬性,下列各節會詳細說明它們。There are two fundamental data types in MSBuild: items and properties, which are explained in more detail in the following sections.

屬性Properties

屬性是索引鍵/值組,用來儲存影響編譯的設定,例如編譯器選項。Properties are key/value pairs, which are used to store settings that affect compilation, such as compiler options.

它們是使用 PropertyGroup 設定,並且可以包含任意數目的 PropertiesGroups,而 PropertiesGroups 可以包含任意數目的屬性。They are set using a PropertyGroup and can contain any number of PropertiesGroups, which can contain any number of properties.

例如,簡單主控台應用程式的 PropertyGroup 看起來可能如下列 XML:For example, the PropertyGroup for a simple console application might look like the following XML:

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProjectGuid>{E248730E-1393-43CC-9183-FFA42F63BE81}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>refactoring</RootNamespace>
    <AssemblyName>refactoring</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>

可以使用 $() 語法從運算式參考屬性。Properties can be referred to from expressions using the $() syntax. 例如,$(Foo) 會評估為 Foo 屬性的值。For example, $(Foo) will be evaluated as the value of the Foo property. 如果尚未設定屬性,它會評估為空字串,而且不會產生任何錯誤。If the property has not been set, it will evaluate as an empty string, without any error.

項目Items

項目提供一種以清單或集合輸入組建系統的處理方法,通常代表檔案。Items provide a way of dealing with inputs into the build system as lists or sets, and typically represent files. 每個項目都包含項目「類型」、項目「規格」 和選擇性的任意「中繼資料」**。Each item has an item type, an item spec, and optional arbitrary metadata. 請注意,MSBuild 不會在個別項目上運作,而是對指定類型的所有項目 (稱為項目「集」**) 執行Note that MSBuild doesn't operate on individual items, it takes on all the items of a given type-called an item set

項目是藉由宣告 ItemGroup 來建立。Items are created by declaring an ItemGroup. 可以有任意數目的 ItemGroup,而 ItemGroup 可以包含任何數目的項目。There can be any number of ItemGroups, which can contain any number of items.

例如,下列程式碼片段會建立 iOS 啟動畫面。For example, the following code snippet creates the iOS Launch Screens. 啟動畫面擁有組建類型 BundleResource,包含作為映像路徑的規格:The Launch Screens have the build type BundleResource, with the spec as the path to the image:

 <ItemGroup>
    <BundleResource Include="Resources\Default-568h%402x.png" />
    <BundleResource Include="Resources\Default%402x.png" />
    <BundleResource Include="Resources\Default.png" />
    <BundleResource Include="Resources\Default-Portrait.png" />
    <BundleResource Include="Resources\Default-Portrait%402x.png" />
    <BundleResource Include="Resources\Default-Landscape%402x.png" />
  </ItemGroup>

可以使用 @() 語法從運算式參考項目集。Item sets can be referred to from expressions using the @() syntax. 例如,@(BundleResource) 會評估為 BundleResource 項目集,這表示所有 BundleResource 項目。For example, @(BundleResource) will be evaluated as the BundleResource item set, which means all of the BundleResource items. 如果沒有此類型的項目,則其為空白,而且不會產生任何錯誤。If there are no items of this type, it will be empty, without any error.

用來學習 MSBuild 的資源Resources for learning MSBuild

若要更詳細了解 MSBuild,可使用下列資源:The following resources can be used to learn about MSBuild in more detail: