Search code examples
msbuildmsbuild-propertygroupmsbuild-target

msbuild, overwriting property value in different file


I am trying to modify property value depending on certain condition in another file. For ex. I have one file that calls target file.

<Import Project="sample.vcxproj"/>
<PropertyGroup>
    <Gender>Boy</Gender>
    <Search>UNIQUE_NAME</Search>
</PropertyGroup>

<Target Name="Build">
    <callTarget Targets="SetName"/>
    <Message Text="$(Person)"/>
</Target>

I have one file that includes item group to decide and target that modifies

<ItemGroup>
    <Name Include="UNIQUE_NAME">
        <Boy>DAVID</Boy>
        <Girl>REBECCA</Girl>
    </NAME>
</ItemGroup>

<Target Name="SetName">
    <PropertyGroup Condition="'$(Search)'=='@(Name)'">
        <Person>@(Name->'%($(Gender))')</Person>
    </PropertyGroup>
</target>

But when I print 'Person' I get empty string. And I checked that 'SetName' is called and correct name is set. What am I missing here?


Solution

  • This has to do with the accessibility of MSBuild properties, depending on whether you are using DependsOnTargets or CallTarget. When using DependsOnTargets you will have greater access to properties. That is why your example works when using that method.

    There is an existing stackoverflow article that speaks to this issue.