0

MSBuild 批处理没有按我预期的方式工作。这是一个演示“问题”行为的 MSBuild 脚本的快速示例:

<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>

我将此脚本命名为“test.proj”并将其与其他几个子文件夹/文件一起放在一个文件夹中:

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

如果我像这样执行 msbuild msbuild .\test.proj /p:Platform=All,输出如下所示:

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

我期待/希望输出看起来像这样:

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

换句话说,我希望OutputFiles根据Message任务的批处理方式对项目组中的项目进行分组/过滤。

如何更改脚本以获得我想要的行为?我更喜欢不涉及在目标/任务区域中硬编码“平台”值的解决方案。

4

1 回答 1

2

这里是。您需要使用每个 Platform.Identity 打破 OutputFiles。我已经测试过了,这可以满足您的要求:

<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>
于 2012-02-02T22:02:53.277 回答