MSBuild:并行构建和.Net项目

Ser*_*ock 10 msbuild parallel-processing build

我编写了一个MSBuild项目文件,试图并行构建我的VS2010解决方案的所有配置:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <ItemGroup>
    <BuildFile Include="$(SourceRoot)MyProject.sln" />
    <Config Include="Debug">
      <Configuration>Debug</Configuration>
    </Config>
    <Config Include="Release">
      <Configuration>Release</Configuration>
    </Config>
  </ItemGroup>

  <Target Name="BuildAll" Outputs="%(Config.Configuration)">
    <Message Text="Start building for configuration: %(Config.Configuration)" />
    <MSBuild Projects="@(BuildFile)"
             Properties="Configuration=%(Config.Configuration)"
             Targets="Build" />
  </Target>  
</Project> 
Run Code Online (Sandbox Code Playgroud)

我用以下命令启动msbuild:

msbuild /m /p:BuildInParallel=true /t:BuildAll buildall.proj 
Run Code Online (Sandbox Code Playgroud)

问题是我的解决方案有许多.Net项目都有相同的输出文件夹.这些项目也使用相同的外部程序集.

因此,通常会同时生成两个输出可执行文件,并同时复制它们的依赖项.这会导致错误,例如:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3001,9): 
error MSB3021: 
Unable to copy file "xxx\NLog.dll" to "D:\src\Blackbird\shared\bin\debug\NLog.xml". The process 
cannot access the file 'xxx\NLog.dll because it is being used by another process. 
Run Code Online (Sandbox Code Playgroud)

我认为这意味着:"2个不同的项目使用NLog并尝试同时在输出文件夹中复制其程序集"......

有没有办法解决这个问题?我真的想避免修改解决方案中的所有项目.

查看任务源代码"C:\ Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3001,9)",我看到有可能使msbuild重试该副本:

<Copy
    SourceFiles="@(ReferenceCopyLocalPaths)"
    DestinationFiles="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')"
    SkipUnchangedFiles="$(SkipCopyUnchangedFiles)"
    OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
    Retries="$(CopyRetryCount)"
    RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
    UseHardlinksIfPossible="$(CreateHardLinksForCopyLocalIfPossible)"
    Condition="'$(UseCommonOutputDirectory)' != 'true'"
    > 
Run Code Online (Sandbox Code Playgroud)

我试着设置变量CopyRetryCount,CopyRetryDelayMilliseconds,...我希望如果复制失败,几毫秒后完成另一个副本就会成功.但我一直无法设置这些参数.我怎样才能改变它们?

还有其他解决方案吗?

Ser*_*ock 3

我找到了解决方案

<MSBuild Projects="@(BuildFile)"
  Properties="Configuration=%(Config.Configuration);Retries=10;RetryDelayMilliseconds=50"
  Targets="Build" />
Run Code Online (Sandbox Code Playgroud)

它按预期工作,但现在它在重试复制之前生成警告

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3001,9): 
warning MSB3026: Could not copy 
"D:\src\xxx\System.Data.SQLite.pdb" to "..\Debug\System.Data.SQLite.pdb". 
Beginning retry 1 in 50ms. The process cannot access the file 
'..\Debug\System.Data.SQLite.pdb' because it is being used by another process. 
Run Code Online (Sandbox Code Playgroud)

在我上次测试期间,它生成了 36 次此警告!有没有办法抑制警告MSB0326?