Stop msbuild process if a target fails - msbuild

Let's say I have three targets A, B and C.
C depends on B.
B depends on A.
If I run msbuild /t:C mybuildfile.xml, it will execute target A, B and C in order.
How do I set up to make sure C and B won't get executed if there is anything failed in A?

<Target Name="StopBuild">
<Message Text="An error has occurred, build stopped." />
</Target>
<Target Name="A">
<OnError ExecuteTargets="StopBuild"/>
</Target>
<Target Name="B" DependsOnTargets="A">
</Target>
Ok, I figured out this by myself. Use the code above, if target A fails, it will go to StopBuild specified in OnError task. For more on how msbuild handles errors, go to
http://en.csharp-online.net/MSBuild:_By_Example%E2%80%94Dealing_with_MSBuild_Errors

Related

Proper use of msbuild inputs and outputs with Targets

We have an in house compiler and linker tool that we're trying to make MSBUILD compatible (i.e. proper behavior in build/incremental builds/rebuild/clean scenarios)
In the first step we actually call CL task to preprocess our files. The issue is I can't seem to figure out how to set up the tasks properly to do that so it detects if the output is deleted or if one of the inputs is modified.
Second step is call our Compiler with it's proper parameters.
Third step is to call our Linker with it's proper parameters.
I think once step one works making step two and three will be simple; I'm stuck on step one. Example code below. The main MFT contains "#includes" which reference all the other MFT files named in _MFTFiles - so we only need to process the main file; but we need to monitor them all so if we change them incremental builds work properly. If anyone has any idea I'd love to hear it. I have the MSBUILD book and of course scoured here but I don't see an example of what I'm trying to accomplish.
Thanks in advance.
<ItemGroup Label="_MainMFT">
<MainMFT Include="MFTSystem.MFT"/>
</ItemGroup>
<ItemGroup Label="_MFTFiles">
<MFTFiles Include="MFTbject.MFT;DebuggerSupport.MFT;enumerations.MFT;collections.MFT;DataStream.MFT;Version.MFT"/>
</ItemGroup>
<Target Name="_PreprocessFiles"
BeforeTargets="Build"
DependsOnTargets=""
Inputs="#(MFTFiles)"
Outputs="#(MFTFiles->'%(Filename).MFTpp')">
<Message Text="PlatformToolsetVersion is $(PlatformToolsetVersion)" Importance="high"/>
<CL Sources="#(MainMFT)" PreprocessorDefinitions="_DEBUG;EL_DIAG_ENABLED" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" PreprocessToFile="true" PreprocessOutputPath="$(ProjectDir)%(Filename).MFTpp" />
<CL Sources="#(MainMFT)" PreprocessorDefinitions="" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" PreprocessToFile="true" PreprocessOutputPath="$(ProjectDir)%(Filename).MFTpp"/>
</Target>
<Target Name="_ObjectCompiler" AfterTargets="_PreprocessFiles;Build">
<Message Text="Calling ObjectCompiler...." Importance="high"/>
</Target>
<Target Name="_ObjectLinker" AfterTargets="_ObjectCompiler;Link">
<Message Text="Calling ObjectLinker...." Importance="high"/>
</Target>

Can I be sure that i receive correct item if I defined item in one msbuild target and get it in other one?

I have two different targets which are invoked with one collection of properties via msbuild task. In one target I define ItemGroup and in other one I recieve it. I invoke targets in the next way:
<MsBuild Projects="deploypkg.project" Properties="CurrentSite=%(SitesName.Identity)" Targets="TargetA"/>
<MsBuild Projects="deploypkg.project" Properties="CurrentSite=%(SitesName.Identity)" Targets="TargetB"/>
When in TargetB I refer defined in TargetA ItemGroup i get items defined only for current site(input property). It's exactly what i need but I'm not sure I can rely on it, because I found nothing about this possibility.
You are not sure, you can create test.project to test it. Something like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Target Name="TargetA">
<Message Text="TargetA: CurrentSite = $(CurrentSite)"/>
</Target>
<Target Name="TargetB">
<Message Text="TargetB: CurrentSite = $(CurrentSite)"/>
</Target>
</Project>
Use test.project in your script:
<MsBuild Projects="test.project" Properties="CurrentSite=%(SitesName.Identity)" Targets="TargetA"/>
<MsBuild Projects="test.project" Properties="CurrentSite=%(SitesName.Identity)" Targets="TargetB"/>

