0

我有以下 MSBuild .proj 文件内容:

<ItemGroup>
  <Exclude Include="*2.*"></Exclude>
</ItemGroup>

<ItemGroup>
  <!-- I have 3 files in the current directory: File1.cpp, File2.cpp and File3.cpp -->
  <ModuleInclude Include="*.cpp" Exclude="@(Exclude)"></ModuleInclude>

  <!-- I have 3 files in the Subfolder directory: eFile1.h, eFile2.h and eFile3.h -->
  <ModuleInclude Include="Subfolder\*.h" Exclude="@(Exclude->'Subfolder\%(identity)')"></ModuleInclude>
</ItemGroup>

<Target Name="Default">
  <Message Text="ModuleIncludes: %(ModuleInclude.identity)" />
  <Message Text="Excluded Items: @(Exclude)" />
  <Message Text="Excluded Subfolder Items: @(Exclude->'Subfolder\%(identity)')" />
</Target>

我看到以下输出:

ModuleIncludes: File1.cpp
ModuleIncludes: File3.cpp
ModuleIncludes: Subfolder\eFile1.h
ModuleIncludes: Subfolder\eFile2.h
ModuleIncludes: Subfolder\eFile3.h
Excluded Items: File2.cpp
Excluded Subfolder Items: Subfolder\File2.cpp

我真正需要的是包含以下子文件夹文件

ModuleIncludes: Subfolder\eFile1.h
ModuleIncludes: Subfolder\eFile3.h

因此,排除的子文件夹项目应为:

Excluded Subfolder Items: Subfolder\eFile2.h

为了能够得到这样的输出,我需要表达式

Subfolder\*2.*

我正在使用的语法

@(Exclude->'Subfolder\%(identity)')

没有给我我需要的东西。正确的语法是什么?或者这是不可能的?

4

1 回答 1

0

如果要将通配符作为文本处理,请使用属性而不是项目:

<PropertyGroup>
  <FileExcludes>*2.*</FileExcludes>
</PropertyGroup>
<ItemGroup>
  <ModuleInclude Include="*.cpp" Exclude="$(FileExcludes)"></ModuleInclude>
  <ModuleInclude Include="Subfolder\*.h" Exclude="Subfolder\$(FileExcludes)"></ModuleInclude>
</ItemGroup>

您甚至可以在此处使用将匹配的排除模式,而不管子文件夹如何:

<PropertyGroup>
  <FileExcludes>**\*2.*</FileExcludes>
</PropertyGroup>
<ItemGroup>
  <ModuleInclude Include="*.cpp" Exclude="$(FileExcludes)"></ModuleInclude>
  <ModuleInclude Include="Subfolder\*.h" Exclude="$(FileExcludes)"></ModuleInclude>
</ItemGroup>

如果您确实需要将其作为列表来添加非本地文件夹,请使用元数据项:

<PropertyGroup>
  <FileExclude Include="2">
    <Pattern>**\*2.*</Pattern>
  </FileExclude>
</PropertyGroup>
<ItemGroup>
  <ModuleInclude Include="*.cpp" Exclude="@(FileExclude)"></ModuleInclude>
  <ModuleInclude Include="..\shared-folder\*.h" Exclude="@(FileExclude->'shared-folder\%(Pattern)')"></ModuleInclude>
</ItemGroup>
于 2018-08-05T08:38:09.860 回答