Specify which MSBuild target to build first
MSBuild project files can contain one or more targets that define how the project is built. MSBuild builds the first target it finds, and any dependencies, unless:
- The
Project
element contains anInitialTargets
attribute. - The
Project
element contains aDefaultTargets
attribute. - The MSBuild command has a
-target
switch that specifies a target.
Use the InitialTargets attribute
The InitialTargets
attribute of the Project
element specifies the initial target that runs first. It overrides any targets that you specify with MSBuild on the command line or in the DefaultTargets
attribute of the Project
element.
Specify a single initial target
Specify the default target in the InitialTargets
attribute of the Project
element. For example:
<Project InitialTargets="Clean">
Specify multiple initial targets
You can specify more than one initial target in the InitialTargets
attribute of the Project
element. List the targets in order and use a semicolon to separate each target. The targets in the list are run sequentially.
For example, to run the Clean
target and then the Compile
target, enter:
<Project InitialTargets="Clean;Compile">
Use the DefaultTargets attribute
The DefaultTargets
attribute of the Project
element specifies which target or targets are built if a target isn't specified explicitly on the command line.
If targets are specified in both the InitialTargets
and DefaultTargets
attributes of the Project
element, and no target is specified on the command line, MSBuild runs the targets specified in the InitialTargets
attribute followed by the targets specified in the DefaultTargets
attribute.
Specify a single default target
Specify the default target in the DefaultTargets
attribute of the Project
element. For example:
<Project DefaultTargets="Compile">`
Specify multiple default targets
You can specify more than one default target in the DefaultTargets
attribute of the Project
element. List the default targets in order and use a semicolon to separate each target. The targets in the list are run sequentially.
For example, to run the Clean
target and then the Compile
target, enter:
<Project DefaultTargets="Clean;Compile">
Use the -target switch to override the default target
If a default target isn't defined in the project file, or if you don't want to use the defined default target, you can use the command line switch -target
to specify a different target. The target or targets specified with the -target
switch are run instead of the targets specified by the DefaultTargets
attribute of the Project
element. Targets specified in the InitialTargets
attribute always run first.
Override the default target with a single target
Specify the target to use as the first target by using the -target
command-line switch with a colon (:) and the name of the target. For example:
msbuild file.proj -target:Clean
Override the default target with multiple targets
Specify a list of targets to use as the first targets and separate them by semicolons with the -target
command-line switch. For example:
msbuild <file name>.proj -t:Clean;Compile