MSBuild 目標MSBuild targets

依特定順序將目標設為群組工作,並允許將建置處理序分成較小的單位。Targets group tasks together in a particular order and allow the build process to be factored into smaller units. 例如,一個目標可能會刪除輸出目錄中的所有檔案來準備進行建置,而另一個目標會編譯專案的輸入,並將它們放在空目錄中。For example, one target may delete all files in the output directory to prepare for the build, while another compiles the inputs for the project and places them in the empty directory. 如需工作的詳細資訊,請參閱工作For more information on tasks, see Tasks.

在專案檔中宣告目標Declare targets in the project file

在專案檔中,目標是使用 Target 項目所宣告。Targets are declared in a project file with the Target element. 例如,下列 XML 會建立名為 Construct 的目標,其接著會呼叫具有 Compile 項目類型的 Csc 工作。For example, the following XML creates a target named Construct, which then calls the Csc task with the Compile item type.

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

與 MSBuild 屬性相同,可以重新定義目標。Like MSBuild properties, targets can be redefined. 例如,套用至物件的For example,

<Target Name="AfterBuild" >
    <Message Text="First occurrence" />
</Target>
<Target Name="AfterBuild" >
    <Message Text="Second occurrence" />
</Target>

如果執行 AfterBuild,則只會顯示「第二次出現」。If AfterBuild executes, it displays only "Second occurrence".

MSBuild 需相依於匯入順序,且目標的最後一個定義將會是系統所使用的定義。MSBuild is import-order dependent, and the last definition of a target is the definition used.

目標組建順序Target build order

如果某一個目標的輸入相依於另一個目標的輸出,則必須排序目標。Targets must be ordered if the input to one target depends on the output of another target.

有幾種方式可指定目標的執行順序。There are several ways to specify the order in which targets run.

  • 初始目標Initial targets

  • 預設目標Default targets

  • 第一個目標First target

  • 目標相依性Target dependencies

  • BeforeTargetsAfterTargets (MSBuild 4.0)BeforeTargets and AfterTargets (MSBuild 4.0)

目標絕對不會在單一建置期間執行兩次,即使組建中的後續目標相依於它也一樣。A target never runs twice during a single build, even if a subsequent target in the build depends on it. 執行目標之後,它對組建而言就已功成身退了。Once a target runs, its contribution to the build is complete.

如需目標建置順序的詳細資料和詳細資訊,請參閱目標建置順序For details and more information about the target build order, see Target build order.

目標批次處理Target batching

目標項目可能有 Outputs 屬性以 %(<中繼資料>) 形式指定中繼資料。A target element may have an Outputs attribute which specifies metadata in the form %(<Metadata>). 如果是這樣,MSBuild 會為每個唯一的中繼資料值執行一次目標,並分組或「批次處理」具有該中繼資料值的項目。If so, MSBuild runs the target once for each unique metadata value, grouping or "batching" the items that have that metadata value. 例如,套用至物件的For example,

<ItemGroup>
    <Reference Include="System.Core">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Include="System.Xml.Linq">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Include="Microsoft.CSharp">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
</ItemGroup>
<Target Name="AfterBuild"
    Outputs="%(Reference.RequiredTargetFramework)">
    <Message Text="Reference:
      @(Reference->'%(RequiredTargetFramework)')" />
</Target>

依其 RequiredTargetFramework 中繼資料,批次處理 Reference 項目。batches the Reference items by their RequiredTargetFramework metadata. 目標的輸出如下所示:The output of the target looks like this:

Reference: 3.5;3.5
Reference: 4.0

目標批次處理很少用於真實的組建。Target batching is seldom used in real builds. 工作批次處理較為常見。Task batching is more common. 如需詳細資訊,請參閱批次處理For more information, see Batching.

累加組建Incremental builds

累加組建是已最佳化的建置,因此不會執行輸出檔案與其相關對應輸入檔案為最新的目標。Incremental builds are builds that are optimized so that targets with output files that are up-to-date with respect to their corresponding input files are not executed. 目標項目可能有 InputsOutputs 屬性,並指出目標預期作為輸入的項目,以及它產生作為輸出的項目。A target element can have both Inputs and Outputs attributes, indicating what items the target expects as input, and what items it produces as output.

如果所有輸出項目都是最新的,則 MSBuild 會略過目標,這可大幅改善建置速度。If all output items are up-to-date, MSBuild skips the target, which significantly improves the build speed. 這稱為目標的累加組建。This is called an incremental build of the target. 如果只有某些檔案是最新的,則 MSBuild 會執行沒有最新項目的目標。If only some files are up-to-date, MSBuild executes the target without the up-to-date items. 這稱為目標的部分累加組建。This is called a partial incremental build of the target. 如需詳細資訊,請參閱累加建置For more information, see Incremental builds.

另請參閱See also