002 – Find matching Items and Properties


Problem Statement

How can a property be built, based on appending some values whose properties are in an item list.

Solutions

1. Without target batching

<Project ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ExtPath>Hello</ExtPath>
    <PalPath>$(ExtPath)\PAL\v6.0</PalPath>
    <MsiPath>$(ExtPath)\MSI\v3.0</MsiPath>
    <WsePath>$(ExtPath)\WSE20\SP3</WsePath>
    <MmcPath>$(ExtPath)\MMC\v3.0</MmcPath>
    <MyConstants>Start</MyConstants>
  </PropertyGroup>
  <ItemGroup>
    <!– Note that these properties match the names of some items –>
    <ConstantsProperty Include="MsiPath" />
    <ConstantsProperty Include="WsePath" />
  </ItemGroup>
  <Target Name="BeforeBuild">
    <!– Create a new item based on properties which match item names –>
    <CreateItem Include="%(Identity)=$(%(ConstantsProperty.Identity))">
      <Output TaskParameter="Include" ItemName="TempMyConstants"/>
    </CreateItem>
    <PropertyGroup>
      <!– Update the existing property –>
      <MyConstants>$(MyConstants);@(TempMyConstants)</MyConstants>
    </PropertyGroup>
    <Message Text="BeforeBuild: MyConstants = $(MyConstants)" />
  </Target>
</Project>

2. Use Target Batching, overly complex, but it works.

<Project ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ExtPath>Hello</ExtPath>
        <PalPath>$(ExtPath)\PAL\v6.0</PalPath>
        <MsiPath>$(ExtPath)\MSI\v3.0</MsiPath>
        <WsePath>$(ExtPath)\WSE20\SP3</WsePath>
        <MmcPath>$(ExtPath)\MMC\v3.0</MmcPath>
        <MyConstants>Start</MyConstants>
    </PropertyGroup>
    <ItemGroup>
        <ConstantsProperty Include="MsiPath" />
        <ConstantsProperty Include="WsePath" />
    </ItemGroup>
    <!– 1. Use DependsOnTarget to avoid any potential scoping bugs with CallTarget –>
    <Target Name="BeforeBuild" DependsOnTargets="List">
        <!– 5. Redefine the property –>
        <PropertyGroup>
            <MyConstants>$(MyConstants);@(TempItem)</MyConstants>
        </PropertyGroup>
        <!– 6. Display the result –>
        <Message Text="BeforeBuild: MyConstants = $(MyConstants)" />
    </Target>
    <!– 2. Use Target Batching to iterate through the defined ConstantsProperty Item Collection –>
    <Target Name="List" Inputs="@(ConstantsProperty)" Outputs="%(Identity)">
        <!– 3. Define a new property to hold the calculated string –>
        <PropertyGroup>
            <MyNewConstants>%(Identity)=$(%(ConstantsProperty.Identity))</MyNewConstants>
        </PropertyGroup>
        <!– 4. Append the new property to a new Item Collection–>
        <ItemGroup>
            <TempItem Include="$(MyNewConstants)"/>
        </ItemGroup>
    </Target>
</Project>

Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s