【问题标题】:MSBuild: parallel builds and .Net projectsMSBuild:并行构建和 .Net 项目
【发布时间】:2012-09-13 19:34:51
【问题描述】:

我编写了一个 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> 

我使用以下命令启动 msbuild:

msbuild /m /p:BuildInParallel=true /t:BuildAll buildall.proj 

问题是我的解决方案有许多 .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. 

我认为这意味着:“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'"
    > 

我尝试设置变量 CopyRetryCount、CopyRetryDelayMilliseconds、...我希望如果复制失败,几毫秒后完成的另一个复制会成功。但我一直无法设置这些参数。我该如何更改它们?

还有其他解决方案吗?

【问题讨论】:

  • 您是在构建 NLog 作为产品的一部分,还是只是将其签入源代码控制?如果您正在构建它,是否有可能您的某些项目引用二进制位置而不是项目?

标签: build msbuild parallel-processing


【解决方案1】:

我找到了解决办法

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

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

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. 

在我上次的测试中,它产生了 36 次这个警告!有没有办法抑制 MSB0326 警告?

【讨论】:

    【解决方案2】:

    一般来说,在构建期间运行的任何东西都不能在其输入上获取排他锁 - 只有共享读取锁。看起来你的构建过程中的某些东西(可能是 NLog,不管它是什么)违反了这一点 - 它对输入 "xxx\NLog.dll" 进行排他锁定,因此当另一个 msbuild 节点尝试复制相同的输入时它会失败。

    重试是针对您的特定症状的合理解决方法 - 尽管不能保证总是成功。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。我需要在我的 AntiVirus 中添加适当的例外,以避免扫描 MsBuild 生成的 DLL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        相关资源
        最近更新 更多