dotnet内核并行或同时构建 [英] dotnet core build in parallel or simultaneously
问题描述
在
节点:运行项目 build-script
或 node ./build-script/app.js
为什么即使使用-no-dependencies
标志,也要构建 Shared
项目?如何并行或同时构建?
对错误CS2012的解释:该进程无法访问文件
我可以重现您的问题,并且 Process Monitor 表示这两个进程同时打开 shared.dll
进行读取.这会导致您描述的问题.
尽管可以通过使用 FileShare.Read
作为 MS构建文档.
通过在命令行上使用/maxcpucount
-switch启用并行构建:
dotnet msbuild.\ ParallelBuildAB.csproj/target:build/restore:false/maxcpucount:2
此外,还必须在项目文件ParallelBuildAB.csproj中使用 BuildInParallel
-task参数.请注意,已引用 AppA.csproj
和 AppB.csproj
:
<?xml version ="1.0"?><项目名称="system" default ="build" xmlns ="http://schemas.microsoft.com/developer/msbuild/2003">< Target Name ="build">< MSBuild BuildInParallel ="true" Projects ="./AppA/AppA.csproj; ./AppB/AppB.csproj" Targets ="Build"/></Target></Project>
In this solution I have 2 apps: AppA
, AppB
that share class library Shared
. I have tried automating the build/running of these in parallel with both a PowerShell and node scripts (I'd be open to other solutions). I'm using both --no-dependencies
and --no-restore
flags, but intermittently I get:
'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
node:
run project build-script
or node ./build-script/app.js
Why is the Shared
project building even with the --no-dependencies
flag? How do I build in parallel or simultaneously?
Explanation for error CS2012 :The process cannot access the file
I could reproduce your issue and Process Monitor indicates that both processes open shared.dll
at the same time for reading. This leads to the issue you described.
Although reading the same file from different processes can be done by using FileShare.Read
as the documentation indicates (see excerpt below), it seems that this is NOT done by csc.exe
. This is a shortcoming of csc.exe
, which probably won't be changed in the near future.
Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
Solution
Instead of using Start-Process
to achieve parallel building, you could use msbuild, which supports this out of the box.
Parallel build is supported by msbuild with /maxcpucount
-switch and BuildInParallel
-task parameter as described in MS build documentation.
Parallel build is enabled by using /maxcpucount
-switch on commandline:
dotnet msbuild .\ParallelBuildAB.csproj /target:build /restore:false /maxcpucount:2
In addition it is also necessary to use BuildInParallel
-task Parameter in project-file ParallelBuildAB.csproj. Please note that AppA.csproj
and AppB.csproj
are referenced:
<?xml version="1.0"?>
<Project name="system" default="build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="build">
<MSBuild BuildInParallel="true" Projects="./AppA/AppA.csproj;./AppB/AppB.csproj" Targets="Build"/>
</Target>
</Project>
这篇关于dotnet内核并行或同时构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!