Is there a way to check the existence of a target so that we can call it only when it exists?
Asked
Active
Viewed 2,764 times
1 Answers
4
You can make a Target depend on a other Target
...
<Target Name="Two" DependsOnTargets="One">
<Message Text="... comes Two." />
</Target>
<Target Name="One">
<Message Text="After One ..." />
</Target>
...
Your recent posts show that you try heavily to rely on calling Targets which is - as far as i understand it - against the philosophy of MSBuild.
Try rather to model dependencies between your Targets.

Filburt
- 17,626
- 12
- 64
- 115
-
And what if Target 'Two' is a pre-existing target? I.e. One you didn't write. – Russell Horwood Sep 09 '16 at 15:34
-
@NineTails Dependency should always work - unless you try to depend on a non-existing target. – Filburt Sep 10 '16 at 00:02
-
What I mean is, say I want the 'Build' task to depend on my target. I can't edit the 'Build' task and give it the 'DependsonTargets' attribute, and there appears to be no equivalent inverse attribute, E.g. 'DependsOnThisTarget'. So how can you make target 'One' always run before target 'Two' without editing target 'Two'? – Russell Horwood Sep 10 '16 at 07:41
-
@NineTails [This answer to "What happened to BeforeBuild and other targets in VS2012"](http://stackoverflow.com/a/13785069/205233) should solve your problem. I didn't use MSBuild some time and wasn't aware of `BeforeTargets` and `AfterTargets` but a quick searched turned these out. – Filburt Sep 11 '16 at 00:16
-
@Filburt unfortunately not. If target 'One' has attribute 'BeforeTargets="TargetTwo"' then if you call 'TargetTwo' then 'TargetOne' will not execute, if you call 'TargetOne' then 'TargetOne' will execute then 'TargetTwo'. I'm looking for a way to call 'TargetTwo' and always have 'TargetOne' run before it just like it would if I could set 'TargetTwo DependsOnTargets="TargetOne"'. I don't believe such a thing is possible with MSBuild. – Russell Horwood Sep 11 '16 at 08:37
-
@NineTails I tried a even simpler solution: Just call MSBuild with the targets in the desired order: `MSBuild.exe Test.proj /target:One;Two` - no modifications needed to your Targets. If this still doesn't solve your issue, you should post a new question. – Filburt Sep 11 '16 at 09:05