I'm trying to copy a folder recursively to multiple destination folders using MSBuild's Copy task. I've seen the following question which gave me a good start, but I must be missing something:
Msbuild copy to several locations based on list of destination parameter?
A snippet from my build file is below:
<ItemGroup>
<DeployPath Include="\\server1\path" />
<DeployPath Include="\\server2\path" />
</Item Group>
<Target Name="Deploy">
<Message Text="%(DeployPath.Identity)" />
<Copy SourceFiles="@(ItemsToCopy)" DestinationFolder="%(DeployPath.Identity)\%(RecursiveDir)" />
</Target>
When I run this, the "Message" task, as I would expect, spits out 2 lines:
\\server1\path
\\server2\path
The problem is, the "Copy" task appears to only run once, and copies the files to the root of the current hard drive and not the specified network paths:
Copies to C:\file1.txt
instead of \\server1\path\file1.txt
I'm fairly new to MSBuild, so I feel like I'm missing something pretty basic here.
Any help would be greatly appreciated.
What you are dealing with here is known as batching. I have blogged quite a bit about batching. You can find my blogs listed at http://sedotech.com/Resources#Batching. Batching is a way to do a loop without really doing one in MSBuild. You can split groups into values with a common metadata value. Metadata could be values like Identity, FullPath, Filename, etc. You can even make your own metadata. In any case when you batch on more than 1 value they are batched independently of each other. Take a look at the example that I created. The result of executing the target is shown after the script.
Output
The most important missing piece in the puzzle seems to be the
Outputs
attribute on theTarget
element without which you'll always only execute the target for one item of the whole list. The other piece is the new property you need to define on the way.The solution to your problem might look like so: