目標組建順序Target build order

如果某一個目標的輸入相依於另一個目標的輸出,則必須排序目標。Targets must be ordered if the input to one target depends on the output of another target. 您可以使用這些屬性來指定執行目標的順序:You can use these attributes to specify the order in which targets are run:

  • InitialTargets.InitialTargets. 這個 Project 屬性會指定優先執行的目標,即使已在命令列上或 DefaultTargets 屬性中指定目標也一樣。This Project attribute specifies the targets that will run first, even if targets are specified on the command line or in the DefaultTargets attribute.

  • DefaultTargets.DefaultTargets. 如果未在命令列上明確指定目標,則這個 Project 屬性會指定要執行哪些目標。This Project atttribute specifies which targets are run if a target is not specified explicitly on the command line.

  • DependsOnTargets.DependsOnTargets. 這個 Target 屬性會指定必須在此目標執行之前執行的目標。This Target attribute specifies targets that must run before this target can run.

  • BeforeTargetsAfterTargetsBeforeTargets and AfterTargets. 這些 Target 屬性會指定此目標應該在指定的目標之前或之後執行 (MSBuild 4.0)。These Target attributes specify that this target should run before or after the specified targets (MSBuild 4.0).

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

    目標可能會有 Condition 屬性。Targets may have a Condition attribute. 如果指定的條件評估為 false,則不會執行目標,且不會對組建產生任何作用。If the specified condition evaluates to false, the target isn't executed and has no effect on the build. 如需條件的詳細資訊,請參閱條件For more information about conditions, see Conditions.

初始目標Initial targets

Project 項目的 InitialTargets 屬性會指定優先執行的目標,即使已在命令列上或 DefaultTargets 屬性中指定目標也一樣。The InitialTargets attribute of the Project element specifies targets that will run first, even if targets are specified on the command line or in the DefaultTargets attribute. 初始目標通常用於錯誤檢查。Initial targets are typically used for error checking.

InitialTargets 屬性的值可以是以分號分隔且已排序的目標清單。The value of the InitialTargets attribute can be a semicolon-delimited, ordered list of targets. 下列範例會指定 Warm 目標執行,然後 Eject 目標執行。The following example specifies that the Warm target runs, and then the Eject target runs.

<Project InitialTargets="Warm;Eject" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

匯入的專案可能會有自己的 InitialTargets 屬性。Imported projects may have their own InitialTargets attributes. 所有的初始目標都會彙總在一起,並依序執行。All initial targets are aggregated together and run in order.

如需詳細資訊,請參閱如何:指定要優先建置的目標For more information, see How to: Specify which target to build first.

預設目標Default targets

如果未在命令列上明確指定目標,則 Project 項目的 DefaultTargets 屬性會指定要建置哪些目標。The DefaultTargets attribute of the Project element specifies which target or targets are built if a target isn't specified explicitly in a command line.

DefaultTargets 屬性的值可以是以分號分隔且已排序的預設目標清單。The value of the DefaultTargets attribute can be a semicolon-delimited, ordered list of default targets. 下列範例會指定 Clean 目標執行,然後 Build 目標執行。The following example specifies that the Clean target runs, and then the Build target runs.

<Project DefaultTargets="Clean;Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

您可以在命令列上使用 -target 參數覆寫預設目標。You can override the default targets by using the -target switch on the command line. 下列範例會指定 Build 目標執行,然後 Report 目標執行。The following example specifies that the Build target runs, and then the Report target runs. 當您以這種方式指定目標時,就會忽略任何預設目標。When you specify targets in this way, any default targets are ignored.

msbuild -target:Build;Report

如果未指定初始目標和預設目標,以及如果未指定任何命令列目標,MSBuild 就會先執行初始目標,接著執行預設目標。If both initial targets and default targets are specified, and if no command-line targets are specified, MSBuild runs the initial targets first, and then runs the default targets.

匯入的專案可能會有自己的 DefaultTargets 屬性。Imported projects may have their own DefaultTargets attributes. 第一個遇到的 DefaultTargets 屬性會判斷將執行哪些預設目標。The first DefaultTargets attribute encountered determines which default targets will run.

如需詳細資訊,請參閱如何:指定要優先建置的目標For more information, see How to: Specify which target to build first.

第一個目標First target

