Trying to exclude certain extensions doing a recursive copy (MSBuild)

随声附和 提交于 2019-11-30 01:00:07

问题


I'm trying to use MSBuild to read in a list of files from a text file, and then perform a recursive copy, copying the contents of those directories files to some staging area, while excluding certain extensions (e.g. .tmp files)

I've managed to do most of the above quite easily using CreateItem and the MSBuild copy task, whatever I do the CreateItem task just ignores my Exclude parameter:

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

Example contents of 'MyFile.txt':

somedirectory\
someotherdirectory\

(I.e. the paths are relative to $(RootFolder) - mention this because I read somewhere that it might be relevant)

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?


回答1:


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>


来源:https://stackoverflow.com/questions/3031021/trying-to-exclude-certain-extensions-doing-a-recursive-copy-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!