MSBuild copy entire directory while using metadata in path names

只愿长相守 提交于 2019-12-07 18:28:24

OK, I actually solved it somehow, but I'm still interested in answering the "bonus" questions / perhaps my solution isn't good:

I added a PropertyGroup inside the target and concatenated the path there:

<PropertyGroup>
  <Deploydir>$(ProjectDir)deploy\%(DeployLabel.Dir)\media</Deploydir>
</PropertyGroup>

and later in that target I used

DestinationFiles="@(MediaFiles->'$(Deploydir)\%(RecursiveDir)%(Filename)%(Extension)')"

instead, so this avoided the problem with percents (possibly) coming from two different places.

These batchings are quite confusing, because some combinations of batchings are working with different ItemGroups and some not. I create a small proj to show some differences:

<?xml version="1.0" encoding="utf-8"?> 
<Project DefaultTargets="Batch" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
 <ItemGroup>
  <BatchItem Include="myFile.txt">
   <Folder>c:\Temp</Folder>
  </BatchItem>
  <BatchItem Include="myFile2.txt">
   <Folder>c:\Tmp</Folder>
  </BatchItem>

  <DifferentItem Include="myFile3.txt">
   <Folder>c:\diff</Folder>
  </DifferentItem>
 </ItemGroup>

 <Target Name="Batch">
  <Message Text="Using (at) @(BatchItem) --  @(DifferentItem)" />
  <Message Text="Using -> @(BatchItem->'%(Folder)\somethingInbetween\%(Identity)') -- @(DifferentItem->'%(Folder)\somethingInbetween\%(Identity)')" />   
  <Message Text="Using percent %(BatchItem.Folder)\%(BatchItem.Identity) -- %(DifferentItem.Folder)\%(DifferentItem.Identity)" />
 </Target>
</Project>
  • The first one will just put all after items in on string semicolon separated.
  • The second one will use specific metadata of one item to make a string of it. This is done for all elements and they are semicolon separated as one item.
  • The third expression is also referencing the metadata of an item, but it is also calling the target for each item in the group. Doing it that way you can't mix ItemGroups.

    Using (at) myFile.txt;myFile2.txt -- myFile3.txt

    Using -> c:\Temp\somethingInbetween\myFile.txt;c:\Tmp\somethingInbetween\myFi le2.txt -- c:\diff\somethingInbetween\myFile3.txt

    Using percent c:\Temp\myFile.txt -- \

    Using percent c:\Tmp\myFile2.txt -- \

    Using percent \ -- c:\diff\myFile3.txt

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