16

我正在尝试使用 MSBuild 从文本文件中读取文件列表,然后执行递归复制,将这些目录文件的内容复制到某个暂存区域,同时排除某些扩展名(例如 .tmp 文件)

我已经设法使用 CreateItem 和 MSBuild 复制任务很容易地完成了上述大部分工作,无论我做什么 CreateItem 任务都会忽略我的 Exclude 参数:

<PropertyGroup>
  <RootFolder>c:\temp</RootFolder>
  <ExcludeFilter>*.tmp</ExcludeFilter>
  <StagingDirectory>staging</StagingDirectory>
</PropertyGroup>
<ItemGroup>
  <InputFile Include="MyFile.txt" />
</ItemGroup>

<Target Name="Build">
  <ReadLinesFromFile File="@(InputFile)">
    <Output ItemName="AllFolders" TaskParameter="Lines" />
  </ReadLinesFromFile>

  <CreateItem Include="$(RootFolder)\%(AllFolders.RelativeDir)**"
              Exclude="$(ExcludeFilter)">
    <Output ItemName="AllFiles" TaskParameter="Include" />
  </CreateItem>

  <Copy SourceFiles="@(AllFiles)"
        DestinationFolder="$(StagingDirectory)\%(RecursiveDir)" 

'MyFile.txt' 的示例内容:

somedirectory\
someotherdirectory\

(即路径是相对的$(RootFolder)- 提到这一点,因为我在某处读到它可能是相关的)

I've tried loads of different combinations of Exclude filters, but I never seem to be able to get it to correctly exclude my .tmp files - is there any way of doing this with MSBuild without resorting to xcopy?

4

1 回答 1

28

You have to specify the Exclude in absolute path and change the exclude wildcard to include subdirectory

If you use an absolute path for Include, you must use an absolute path for Exclude. If you use a relative path for Include, you must use a relative path for both.

<PropertyGroup>
  <RootFolder>c:\temp</RootFolder>
  <ExcludeFilter>**\*.tmp</ExcludeFilter>
  <StagingDirectory>staging</StagingDirectory>
</PropertyGroup>
<ItemGroup>
  <InputFile Include="MyFile.txt" />
</ItemGroup>

<Target Name="Build">
  <ReadLinesFromFile File="@(InputFile)">
    <Output ItemName="AllFolders" TaskParameter="Lines" />
  </ReadLinesFromFile>

  <CreateItem Include="$(RootFolder)\%(AllFolders.RelativeDir)**"
              Exclude="$(RootFolder)\%(AllFolders.RelativeDir)$(ExcludeFilter)">
    <Output ItemName="AllFiles" TaskParameter="Include" />
  </CreateItem>

  <Copy SourceFiles="@(AllFiles)"
        DestinationFolder="$(StagingDirectory)\%(RecursiveDir)"/>
</Target>

Multiple excludes and absolute path

If you want to exclude multiple items, there is no clean way when you are using absolute path, but you could do with Remove.

First way : Using Remove and item

<PropertyGroup>
  <RootFolder>c:\temp</RootFolder>
</PropertyGroup>

<ItemGroup>
  <InputFile Include="MyFile.txt" />
  <!-- Exclude are defined here -->
  <ExcludeFilters Include="$(RootFolder)\**\*.tmp"/>
  <ExcludeFilters Include="$(RootFolder)\**\*.bmp"/>
</ItemGroup>

<Target Name="Build">
  <ReadLinesFromFile File="@(InputFile)">
    <Output ItemName="AllFolders" TaskParameter="Lines" />
  </ReadLinesFromFile>

  <CreateItem Include="$(RootFolder)\%(AllFolders.RelativeDir)**">
    <Output ItemName="AllFiles" TaskParameter="Include" />
  </CreateItem>

  <!-- Removing the wrong extension in item -->
  <ItemGroup>
    <AllFiles Remove="@(ExcludeFilters)"/>
  </ItemGroup>

  <Copy SourceFiles="@(AllFiles)"
        DestinationFolder="$(StagingDirectory)\%(RecursiveDir)"/>
</Target>

Second way : Using Remove and condition

<Target Name="Build">
  <ReadLinesFromFile File="@(InputFile)">
    <Output ItemName="AllFolders" TaskParameter="Lines" />
  </ReadLinesFromFile>

  <CreateItem Include="$(RootFolder)\%(AllFolders.RelativeDir)**">
    <Output ItemName="AllFiles" TaskParameter="Include" />
  </CreateItem>

  <!-- Removing the wrong extension in item -->
  <ItemGroup>
    <AllFiles Remove="@(AllFiles)" Condition="'%(Extension)' == '.tmp'"/>
    <AllFiles Remove="@(AllFiles)" Condition="'%(Extension)' == '.bmp'"/>
  </ItemGroup>

  <Copy SourceFiles="@(AllFiles)"
        DestinationFolder="$(StagingDirectory)\%(RecursiveDir)"/>
</Target>
于 2010-06-13T07:05:17.813 回答