Msbuild ItemGroup意外地累积要复制的文件

时间:2013-08-20 18:47:51

标签: msbuild msbuild-4.0

我正在编写一个msbuild脚本来部署多个目标。我正在尝试重用一些元素,并且会出现意外行为。

当我运行这个proj文件时:

<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按照我的预期将文件从source1目录递归地复制到dest1。

DeployTarget2将两者 source1 source2复制到dest2,这不是我的预期。

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

有谁知道为什么会这样? 你能指点我这个&#34;功能&#34;?

的文档

1 个答案:

答案 0 :(得分:2)

ItemGroups是累积的。每次引用项目组时,项目都会添加到现有组中。如果您想拥有明确的组,您需要为它们指定一个唯一的名称,或者在使用之前需要清除它们。

使用<DeployFiles Remove="**/*">

另请参阅:https://stackoverflow.com/a/7915992/736079