MSBuildMSBuild

Microsoft Build EngineMicrosoft Build Engine 是用於建置應用程式的平台。The Microsoft Build EngineMicrosoft Build Engine is a platform for building applications. 這個引擎也稱為 MSBuild,提供了專案檔的 XML 結構描述,以控制組建平台處理和建置軟體的方式。This engine, which is also known as MSBuild, provides an XML schema for a project file that controls how the build platform processes and builds software. Visual Studio 會使用 MSBuild,但 MSBuild 並不倚賴 Visual Studio。Visual Studio uses MSBuild, but it doesn't depend on Visual Studio. 藉由在專案或方案檔上叫用 msbuild.exe,就可以在未安裝 Visual Studio 的環境中組織及建置產品。By invoking msbuild.exe on your project or solution file, you can orchestrate and build products in environments where Visual Studio isn't installed.

Visual Studio 會使用 MSBuild 載入及建置 Managed 專案。Visual Studio uses MSBuild to load and build managed projects. Visual Studio 中的專案檔 ( .csproj.vbproj.vcxproj 等等) 包含 MSBuild XML 程式碼,該程式碼會在您使用 IDE 建置專案時執行。The project files in Visual Studio (.csproj, .vbproj, .vcxproj, and others) contain MSBuild XML code that executes when you build a project by using the IDE. Visual Studio 專案會匯入所有必要的設定,並建置執行一般開發工作的流程,但是您可以在 Visual Studio 內或使用 XML 編輯器擴充或修改它們。Visual Studio projects import all the necessary settings and build processes to do typical development work, but you can extend or modify them from within Visual Studio or by using an XML editor.

如需 C++ 適用之 MSBuild 的相關資訊,請參閱 MSBuild (Visual C++)For information about MSBuild for C++, see MSBuild (Visual C++).

下列範例將說明何時可能會使用 MSBuild 命令列執行組建,而不是使用 Visual Studio IDE。The following examples illustrate when you might run builds by using an MSBuild command line instead of the Visual Studio IDE.

  • 未安裝 Visual Studio。Visual Studio isn't installed. (下載 MSBuild 而不使用 Visual Studio)(download MSBuild without Visual Studio)

  • 您想要使用 64 位元版的 MSBuild。You want to use the 64-bit version of MSBuild. 通常並不需要這個版本的 MSBuild,不過它可讓 MSBuild 存取更多記憶體。This version of MSBuild is usually unnecessary, but it allows MSBuild to access more memory.

  • 您想要在多個流程中執行組建。You want to run a build in multiple processes. 不過,您可以使用 IDE 在 C++ 和 C# 的專案中得到相同的結果。However, you can use the IDE to achieve the same result on projects in C++ and C#.

  • 您想要修改建置系統。You want to modify the build system. 例如,您可能會想要啟用下列動作:For example, you might want to enable the following actions:

    • 使用編譯器處理檔案之前,先對檔案進行前置處理。Preprocess files before they reach the compiler.

    • 將組建輸出複製到不同位置。Copy the build outputs to a different place.

    • 從組建輸出建立壓縮檔。Create compressed files from build outputs.

    • 進行後續處理步驟。Do a post-processing step. 例如,您可能想要對組件加上不同版本的戳記。For example, you might want to stamp an assembly with a different version.

您可以在 Visual Studio IDE 中撰寫程式碼,但是使用 MSBuild 執行組建。You can write code in the Visual Studio IDE but run builds by using MSBuild. 另一種替代方式是,您可以在開發電腦的 IDE 中建置程式碼,但是使用 MSBuild 命令列建置整合自多位開發人員的程式碼。As another alternative, you can build code in the IDE on a development computer but use an MSBuild command line to build code that's integrated from multiple developers.

Note

