'How do you call an overridden MSBuild target

In MSBuild you can override a <Target /> from another file in your own. For example the AfterBuild target included in Microsoft.Common.targets file simply by defining your own Target with the same name:

<Target Name="AfterBuild">
    <!-- Do something different -->
</TargetName>

You'll see a note like this:

Overriding target "AfterBuild" in project "C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets" with target "AfterBuild" from project "XXXXX".

Is there any way to call the original AfterBuild target?

I'd like to do this to instrument certain complex default Targets and then execute the original behavior. Many targets like Build expose a BuildDependsOn property that can be used for this. Many others do not - and I'd like to override them without completely duplicating their content.



Solution 1:[1]

I'd like to do this to instrument certain complex default Targets and then execute the original behavior. Many targets like Build expose a BuildDependsOn property that can be used for this. Many others do not - and I'd like to override them without completely duplicating their content.

If you want to first run something custom on a target and then run the original target, why not just use BeforeTargets?

So instead of

<Target Name="AfterBuild">
    <!-- custom AfterBuild overriding default AfterBuild -->
</TargetName>

just use

<Target Name="JustBefore_AfterBuild" BeforeTargets="AfterBuild">
    <!-- custom AfterBuild just before default AfterBuild -->
</TargetName>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kjara