目标生成顺序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 attribute 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

ProjectDefaultTargets 元素的 InitialTargets 属性指定将首先运行的目标,即使在命令行或 属性中指定了目标。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 目标,然后运行 Cook 目标,之后再运行 Serve 目标。MSBuild runs the Chop target, and then runs the Cook target before it runs the Serve target.

在目标之前和在目标之后BeforeTargets 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>

若要创建在 Compile 目标后,Link 目标前运行的直接目标 Optimize,请将以下目标添加到 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.

    备注

    如果目标未执行,则会将其视为已跳过,因为其输出项是最新的(请参阅增量生成)。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