Question

MSBuild batching is not working the way I was expecting. Here's a quick example of an MSBuild script that demonstrates the 'problem' behavior:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <ItemGroup>
    <Platform Condition="('$(Platform)' == 'All') Or ('$(Platform)' == 'x86')" Include="x86" />
    <Platform Condition="('$(Platform)' == 'All') Or ('$(Platform)' == 'x64')" Include="x64" />
  </ItemGroup>
  <Target Name="Build">
    <ItemGroup>
      <OutputFiles Include="%(Platform.Identity)\*.txt"/>
    </ItemGroup>
    <Message Importance="high" Text="%(Platform.Identity): @(OutputFiles)" />
  </Target>
</Project>

I've named this script 'test.proj' and placed it in a folder along with a couple other subfolders/files:

.\x86\test-x86.txt
.\x64\test-x64.txt

If I execute msbuild like this msbuild .\test.proj /p:Platform=All, the output looks like this:

...
Build:
  x86: x86\test-x86.txt;x64\test-x64.txt
  x64: x86\test-x86.txt;x64\test-x64.txt
...

I was expecting/hoping the output would look like this:

...
Build:
  x86: x86\test-x86.txt
  x64: x64\test-x64.txt
...

In other words, I want the items in the OutputFiles item group to be grouped/filtered according to how the Message task is batched.

How can I change the script to get the behavior that I want? I would prefer a solution that does not involve hard-coding the 'platform' values in the target/task area.

Was it helpful?

Solution

Here it is. You need to break the OutputFiles with each Platform.Identity. I've tested, and this does what you wished:

<Project ToolsVersion="3.5" DefaultTargets="Build;" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Platform Condition="('$(Platform)' == 'All') Or ('$(Platform)' == 'x86')" Include="x86"/>
    <Platform Condition="('$(Platform)' == 'All') Or ('$(Platform)' == 'x64')" Include="x64"/>
  </ItemGroup>
  <Target Name="Build">
    <ItemGroup>
      <OutputFiles Include="%(Platform.Identity)\*.txt">
        <Flavor>%(Platform.Identity)</Flavor>
      </OutputFiles>
    </ItemGroup>
    <Message Importance="high" Text="%(OutputFiles.Flavor): %(OutputFiles.Identity)" />
  </Target>
</Project>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top