views:

27

answers:

2

I wonder why in the following code, MsBuild refuses to set the Suffix Metadata. It does work with a CreateItem task instead of the ItemGroup Declaration (because CreateItem is computed at build time) but I can't do this here because this code is in a "property file" : the project has no target, it's just a bunch of properties/items I include in real projects.

<ItemGroup>
        <Layout Include="Bla">
            <PartnerCode>bla</PartnerCode>
        </Layout>
        <Layout Include="Bli">
            <PartnerCode>bli</PartnerCode>
        </Layout>
</ItemGroup>

<ItemGroup Condition="'$(LayoutENV)'=='Preprod'">
        <LayoutFolder Include="Preprod">
            <Destination>..\Compil\layout\pre\</Destination>
        </LayoutFolder>
</ItemGroup>


<ItemGroup>
    <Destinations Include="@(LayoutFolder)" >
        <Suffix>%(Layout.PartnerCode)</Suffix>
    </Destinations>
</ItemGroup>

Destinations is well built but the Suffix Metadata is not set.

As for now, I have duplicated the Destinations Definition in every project I needed it but it's not very clean. If someone has a better solution, I'm interested!

+1  A: 

With MSBuild 4 you can use metadata from previous items in item declaration like this :

<ItemGroup>
  <Layout Include="Bla">
      <PartnerCode>bla</PartnerCode>
  </Layout>
  <Layout Include="Bli">
      <PartnerCode>bli</PartnerCode>
  </Layout>
</ItemGroup>

<ItemGroup>
  <Destinations Include="@(Layout)" >
      <Suffix>%(PartnerCode)</Suffix>
  </Destinations>
</ItemGroup>

(It's strange that you batch on LayoutFolder and try to get Layout metadata. What value do you want as Suffix bla or bli?)

madgnome
In fact, I can. I did it several times. It's fine if you explicitly write the itemgroup name on which you want to batch like %(LAYOUT.PartnerCode).But maybe I can't batch if not in a target element...
Benjamin Baumann
If you write %(Layout.PartnerCode) in Destinations.Suffix what value do you want? bla or bli?
madgnome
Let's say Layout.Count = n. I want a collection with 2n items : all items from Layout, one time with bla and another time with bli.
Benjamin Baumann
A: 

It appears that I try to set Metadata dynamically outside a target which is impossible. I try to set the Suffix Metadata by batching over Layout items but Layout items are not properly set when the batching is done. The batching is done when msbuild parse my property files, it does not wait for Layout to be declared.

Nevertheless, like MadGnome pointed out, I can batch over LayoutFolder (which is the source items for my includes) because MSBuild does wait for it to be declared.

Benjamin Baumann