您可以使用 Team Foundation Build 自動編譯、測試和部署您的應用程式。You can use Team Foundation Build to automatically compile, test, and deploy your application. 您的建置系統可以在開發人員簽入程式碼 (例如,做為連續整合策略的一部分) 時或是根據排程 (例如,夜間組建驗證測試組建) 自動執行組建。Your build system can automatically run builds when developers check in code (for example, as part of a Continuous Integration strategy) or according to a schedule (for example, a nightly Build Verification Test build). Team Foundation Build 會使用 MSBuild 編譯您的程式碼。Team Foundation Build compiles your code by using MSBuild. 如需詳細資訊,請參閱 Azure PipelinesFor more information, see Azure Pipelines.

本主題提供 MSBuild 的概觀。This topic provides an overview of MSBuild. 如需入門教學課程,請參閱逐步解說:使用 MSBuildFor an introductory tutorial, see Walkthrough: Using MSBuild.

在命令提示字元中使用 MSBuildUse MSBuild at a command prompt

若要在命令提示字元執行 MSBuildMSBuild,請使用適當的命令列選項,將專案檔傳遞給 MSBuild.exeTo run MSBuildMSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. 命令列選項能讓您設定屬性、執行特定目標,以及設定可控制建置流程的其他選項。Command-line options let you set properties, execute specific targets, and set other options that control the build process. 例如,您可以使用下列命令列語法,在 Configuration 屬性設為 Debug 的情況下建置 MyProj.proj 檔案。For example, you would use the following command-line syntax to build the file MyProj.proj with the Configuration property set to Debug.

MSBuild.exe MyProj.proj -property:Configuration=Debug

如需有關 MSBuildMSBuild 命令列選項的詳細資訊,請參閱命令列參考For more information about MSBuildMSBuild command-line options, see Command-line reference.

Important

下載專案之前,請判斷程式碼的可信度。Before you download a project, determine the trustworthiness of the code.

專案檔Project file

MSBuildMSBuild 使用直接且可擴充的 XML 專案檔格式。uses an XML-based project file format that's straightforward and extensible. MSBuildMSBuild 專案檔格式讓開發人員可以描述將要建置的項目,以及如何針對不同的作業系統和組態來建置這些項目。The MSBuildMSBuild project file format lets developers describe the items that are to be built, and also how they are to be built for different operating systems and configurations. 此外,專案檔格式還能讓開發人員撰寫可重複使用的建置規則供個別檔案使用,讓這些組建在產品內的不同專案中仍有一致的表現。In addition, the project file format lets developers author reusable build rules that can be factored into separate files so that builds can be performed consistently across different projects in the product.

下列章節將說明 MSBuildMSBuild 專案檔格式的一些基本項目。The following sections describe some of the basic elements of the MSBuildMSBuild project file format. 如需如何建立基本專案檔的教學課程,請參閱逐步解說:從頭開始建立 MSBuild 專案檔For a tutorial about how to create a basic project file, see Walkthrough: Creating an MSBuild project file from scratch.

屬性Properties

屬性表示成對的索引鍵/值組,可以用來設定組建。Properties represent key/value pairs that can be used to configure builds. 宣告屬性的方式是建立具有屬性名稱的項目,做為 PropertyGroup 項目的子項目。Properties are declared by creating an element that has the name of the property as a child of a PropertyGroup element. 例如,下列程式碼會建立名為 BuildDir 並具有 Build 值的屬性。For example, the following code creates a property named BuildDir that has a value of Build.

<PropertyGroup>
    <BuildDir>Build</BuildDir>
</PropertyGroup>

您可以在項目中放入 Condition 屬性 (Attribute),藉此定義條件式屬性 (Property)。You can define a property conditionally by placing a Condition attribute in the element. 除非條件判斷值為 true,否則會忽略條件式項目的內容。The contents of conditional elements are ignored unless the condition evaluates to true. 下列範例會定義 Configuration 項目 (如果尚未定義的話)。In the following example, the Configuration element is defined if it hasn't yet been defined.

<Configuration  Condition=" '$(Configuration)' == '' ">Debug</Configuration>

使用語法 $(<PropertyName>),就可以在整個專案檔中參考屬性。Properties can be referenced throughout the project file by using the syntax $(<PropertyName>). 例如,您可以使用 $(BuildDir)$(Configuration) 參考上述範例中的屬性。For example, you can reference the properties in the previous examples by using $(BuildDir) and $(Configuration).

