'MSBuild: How to conditionally use one of two targets with same name?

I've seen some answers to a similar question but not exactly the same and not with positive luck trying to use suggested solutions... so I'm trying something like this:

<project>
    <target name="foo" Condition="'$(Configuration)' == 'Debug' ">
        <Message Text="=== RUNNING FOO DEBUG TARGET ===" />
    </target>
    <target name="foo" Condition="'$(Configuration)' == 'Release' ">
        <Message Text="=== RUNNING FOO RELEASE TARGET ===" />
    </target>
</project>

... but I'm finding that it doesn't appear to be possible to have two targets with the same name working properly under these conditions. One will negate the other.

How can I do this?



Solution 1:[1]

Provide a wrapper target, that depends on both targets. The both will be called, but only the one matching the condition will actually do something.

<Project>
    <Target Name="foo" DependsOnTargets="_fooDebug;_fooRelease"/>

    <Target Name="_fooDebug" Condition="'$(Configuration)' == 'Debug' ">
        <Message Text="=== RUNNING FOO DEBUG TARGET ===" />
    </Target>
    <Target Name="_fooRelease" Condition="'$(Configuration)' == 'Release' ">
        <Message Text="=== RUNNING FOO RELEASE TARGET ===" />
    </Target>
</Project>

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 Christian.K