What is the difference between 'DependsOnTargets' and 'AfterTargets'?

What is the difference between DependsOnTargets and AfterTargets?
I can not distinguish these two.
DependsOnTargets
Defines the targets that must be executed before the target can be executed.
<Target Name="DependsOn" DependsOnTargets="DependencyTarget1;DependencyTarget2">
<Message Text="Target : DependsOn"/>
</Target>
<Target Name="DependencyTarget2">
<Message Text="Target : DependencyTarget2"/>
</Target>
<Target Name="DependencyTarget1">
<Message Text="Target : DependencyTarget1"/>
</Target>
Output
> Target : DependencyTarget1
> Target : DependencyTarget2
> Target : DependsOn
BeforeTargets and AfterTargets (Only available in MSBuild 4)
Indicates that the target should run before or after the specified target or targets.
<Target Name="BeforeAndAfter">
<Message Text="Target : BeforeAndAfter"/>
</Target>
<!-- BeforeTarget1 will run BEFORE target "BeforeAndAfter" -->
<Target Name="BeforeTarget" BeforeTargets="BeforeAndAfter">
<Message Text="BeforeTarget run before : BeforeAndAfter"/>
</Target>
<!-- BeforeTarget1 will run AFTER target "BeforeAndAfter" -->
<Target Name="AfterTarget" AfterTargets="BeforeAndAfter">
<Message Text="AfterTarget run after : BeforeAndAfter"/>
</Target>
Output
> BeforeTarget run before : BeforeAndAfter
> Target : BeforeAndAfter
> AfterTarget run after : BeforeAndAfter
If you have multiples targets that should run before or after the same specified target, they will be executed in declaration order :
<Target Name="BeforeAndAfter">
<Message Text="Target : BeforeAndAfter"/>
</Target>
<!--
BOTH BeforeTarget1 and BeforeTarget2 should run before target "BeforeAndAfter"
-->
<Target Name="BeforeTarget1" BeforeTargets="BeforeAndAfter">
<Message Text="BeforeTarget1 run before : BeforeAndAfter"/>
</Target>
<Target Name="BeforeTarget2" BeforeTargets="BeforeAndAfter">
<Message Text="BeforeTarget2 run before : BeforeAndAfter"/>
</Target>
BeforeTargets and AfterTargets could be use to extend existing build process.
For example, with this attributes you can easily execute a target before CoreCompile (defines in Microsoft.CSharp.targets). Without that you'll have to override the property CoreCompileDependsOn.
Without AfterTargets you have no way to easily execute a target after another one if no extension point is defined (CallTarget at the end of the target with a property that you can override)
DependsOnTargets, BeforeTargets and AfterTargets execution order?
When DependsOnTargets, BeforeTargets and AfterTargets are used on the same target, the order of execution is :
DependsOnTargets
BeforeTargets
The target
AfterTargets
<Target Name="MainTarget" DependsOnTargets="DefaultDependsOn">
<Message Text="Target : MainTarget"/>
</Target>
<Target Name="DefaultDependsOn">
<Message Text="Target : DefaultDependsOn"/>
</Target>
<Target Name="DefaultBeforeTarget" BeforeTargets="MainTarget">
<Message Text="Target : DefaultBeforeTarget"/>
</Target>
<Target Name="DefaultAfterTarget" AfterTargets="MainTarget">
<Message Text="Target : DefaultAfterTarget"/>
</Target>
Output
> Target : DefaultDependsOn
> Target : DefaultBeforeTarget
> Target : MainTarget
> Target : DefaultAfterTarget
More succinctly from this GitHub issue on Microsoft Docs:
<Target Name="x" DependsOnTargets="y" /> means:
If something wants to run x, y must run first.
<Target Name="a" AfterTargets="b" /> means:
If something runs b, then run a after it.
While the other answers previously provided are correct, I think they failed to mention what I think is the primary benefit of AfterTargets over DependsOnTargets.
DependsOnTargets has been around from the beginning of MSBuild. The problem with DependsOnTargets, is that it requires a target author to explicitly allow for extensibility. This is done by defining a property that is used as the DependsOnTargets value, as follows:
<PropertyGroup>
<SomeTargetDependsOnTargets>
Dependency1;Dependency2
</SomeTargetDependsOnTargets>
</PropertyGroup>
<Target Name="SomeTarget" DependsOnTargets="$(SomeTargetDependsOnTargets)">
...
</Target>
You could then add a dependency by modifying the SomeTargetDependsOnTargets property as follows:
<SomeTargetDependsOnTargets>
$(SomeTargetDependsOnTargets);Dependency3
</SomeTargetDependsOnTargets>
The problem with this design, is that if the author had simply inlined Dependency1;Dependency2 rather than extracting it into a property, there would be no way to externally modify it to allow for customization.
AfterTargets, on the other hand, doesn't require the original target author to have explicitly extracted the DependsOnTargets value into a property to allow for extensibility.
I think the answer is much simpler. The effect of DependsOnTargets and AfterTargets is essentially the same. The reason for BeforeTargets & AfterTargets (from the Microsoft Documentation):
This lets the project author extend an existing set of targets without
modifying them directly.
So if you have an existing target B and you want to add a new target A that must execute first, you have two choices:
Modify target B to read: DependsOnTargets="A".
Modify target A to read: BeforeTargets="B".
If you can't modify B (e.g. it's an existing Microsoft target), that's when you need BeforeTargets.
One more difference, that is mentioned in another answer is that
BeforeTargets and AfterTargets are run regardless of the Condition whereas DependsOnTargets (and target itself) are skipped when Condition evaluates to false"
In below code second target is executed even though first target is not executed:
<Target Name="FirstTarget" AfterTargets="PostBuildEvent" Condition="'False'">
<Message Text="This target is never executed" />
</Target>
<Target Name="SecondTarget" AfterTargets="FirstTarget">
<Message Text="SecondTarget is executed" />
</Target>
In below code second target is not executed:
<Target Name="FirstTarget" AfterTargets="PostBuildEvent" Condition="'False'">
<Message Text="This target is never executed" />
</Target>
<Target Name="SecondTarget" DependsOnTargets="FirstTarget">
<Message Text="SecondTarget is executed" />
</Target>
DependsOnTarget
Let's assume that you have two tasks:
Build Project
Copy all content.
You can start your build by executing task 2 and then in the task declaration define its dependencies. So if you define that task 2 depends on task 1, the build process will start and execute task 1 and then 2.
AfterTargets
Much simpler: it means only tasks which are execute after other targets. So taking the example from above - after Task 1 - build project execute task 2.
I hope this helps
One more difference between AfterTargets and DependsOnTargets.
When you have a target dependency chain/graph, be aware that putting 1+ targets into BeforeTargets or AfterTargets works like "any", and not like "all".
It works like saying "if any of them has run...", and not "if all of them has run..."
Let's say you want to achive this target dependency chain: A1 -> A2 -> A3.
The following will not work:
<Target Name="A1" BeforeTargets="Compile">
<Message Text="A1" />
</Target>
<Target Name="A3" AfterTargets="A1;A2">
<Message Text="A3" />
</Target>
<Target Name="A2" AfterTargets="A1">
<Message Text="A2" />
</Target>
Output will be:
A1
A3
A2
Because, as already mentioned here, the above only means:
 
