MSBuild 工作MSBuild tasks

組建平台必須能夠在建置程序期間執行任意數目的動作。A build platform needs the ability to execute any number of actions during the build process. MSBuildMSBuild 會使用「工作」來執行這些動作。uses tasks to perform these actions. 工作是 MSBuildMSBuild 用來執行不可部分完成之建置作業的可執行程式碼單元。A task is a unit of executable code used by MSBuildMSBuild to perform atomic build operations.

工作邏輯Task logic

MSBuildMSBuild XML 專案檔格式無法完全獨立執行建置作業,因此,必須在專案檔以外的地方實作工作邏輯。The MSBuildMSBuild XML project file format cannot fully execute build operations on its own, so task logic must be implemented outside of the project file.

工作的執行邏輯會實作為 .NET 類別,此類別會實作 Microsoft.Build.Framework 命名空間中所定義的 ITask 介面。The execution logic of a task is implemented as a .NET class that implements the ITask interface, which is defined in the Microsoft.Build.Framework namespace.

工作類別也會定義可供專案檔中的工作使用的輸入和輸出參數。The task class also defines the input and output parameters available to the task in the project file. 由工作類別公開的所有公用、可設定、非靜態且非抽象的屬性都可在專案檔中加以存取,方法是在 Task 項目上放置具有相同名稱的對應屬性。All public settable non-static non-abstract properties exposed by the task class can be accessed in the project file by placing a corresponding attribute with the same name on the Task element.

若要撰寫自己的工作,請編寫可實作 ITask 介面的 Managed 類別。You can write your own task by authoring a managed class that implements the ITask interface. 如需詳細資訊,請參閱工作撰寫For more information, see Task writing.

從專案檔執行工作Execute a task from a project file

在執行專案檔中的工作之前,您必須先使用 UsingTask 項目,將實作工作之組件中的類型對應到工作名稱。Before executing a task in your project file, you must first map the type in the assembly that implements the task to the task name with the UsingTask element. 這讓 MSBuildMSBuild 知道,如果它在您的專案檔中找到工作,應該到何處尋找該工作的執行邏輯。This lets MSBuildMSBuild know where to look for the execution logic of your task when it finds it in your project file.

若要執行 MSBuildMSBuild 專案檔中的工作,請使用工作名稱建立項目以做為 Target 項目的子系。To execute a task in an MSBuildMSBuild project file, create an element with the name of the task as a child of a Target element. 如果工作接受參數,則會傳遞這些參數以做為項目的屬性。If a task accepts parameters, these are passed as attributes of the element.

MSBuildMSBuild 項目清單和屬性都可用來做為參數。item lists and properties can be used as parameters. 例如,下列程式碼會呼叫 MakeDir 工作,並設定 MakeDir 物件的 Directories 屬性值等於前一個範例中所宣告的 BuildDir 屬性值。For example, the following code calls the MakeDir task and sets the value of the Directories property of the MakeDir object equal to the value of the BuildDir property declared in the previous example.

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

工作也可以將資訊傳回專案檔,資訊可儲存於項目或屬性中,以供稍後使用。Tasks can also return information to the project file, which can be stored in items or properties for later use. 例如,下列程式碼會呼叫 Copy 工作,並將來自 CopiedFiles 輸出屬性的資訊儲存於 SuccessfullyCopiedFiles 項目清單中。For example, the following code calls the Copy task and stores the information from the CopiedFiles output property in the SuccessfullyCopiedFiles item list.

<Target Name="CopyFiles">
    <Copy
        SourceFiles="@(MySourceFiles)"
        DestinationFolder="@(MyDestFolder)">
        <Output
            TaskParameter="CopiedFiles"
            ItemName="SuccessfullyCopiedFiles"/>
     </Copy>
</Target>

包含的工作Included tasks

MSBuildMSBuild 隨附許多工作,例如,用於複製檔案的 Copy、用於建立目錄的 MakeDir,以及用於編譯 Visual C#Visual C# 原始程式碼檔的 Cscships with many tasks such as Copy, which copies files, MakeDir, which creates directories, and Csc, which compiles Visual C#Visual C# source code files. 如需可用工作的完整清單和用法資訊,請參閱工作參考For a complete list of available tasks and usage information, see Task reference.

覆寫的工作Overridden tasks

MSBuildMSBuild 會在數個位置中尋找工作。looks for tasks in several locations. 第一個位置是在儲存於 .NET Framework 目錄中副檔名為 .OverrideTasks 的檔案中。The first location is in files with the extension .OverrideTasks stored in the .NET Framework directories. 這些檔案中的工作會覆寫任何其他具有相同名稱的工作,包括專案檔中的工作。Tasks in these files override any other tasks with the same names, including tasks in the project file. 第二個位置是在 .NET Framework 目錄中副檔名為 .Tasks 的檔案中。The second location is in files with the extension .Tasks in the .NET Framework directories. 如果在這兩個位置中找不到工作,就會使用專案檔中的工作。If the task is not found in either of these locations, the task in the project file is used.

另請參閱See also