Question

I am writing an msbuild script to deploy multiple targets. I am trying to reuse some elements and am getting an unexpected behavior.

When I run this proj file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="All" DependsOnTargets="DeployTarget1;DeployTarget2">
  </Target>
  <Target Name="DeployTarget1">
    <PropertyGroup>
      <RootDir>.\source1</RootDir>
    </PropertyGroup>
    <ItemGroup>
      <DeployFiles Include="$(RootDir)\**\*.dll" Exclude="$(RootDir)\bin\C_*.xml" />
    </ItemGroup>
    <Copy SourceFiles="@(DeployFiles)" DestinationFiles="@(DeployFiles->'D:\deploytest\dest1\%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>
  <Target Name="DeployTarget2">
    <PropertyGroup>
      <RootDir>.\source2</RootDir>
    </PropertyGroup>
    <ItemGroup>
      <DeployFiles Include="$(RootDir)\**\*.dll" Exclude="$(RootDir)\bin\C_*.xml" />
    </ItemGroup>
    <Copy SourceFiles="@(DeployFiles)" DestinationFiles="@(DeployFiles->'D:\deploytest\dest2\%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>
</Project>

DeployTarget1 copies the files from the source1 directory recursively to dest1 as I expect.

DeployTarget2 copies both source1 and source2 to dest2, this is not what I expected.

D:\deploytest>msbuild /t:All test.proj
Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.18052]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 8/20/2013 2:37:17 PM.
Project "D:\deploytest\test.proj" on node 1 (All target(s)).
DeployTarget1:
  Copying file from ".\source1\test1.dll" to "D:\deploytest\dest1\test1.dll".
  copy /y ".\source1\test1.dll" "D:\deploytest\dest1\test1.dll"
DeployTarget2:
  Copying file from ".\source1\test1.dll" to "D:\deploytest\dest2\test1.dll".
  copy /y ".\source1\test1.dll" "D:\deploytest\dest2\test1.dll"
  Copying file from ".\source2\test2.dll" to "D:\deploytest\dest2\test2.dll".
  copy /y ".\source2\test2.dll" "D:\deploytest\dest2\test2.dll"
Done Building Project "D:\deploytest\test.proj" (All target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.06

Does anyone know why this happens?
Can you point me to documentation on this "feature"?

Was it helpful?

Solution

ItemGroups are cummulative. Each time an item group if referenced, items are added to the existing group. If you want to have explicit groups, you either need to give them a unique name or you need to clear them before usage.

Use <DeployFiles Remove="**/*">

See also: https://stackoverflow.com/a/7915992/736079

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top