Target Element (MSBuild)
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here
Contains a set of tasks for MSBuild to execute sequentially.
<Target Name="Target Name" Inputs="Inputs" Outputs="Outputs" Returns="Returns" KeepDuplicateOutputs="true/false" BeforeTargets="Targets" AfterTargets="Targets" DependsOnTargets="DependentTarget" Condition="'String A' == 'String B'"> Label="Label"> <Task>... </Task> <PropertyGroup>… </PropertyGroup> <ItemGroup>… </ItemGroup> <OnError... /> </Target>
Attributes and Elements
The following sections describe attributes, child elements, and parent elements.
The name of the target.
The condition to be evaluated. If the condition evaluates to
The files that form inputs into this target. Multiple files are separated by semicolons. The timestamps of the files will be compared with the timestamps of files in
The files that form outputs into this target. Multiple files are separated by semicolons. The timestamps of the files will be compared with the timestamps of files in
The set of items that will be made available to tasks that invoke this target, for example, MSBuild tasks. Multiple targets are separated by semicolons. If the targets in the file have no
||Optional Boolean attribute.
A semicolon-separated list of target names. When specified, indicates that this target should run before the specified target or targets. This lets the project author extend an existing set of targets without modifying them directly. For more information, see Target Build Order.
A semicolon-separated list of target names. When specified, indicates that this target should run after the specified target or targets. This lets the project author extend an existing set of targets without modifying them directly. For more information, see Target Build Order.
The targets that must be executed before this target can be executed or top-level dependency analysis can occur. Multiple targets are separated by semicolons.
An identifier that can identify or order system and user elements.
|Task||Creates and executes an instance of an MSBuild task. There may be zero or more tasks in a target.|
|PropertyGroup||Contains a set of user-defined
|ItemGroup||Contains a set of user-defined
|OnError||Causes one or more targets to execute if the
For information about the
|Project||Required root element of an MSBuild project file.|
The first target to execute is specified at run time. Targets can have dependencies on other targets. For example, a target for deployment depends on a target for compilation. The MSBuild engine executes dependencies in the order in which they appear in the
DependsOnTargets attribute, from left to right. For more information, see Targets.
A target is only executed once during a build, even if more than one target has a dependency on it.
If a target is skipped because its
Condition attribute evaluates to
false, it can still be executed if it is invoked later in the build and its
Condition attribute evaluates to
true at that time.
Before MSBuild 4,
Target returned any items that were specified in the
Outputs attribute. To do this, MSBuild had to record these items in case tasks later in the build requested them. Because there was no way to indicate which targets had outputs that callers would require, MSBuild accumulated all items from all
Outputs on all invoked
Targets. This lead to scaling problems for builds that had a large number of output items.
If the user specifies a
Returns on any
Target element in a project, then only those
Targets that have a
Returns attribute record those items.
Target may contain both an
Outputs attribute and a
Outputs is used with
Inputs to determine whether the target is up-to-date.
Returns, if present, overrides the value of
Outputs to determine which items are returned to callers. If
Returns is not present, then
Outputs will be made available to callers except in the case described earlier.
Before MSBuild 4, any time that a
Target included multiple references to the same item in its
Outputs, those duplicate items would be recorded. In very large builds that had a large number of outputs and many project interdependencies, this would cause a large amount of memory to be wasted because the duplicate items were not of any use. When the
KeepDuplicateOutputs attribute is set to
true, these duplicates are recorded.
The following code example shows a
Target element that executes the
<Target Name="Compile" DependsOnTargets="Resources" Returns="$(TargetPath)"> <Csc Sources="@(CSFile)" TargetType="library" Resources="@(CompiledResources)" EmitDebugInformation="$(includeDebugInformation)" References="@(Reference)" DebugType="$(debuggingType)" > <Output TaskParameter="OutputAssembly" ItemName="FinalAssemblyName" /> </Csc> </Target>