build - MSBuild:并行构建和 .Net 项目

我编写了一个 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、...我希望如果复制失败,几毫秒后完成的另一个复制会成功。但是我一直无法设置这些参数。我该如何更改它们?

还有其他解决方案吗?

最佳答案

我找到了解决办法

<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 警告?

https://stackoverflow.com/questions/12413529/

相关文章:

c++ - 按降序对 vector 进行排序

c# - 构建软件补丁的好习惯是什么?

c++ - std::string 到 char*

java - react-native run-android 构建错误 ':app:generat

c++ - 在 C++ 中将默认参数值放在哪里?

eclipse - 从命令行刷新eclipse

java - Gradle 中的 uberJar、fatJar 和 shadowJar 有什么区别?

php - 在 Git Workflow 中生成 API 文档

build - 使用 RequireJS 的优化器和单个构建文件来生成多个输出

c++ - 单例:应该如何使用