title: MSBuild merge item groups
body: <p>Need some help with this MSBuild code.</p>
  
  <p>I want to generate 4 app.config files with different settings and create 2 setup files for QA and production.</p>
  
  <p>Each setup file will have 2 physical installations (Production lines).</p>
  
  <p>So QA setup should include 2 app.configs with qa settings for production line 1 and 2, the same for production setup.</p>
  
  <p>Here is a extract of the msbuild I have so far.</p>
  
  <pre><code>&lt;ItemGroup&gt;
    &lt;BuildEnvironment Include="QA"&gt;
      &lt;Server&gt;qa-server&lt;/Server&gt;
    &lt;BuildEnvironment/&gt;
    &lt;BuildEnvironment Include="Prod"&gt;
      &lt;Server&gt;prod-server&lt;/Server&gt;
    &lt;BuildEnvironment/&gt;
  
    &lt;Line Include="1"&gt;
      &lt;Setting&gt;A&lt;/Setting&gt;
    &lt;/Line&gt;
    &lt;Line Include="2"&gt;
      &lt;Setting&gt;B&lt;/Setting&gt;
    &lt;/Line&gt;
  &lt;ItemGroup&gt;
  
  &lt;Target Name="PublishSetup" Inputs="@(BuildEnvironment)" Outputs="%(BuildEnvironment.Identity)"&gt;
    &lt;!-- Doesn't work at all --&gt;
    &lt;ItemGroup&gt;
      &lt;AppConfig Include="@(BuildEnvironment);@(Line)"&gt;
        &lt;Path&gt;$(MyOutDir)\App.Config-%(Identity)&lt;/Path&gt;
      &lt;/AppConfig&gt;
    &lt;/ItemGroup&gt;
  
    &lt;!-- Copy app.config to the four new files --&gt;
    &lt;Copy SourceFiles="$(AppConfigFile)" DestinationFiles="%(AppConfig.Path)" /&gt;    
  
    &lt;!-- Update each new app.config with XmlUpdate (community task), something like the following --&gt;
    &lt;XmlUpdate XmlFileName="%(AppConfig.Path)" XPath=".." Value="%(AppConfig.Server)" /&gt;
    &lt;XmlUpdate XmlFileName="%(AppConfig.Path)" XPath=".." Value="%(AppConfig.Setting)" /&gt;
  
    &lt;!-- Build 2 setup.exe, one for qa and one prod using a Exec-task passing in qa and prod as command line argument --&gt;
    &lt;Exec Command="setupcompiler.exe /d%(BuildEnvironment.Identity)" /&gt;
  &lt;/Target&gt;
  </code></pre>
  
  <p>The 4 resulting app.configs should be like this</p>
  
  <pre><code>app.config-QA-1
  &lt;connectionstring datasource="qa-server" ../&gt;
  &lt;applicationSetting name="aName" value="A" /&gt;
  
  app.config-QA-2
  &lt;connectionstring datasource="qa-server" ../&gt;
  &lt;applicationSetting name="aName" value="B" /&gt;
  
  app.config-Prod-1
  &lt;connectionstring datasource="prod-server" ../&gt;
  &lt;applicationSetting name="aName" value="A" /&gt;
  
  app.config-Prod-2
  &lt;connectionstring datasource="prod-server" ../&gt;
  &lt;applicationSetting name="aName" value="B" /&gt;
  </code></pre>