如需屬性的詳細資訊,請參閱 MSBuild 屬性For more information about properties, see MSBuild properties.

項目Items

項目是建置系統的輸入內容,通常代表檔案。Items are inputs into the build system and typically represent files. 項目會依據使用者定義的項目名稱分組為各個項目類型。Items are grouped into item types, based on user-defined item names. 這些項目類型可以做為工作的參數,工作會使用個別項目來執行建置流程的步驟。These item types can be used as parameters for tasks, which use the individual items to perform the steps of the build process.

在專案檔中宣告項目 (Item) 的方式就是建立一個具有項目 (Item) 類型名稱的項目 (Element),做為 ItemGroup 項目 (Element) 的子系。Items are declared in the project file by creating an element that has the name of the item type as a child of an ItemGroup element. 例如,下列程式碼會建立名為 Compile 的項目 (Item) 類型,其中包含兩個檔案。For example, the following code creates an item type named Compile, which includes two files.

<ItemGroup>
    <Compile Include = "file1.cs"/>
    <Compile Include = "file2.cs"/>
</ItemGroup>

使用語法 @(<ItemType>),就可以在整個專案檔中參考項目類型。Item types can be referenced throughout the project file by using the syntax @(<ItemType>). 例如,範例中的項目類型應該是藉由使用 @(Compile) 參考的。For example, the item type in the example would be referenced by using @(Compile).

在 MSBuild 中,項目 (Element) 和屬性 (Attribute) 名稱區分大小寫。In MSBuild, element and attribute names are case-sensitive. 不過,屬性 (Property)、項目 (Item) 和中繼資料名稱則不區分大小寫。However, property, item, and metadata names are not. 下列範例會建立項目類型 CompilecomPile 或任何其他大小寫變化,並且為項目類型提供 "one.cs;two.cs" 值。The following example creates the item type Compile, comPile, or any other case variation, and gives the item type the value "one.cs;two.cs".

<ItemGroup>
  <Compile Include="one.cs" />
  <comPile Include="two.cs" />
</ItemGroup>

在進階的建置案例中,可以使用萬用字元宣告項目,而項目中可包含額外中繼資料。Items can be declared by using wildcard characters and may contain additional metadata for more advanced build scenarios. 如需項目的詳細資訊,請參閱項目For more information about items, see Items.

工作Tasks

工作是 MSBuildMSBuild 專案用來執行建置作業之可執行程式碼的單元。Tasks are units of executable code that MSBuildMSBuild projects use to perform build operations. 例如,工作可能是編譯輸入檔,或是執行外部工具。For example, a task might compile input files or run an external tool. 工作可以重複使用,而且可以由不同專案中的不同開發人員共用。Tasks can be reused, and they can be shared by different developers in different projects.

工作的執行邏輯是以 Managed 程式碼撰寫,並使用 UsingTask 項目對應到 MSBuildMSBuildThe execution logic of a task is written in managed code and mapped to MSBuildMSBuild by using the UsingTask element. 若想撰寫自己的工作,您可以撰寫一個實作 ITask 介面的 Managed 類型。You can write your own task by authoring a managed type that implements the ITask interface. 如需有關如何撰寫工作的詳細資訊,請參閱工作撰寫For more information about how to write tasks, see Task writing.

MSBuildMSBuild 包含您可以依據自己的需求修改的一般工作。includes common tasks that you can modify to suit your requirements. 範例包括用於複製檔案的 Copy、用於建立目錄的 MakeDir,以及用於編譯 Visual C# 原始程式碼檔的 CscExamples are Copy, which copies files, MakeDir, which creates directories, and Csc, which compiles Visual C# source code files. 如需可用工作的清單和用法資訊,請參閱工作參考For a list of available tasks together with usage information, see Task reference.

