Maniaque
Maniaque

Reputation: 761

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?

Upvotes: 10

Views: 2203

Answers (1)

Christian.K
Christian.K

Reputation: 49290

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>

Upvotes: 22

Related Questions