<Target Name="A1" BeforeTargets="Compile">
If Compile wants to run, run A1 before it.
 
<Target Name="A3" AfterTargets="A1;A2">
If A1 run, run A3 after it.
If A2 run, run A3 after it.
 
<Target Name="A2" AfterTargets="A1">
If A1 run, run A2 after it.
 
Thus, running A1 will trigger running A3.
Note, that the declaration order matters!
We wanted to say A3 depends on A1 and A2, but the above did not mean that.
Viewing from another aspect, declaring this:
<Target Name="A3" AfterTargets="A1;A2">
Works like "if any of the AfterTargets has run, run me."
If A1 or A2 run, run A3
 
To safely achive target dependency chain A1 -> A2 -> A3, I would use DependsOnTargets this way:
<Target Name="B1" BeforeTargets="Compile">
<Message Text="B1" />
</Target>
<Target Name="B3" DependsOnTargets="B1;B2" BeforeTargets="Compile">
<Message Text="B3" />
</Target>
<Target Name="B2" DependsOnTargets="B1" BeforeTargets="Compile">
<Message Text="B2" />
</Target>
Declaring all the dependencies of a Target in DependsOnTargets creates the graph/chain we wanted.
Note, that we need to hook in all the targets into the build here in a plus step, because DependsOnTargets does not do that (unlike AfterTargets). (The above code uses BeforeTargets="Compile" in all Targets to achive this)
However, since we declared the dependencies between our Targets, the declaration order (and thus the hook-in order) does not matter.
There are two mainly differences:
First: Targets specified in the dependsOnTargets attribute are
executed before those in AfterTargets.
Second: If one of the targets specified in AfterTargets attribute got executed
this target get also executed after(I mean the target
where you specify the AfterTargets attribute), which is not true for
those in DependsOnTargets.