MSBuildMSBuild 專案檔中執行工作的方式就是建立一個具有工作名稱的項目,做為 Target 項目的子系。A task is executed in an MSBuildMSBuild project file by creating an element that has the name of the task as a child of a Target element. 工作通常會接受參數,而這些參數會當做項目的屬性傳遞。Tasks typically accept parameters, which are passed as attributes of the element. MSBuildMSBuild 屬性和項目都可當做參數使用。Both MSBuildMSBuild properties and items can be used as parameters. 例如,下列程式碼會呼叫 MakeDir 工作,並將前面範例中宣告的 BuildDir 屬性值傳遞給此工作。For example, the following code calls the MakeDir task and passes it the value of the BuildDir property that was declared in the earlier example.

<Target Name="MakeBuildDirectory">
    <MakeDir  Directories="$(BuildDir)" />
</Target>

如需工作的詳細資訊,請參閱工作For more information about tasks, see Tasks.

目標Targets

目標 (Target) 會將工作以特殊順序組成群組,並公開 (Expose) 專案檔的區段做為建置處理序的進入點 (Entry Point)。Targets group tasks together in a particular order and expose sections of the project file as entry points into the build process. 目標通常會分組為許多邏輯區段,以提高可讀性和進行擴充。Targets are often grouped into logical sections to increase readability and to allow for expansion. 將建置步驟分成多個目標之後,您就可以從其他目標呼叫一段建置流程,而不需要在每個目標內複製那一段程式碼。Breaking the build steps into targets lets you call one piece of the build process from other targets without copying that section of code into every target. 例如,如果建置流程的許多個進入點都必須建置參考,您可以建立一個建置參考的目標,再於每個需要建置參考的進入點執行此目標。For example, if several entry points into the build process require references to be built, you can create a target that builds references and then run that target from every entry point where it's required.

在專案檔中,目標是使用 Target 項目宣告的。Targets are declared in the project file by using the Target element. 例如,下列程式碼會建立名為 Compile 的目標,接著呼叫 Csc 工作,而此工作具有在前面範例中宣告的項目清單。For example, the following code creates a target named Compile, which then calls the Csc task that has the item list that was declared in the earlier example.

<Target Name="Compile">
    <Csc Sources="@(Compile)" />
</Target>

在進階案例中,目標可用於描述彼此之間的關係並執行相依性分析,如果目標是最新版,即可略過建置流程的整個區段。In more advanced scenarios, targets can be used to describe relationships among one another and perform dependency analysis so that whole sections of the build process can be skipped if that target is up-to-date. 如需目標的詳細資訊,請參閱目標For more information about targets, see Targets.

組建記錄檔Build logs

您可以將建置錯誤、警告和訊息記錄至主控台或另一個輸出裝置。You can log build errors, warnings, and messages to the console or another output device. 如需詳細資訊,請參閱取得組建記錄檔MSBuild 中的記錄For more information, see Obtaining build logs and Logging in MSBuild.

在 Visual Studio 中使用 MSBuildUse MSBuild in Visual Studio

Visual StudioVisual Studio 會使用 MSBuildMSBuild 專案檔格式儲存 Managed 專案的建置資訊。uses the MSBuildMSBuild project file format to store build information about managed projects. 使用 Visual StudioVisual Studio 介面加入或變更的專案設定,會反映在針對每個專案產生的 .*proj 檔案中。Project settings that are added or changed by using the Visual StudioVisual Studio interface are reflected in the .*proj file that's generated for every project. Visual StudioVisual Studio 會使用 MSBuildMSBuild 的裝載執行個體 (Hosted Instance) 來建置 Managed 專案。uses a hosted instance of MSBuildMSBuild to build managed projects. 這表示 Managed 專案可以在 Visual StudioVisual Studio 中或是於命令提示字元 (即使未安裝 Visual StudioVisual Studio) 建置,其結果完全相同。This means that a managed project can be built in Visual StudioVisual Studio or at a command prompt (even if Visual StudioVisual Studio isn't installed), and the results will be identical.

如需如何在 Visual Studio 中使用 MSBuild 的教學課程,請參閱逐步解說:使用 MSBuildFor a tutorial about how to use MSBuild in Visual Studio, see Walkthrough: Using MSBuild.

多目標Multitargeting

