我编写了一个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等,希望如果复制失败,几毫秒后进行另一次复制可以成功。但是我无法设置这些参数。我该如何更改它们?
是否有其他解决方案?