如果沒有初始目標、預設目標或命令列目標,則 MSBuild 會執行它在專案檔或任何匯入的專案檔中遇到的第一個目標。If there are no initial targets, default targets, or command-line targets, then MSBuild runs the first target it encounters in the project file or any imported project files.

目標相依性Target dependencies

目標可以說明彼此間的相依性關係。Targets can describe dependency relationships with each other. DependsOnTargets 屬性會指出某一個目標相依於其他目標。The DependsOnTargets attribute indicates that a target depends on other targets. 例如,套用至物件的For example,

<Target Name="Serve" DependsOnTargets="Chop;Cook" />

告知 MSBuild,Serve 目標相依於 Chop 目標和 Cook 目標。tells MSBuild that the Serve target depends on the Chop target and the Cook target. MSBuild 會執行 Chop 目標,然後在執行 Serve 目標之前先執行 Cook 目標。MSBuild runs the Chop target, and then runs the Cook target before it runs the Serve target.

BeforeTargets 和 AfterTargetsBeforeTargets and AfterTargets

在 MSBuild 4.0 中,您可以使用 BeforeTargetsAfterTargets 屬性來指定目標順序。In MSBuild 4.0, you can specify target order by using the BeforeTargets and AfterTargets attributes.

請考慮下列指令碼。Consider the following script.

<Project DefaultTargets="Compile;Link" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="Compile">
        <Message Text="Compiling" />
    </Target>
    <Target Name="Link">
        <Message Text="Linking" />
    </Target>
</Project>

若要建立中繼目標 Optimize,在 Compile 目標之後,但在 Link 目標之前執行,請 在 Project 項目中的任一處加入下列目標。To create an intermediate target Optimize that runs after the Compile target, but before the Link target, add the following target anywhere in the Project element.

<Target Name="Optimize"
    AfterTargets="Compile" BeforeTargets="Link">
    <Message Text="Optimizing" />
</Target>

判斷目標建置順序Determine the target build order

MSBuild 會以如下方式判斷目標建置順序:MSBuild determines the target build order as follows:

  1. 執行 InitialTargets 目標。InitialTargets targets are run.

  2. 執行命令列上使用 -target 參數指定的目標。Targets specified on the command line by the -target switch are run. 如果您未在命令列上指定目標,則會執行 DefaultTargets 目標。If you specify no targets on the command line, then the DefaultTargets targets are run. 如果兩者都不存在,則會執行第一個遇到的目標。If neither is present, then the first target encountered is run.

  3. 評估目標的 Condition 屬性。The Condition attribute of the target is evaluated. 如果 Condition 屬性存在且評估為 false,則不會執行目標,且不會對組建產生任何進一步的作用。If the Condition attribute is present and evaluates to false, the target isn't executed and has no further effect on the build.

    其他列出 BeforeTargetsAfterTargets 中條件式目標的目標,仍會根據指定的順序執行。Other targets that list the conditional target in BeforeTargets or AfterTargets still execute in the prescribed order.

  4. 在執行或跳過目標前,其 DependsOnTargets 目標仍會執行,除非 Condition 屬性已套用到目標,並評估為 falseBefore the target is executed or skipped, its DependsOnTargets targets are run, unless the Condition attribute is applied to the target and evaluates to false.

    Note

    若沒有執行,則會將目標視為跳過,因為其輸出項目已是最新狀態 (請參閱累加建置)。A target is considered skipped if it is not executed because its output items are up-to-date (see incremental build). 這項檢查會在於目標內部執行工作前完成,且不會影響目標的執行順序。This check is done just before executing the tasks inside target, and does not affect the order of execution of targets.

  5. 在執行或跳過目標前,任何在 BeforeTargets 屬性中列出目標的其他目標都會執行。Before the target is executed or skipped, any other target that lists the target in a BeforeTargets attribute is run.

  6. 在執行目標前,會先比較它的 Inputs 屬性和 Outputs 屬性。Before the target is executed, its Inputs attribute and Outputs attribute are compared. 如果 MSBuild 判斷有任何與一或多個對應輸入檔相關的輸出檔過時,則 MSBuild 會執行目標。If MSBuild determines that any output files are out of date with respect to the corresponding input file or files, then MSBuild executes the target. 否則,MSBuild 會略過目標。Otherwise, MSBuild skips the target.

  7. 在執行或跳過目標後,任何在 AfterTargets 屬性中列出它的其他目標都會執行。After the target is executed or skipped, any other target that lists it in an AfterTargets attribute is run.

另請參閱See also