您可以使用 Visual Studio,將應用程式編譯為在數個 .NET Framework 版本中的任何一版上執行。By using Visual Studio, you can compile an application to run on any one of several versions of the .NET Framework. 例如,您可以將應用程式編譯為在 32 位元平台的 .NET Framework 2.0 上執行,也可以將同一個應用程式編譯為在 64 位元平台的 .NET Framework 4.5 上執行。For example, you can compile an application to run on the .NET Framework 2.0 on a 32-bit platform, and you can compile the same application to run on the .NET Framework 4.5 on a 64-bit platform. 編譯為一個以上 Framework 版本的能力稱為「多目標」(Multitargeting)。The ability to compile to more than one framework is named multitargeting.

以下為多目標的一些優點:These are some of the benefits of multitargeting:

  • 您可以開發以較舊版 .NET Framework (例如 2.0、3.0 和 3.5 版) 為目標的應用程式。You can develop applications that target earlier versions of the .NET Framework, for example, versions 2.0, 3.0, and 3.5.

  • 您可以將 .NET Framework 以外的 Framework 做為目標,例如 Silverlight。You can target frameworks other than the .NET Framework, for example, Silverlight.

  • 您可以將「Framework 設定檔」 當做目標,這是預先定義的目標 Framework 子集。You can target a framework profile, which is a predefined subset of a target framework.

  • 如果 .NET Framework 目前版本的 Service Pack 已發行,您可以將它當做目標。If a service pack for the current version of the .NET Framework is released, you could target it.

  • 多目標可保證應用程式只使用目標 Framework 和平台中提供的功能。Multitargeting guarantees that an application uses only the functionality that's available in the target framework and platform.

如需詳細資訊,請參閱多目標For more information, see Multitargeting.

另請參閱See also

標題Title 說明Description
逐步解說:從頭開始建立 MSBuild 專案檔Walkthrough: Creating an MSBuild project file from scratch 顯示如何僅使用文字編輯器來累加建立基本專案檔。Shows how to create a basic project file incrementally, by using only a text editor.
逐步解說:使用 MSBuildWalkthrough: Using MSBuild 介紹 MSBuild 的建置區塊,以及顯示如何在不關閉 Visual Studio IDE 的情況下,撰寫和管理 MSBuild 專案及進行偵錯。Introduces the building blocks of MSBuild and shows how to write, manipulate, and debug MSBuild projects without closing the Visual Studio IDE.
MSBuild 概念MSBuild concepts 呈現 MSBuild 的四個建置組塊:屬性、項目、目標和工作。Presents the four building blocks of MSBuild: properties, items, targets, and tasks.
項目Items 描述 MSBuildMSBuild 檔案格式的一般概念,以及項目如何彼此搭配。Describes the general concepts behind the MSBuildMSBuild file format and how the pieces fit together.
MSBuild 屬性MSBuild properties 介紹屬性和屬性集合。Introduces properties and property collections. 屬性是成對的索引鍵/值組,可以用來設定組建 (Build)。Properties are key/value pairs that can be used to configure builds.
目標Targets 解釋如何以特定順序將各項工作集合在一起成為群組,並能夠在命令列上呼叫建置流程的區段。Explains how to group tasks together in a particular order and enable sections of the build process to be called on the command line.
工作Tasks 顯示如何建立 MSBuildMSBuild 可用來執行原子建置作業的可執行程式碼單元。Shows how to create a unit of executable code that can be used by MSBuildMSBuild to perform atomic build operations.
條件Conditions 討論如何在 MSBuild 項目中使用 Condition 屬性。Discusses how to use the Condition attribute in an MSBuild element.
進階概念Advanced concepts 呈現批次處理、執行轉換、多目標、以及其他進階技巧。Presents batching, performing transforms, multitargeting, and other advanced techniques.
MSBuild 中的記錄Logging in MSBuild 描述如何記錄建置事件、訊息和錯誤。Describes how to log build events, messages, and errors.
其他資源Additional resources 列出社群和支援資源,以提供 MSBuild 的詳細資訊。Lists community and support resources for more information about MSBuild.

參考資料Reference