dotnet核心并行或同时构建
在this解决方案中,我有2个应用程序:AppA
,AppB
共享类库Shared
。我已尝试与PowerShell和node脚本并行地自动构建/运行这些脚本(我会对其他解决方案持开放态度)。我正在使用--no-dependencies
和--no-restore
标志,但间歇性地我得到:
'CSC : error CS2012: Cannot open \'C:\\Users\\User\\source\\repos\\ParallelBuild\\Shared\\obj\\Debug\\netcoreapp2.0\\Shared.dll\' for writing -- \'The process cannot access the file \'C:\\Users\\User\\source\\repos\\ParallelBuild\\Shared\\obj\\Debug\\netcoreapp2.0\\Shared.dll\' because it is being used by another process.\' [C:\\Users\\User\\source\\repos\\ParallelBuild\\Shared\\Shared.csproj]\r\n'
PowerShell:./ build.ps1
节点:运行项目build-script
或node ./build-script/app.js
为什么Shared
项目建设甚至与--no-dependencies
旗帜?如何并行或同时构建?
错误CS2012的说明:进程无法访问该文件
我可以重现你的问题,Process Monitor表明两个进程同时打开shared.dll
进行阅读。这导致您描述的问题。
虽然可以通过使用FileShare.Read
来表示来自不同进程的相同文件(参见下面的摘录),但似乎这不是由documentation完成的。这是csc.exe
的缺点,在不久的将来可能不会改变。
允许随后打开文件进行读取。如果未指定此标志,则在文件关闭之前,任何打开文件以供读取(通过此进程或其他进程)的请求都将失败。但是,即使指定了此标志,仍可能需要其他权限才能访问该文件。
解
而不是使用csc.exe
来实现并行构建,您可以使用msbuild,它支持开箱即用。
msbuild支持并行构建,Start-Process
-switch和/maxcpucount
-task参数,如BuildInParallel
中所述。
在命令行上使用qazxsw poi-switch启用并行构建:
dotnet msbuild。\ ParallelBuildAB.csproj / target:build / restore:false / maxcpucount:2
此外,还需要在项目文件ParallelBuildAB.csproj中使用MS build documentation-task参数。请注意,/maxcpucount
和BuildInParallel
被引用:
AppA.csproj