Last time we saw that an MSBuild file must have at least one
Target node.
But it can have as many Targets as you want…
<?xml
version=“1.0″
encoding=“utf-8″?>
<Project
xmlns=“http://schemas.microsoft.com/developer/msbuild/2003″>
<Target
Name=“Target1″>
<Message
Text=“Target
1, Message1″
/>
<Message
Text=“Target
1, Message2″
/>
</Target>
<Target
Name=“Target2″>
<Message
Text=“Target
2, Message1″
/>
<Message
Text=“Target
2, Message2″
/>
</Target>
<Target
Name=“Target3″>
<Message
Text=“Target
3, Message1″
/>
<Message
Text=“Target
3, Message2″
/>
</Target>
</Project>
Paste the
above code snippet into Notepad++ and run it. (If you don’t know how to run
an MSBuild file from Notepad++ maybe you missed my 2nd
Post.)
Here is
the output…
Build started 10/15/2012 2:01:06 PM.
Project “C:\Users\sspearman.UPWARD\Documents\MSBuild\test.build” on node 1 (default
targets).
Target1:
Target 1, Message1
Target 1, Message2
Done Building Project “C:\Users\sspearman.UPWARD\Documents\MSBuild\test.build” (default
targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.05
Notice that
MSBuild only ran the first Target. It ran it because it was first.
There are
several other ways to configure which target gets run.
1. You
can change add a DefaultTargets as a project attribute. Change the Project
tag as follows…
<Project
DefaultTargets=“Target2″
xmlns=“http://schemas.microsoft.com/developer/msbuild/2003″>
Now run
it. Notice that Target 2 is what gets run.
You can
also put multiple targets into that attribute…now change your Project node to be
like this…
<Project
DefaultTargets=“Target2;Target3″
xmlns=“http://schemas.microsoft.com/developer/msbuild/2003″>
Now when
you run it both Target2 and Target3 are run.
2.
One other way to control which Target runs is from the command line.
In Notepad++
hit F6 and edit the Command to be the following…
“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe” “$(FULL_CURRENT_PATH)”
/t:Target3
Test it and you will see that Target3 is run because of the /t switch. And
just as with the DefaultProjects, you can use this argument with a semi-colon delimited
list of Targets.