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 are run.

  • Initial targets

  • Default targets

  • First target

  • Target dependencies

  • BeforeTargets and AfterTargets (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.

Targets may have a Condition attribute. If the specified condition evaluates to false, the target is not executed and has no effect on the build. For more information about conditions, see MSBuild Conditions.

Initial Targets

The Project element has an optional InitialTargets attribute whose value can be a semicolon-delimited ordered list of targets. For example,

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

specifies that the Warm target runs, and then the Eject target runs.

Imported projects may have their own InitialTargets attributes. All initial targets are aggregated together and run in order. Initial targets are typically used for error checking.

Initial targets can be overridden by using the command line. For example,

msbuild /target:Build;Report

specifies that the Build target runs, and then the Report target runs. When targets are specified this way, any initial targets are ignored.

For more information, see How to: Specify Which Target to Build First.

Default Targets

The Project element also has an optional DefaultTargets attribute whose value can be a semicolon-delimited ordered list of default targets. For example,

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

specifies that the Clean target runs, and then the Build target runs.

Imported projects may have their own DefaultTargets attributes. The first DefaultTargets attribute encountered determines which default targets will run.

Default targets can be overridden by using the command line. For example,

msbuild /target:Build;Report

specifies that the Build target runs, and then the Report target runs. When targets are specified this way, any default targets are ignored.

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.

First Target

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. The DependsOnTargets attribute indicates that a target depends on other targets. For example,

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

tells MSBuild that the Serve target depends on the Chop target and the Cook target. MSBuild runs the Chop target, and then runs the Cook target before it runs the Serve target.

BeforeTargets and AfterTargets

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="https://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="Compile">
        <Message Text="Compiling" />
    </Target>
    <Target Name="Link">
        <Message Text="Linking" />
    </Target>
</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>

Determining the Target Build Order

MSBuild determines the target build order as follows:

  1. Targets specified on the command line by the /target switch are run. If no targets are specified, InitialTargets targets are run, followed by DefaultTargets targets. If neither are present, then the first target encountered is run.

  2. The Condition attribute of the target is evaluated. If the Condition attribute is present and evaluates to false, the target is not executed and has no further affect on the build.

  3. Before a target is executed, its DependsOnTargets targets are run.

  4. Before a target is executed, any target that lists it in a BeforeTargets attribute is run.

  5. Before a target is executed, its Inputs attribute and Outputs attribute are compared. 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. Otherwise, MSBuild skips the target.

  6. After a target is executed or skipped, any target that lists it in an AfterTargets attribute is run.

See Also

Concepts

MSBuild Targets