All Targets Not Being Called (nested Targets not being executed)

I am using a two TARGET files. On one TARGET file I call a TARGET that is inside the second TARGET file. This second TARGET then calls another TARGET that has 6 other TARGET calls, which do a number of different things (in addition to calling other nested TARGETS (but inside the same TARGET file)). The problem is that, on the TARGET where I call 6 TARGETS, only the first one is being executed. The program doesnt find its way to call the 2nd, 3rd, 4th, 5th, and 6th TARGET. Is there a limit to the number of nested TARGETS that can be called and run? Nothing is failing. The problem is the other TARGET calls are not running. Thanks for any help you can provide.
There is no limit to the number of targets nested. Have you tried running msbuild with all the log to see why the targets are not called :
msbuild [project.file] /verbosity:detailed
I think this is due to unfulfilled condition (Condition attribute on target), unchanged input (Input attribute on target) or you are trying to call the same target multiples times.
Invoke the same target multiple times
Using MSBuild task :
<!-- The target we want to execute multiple times -->
<Target Name="VeryUsefulOne">
<Message Text="Call VeryUsefulOne Target"/>
</Target>
<Target Name="One">
<Message Text="One"/>
<MSBuild Targets="VeryUsefulOne"
Properties="stage=one"
Projects="$(MSBuildProjectFile)"/>
</Target>
<Target Name="Two">
<Message Text="Two"/>
<MSBuild Targets="VeryUsefulOne"
Properties="stage=two"
Projects="$(MSBuildProjectFile)"/>
</Target>
<Target Name="OneTwo">
<CallTarget Targets="One;Two"/>
</Target>
It's important to change Properties value between call.

Is it possible to refer to metadata of the target from within the target implementation in MSBuild?

My msbuild targets file contains the following section:
<ItemGroup>
<Targets Include="T1">
<Project>A\B.sln"</Project>
<DependsOnTargets>The targets T1 depends on</DependsOnTargets>
</Targets>
<Targets Include="T2">
<Project>C\D.csproj"</Project>
<DependsOnTargets>The targets T2 depends on</DependsOnTargets>
</Targets>
...
</ItemGroup>
<Target Name="T1" DependsOnTargets="The targets T1 depends on">
<MSBuild Projects="A\B.sln" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="T2" DependsOnTargets="The targets T2 depends on">
<MSBuild Projects="C\D.csproj" Properties="Configuration=$(Configuration)" />
</Target>
As you can see, A\B.sln appears twice:
As Project metadata of T1 in the ItemGroup section.
In the Target statement itself passed to the MSBuild task.
I am wondering whether I can remove the second instance and replace it with the reference to the Project metadata of the target, which name is given to the Target task?
Exactly the same question is asked for the (Targets.DependsOnTargets) metadata. It is mentioned twice much like the %(Targets.Project) metadata.
Thanks.
EDIT:
I should probably describe the constraints, which must be satisfied by the solution:
I want to be able to build individual projects with ease. Today I can simply execute msbuild file.proj /t:T1 to build the T1 target and I wish to keep this ability.
I wish to emphasize, that some projects depend on others, so the DependsOnTargets attribute is really necessary for them.
Target names must be fixed values, so what you have here wouldn't work.
Also I would recommend not using Batching Expressions inside of the DependsOnTargets expression as well. This could lead to strange behavior if you do not fully understand what is happening.
In your case you may be able to just create a "driver" target which uses those items to perform the build. The only difficult part would be the DependsOnTargets that you are trying to perform. I'm not sure about the details on what you are trying to do with that so cannot make any suggestions but as for the other take a look at creating a target similar to.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Targets Include="T1">
<Project>A\B.sln"</Project>
<DependsOnTargets>The targets T1 depends on</DependsOnTargets>
</Targets> <Targets Include="T2">
<Project>C\D.csproj"</Project>
<DependsOnTargets>The targets T2 depends on</DependsOnTargets>
</Targets> ...
</ItemGroup>
<Target Name="Build">
<!--
This will be executed once for every unique value of Project in the
Target item group
-->
<MSBuild Projects="%(Targets.Project)"
Properties="Configuration=$(Configuration)"
</Target>
</Project>