How to run tasks in parallel in MSBuild

2019-01-11 01:10发布

问题:

Unless I've grossly misunderstood MSBuild, tasks are executed in the document order in which they appear within a 'Target' node.

I'd like to be able to specify that two tasks (such as xcopy tasks) could run in parallel. I was expecting there to be a 'Parallel' task or something...?

回答1:

As was stated, you cannot parallelise at the task level or even at the target level. MSBuild only will build projects (i.e. MSBuild project files) in parallel. So you have to use the MSBuild task with multiple projects specified and the BuildInParallel attribute should be set to true. Also make sure that when the build is invoked on the command line that the /m switch is sent it.

Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build



回答2:

Try the new parallel task in the MSBuild Extension Pack - http://mikefourie.wordpress.com/2012/02/29/executing-msbuild-targets-in-parallel-part-1



回答3:

MSBuild has a /m command line switch to tell it the maximum number of concurrent processes to build with. The default value is 1. /m:x will use x processes. /m will use the number of processors on computer.

I've used this as part of a shortcut in Visual Studio to run builds quicker by compiling projects in parallel. Scott Hanselman has a few posts about it here and here.



回答4:

I don't think there is a way to do this other than using some external tool. I suspect this is because this introduces potential dependency problems that MS chooses to ignore for the time being, allowing external tool vendors to tackle the problem (and charge quite a bit of money for it).



回答5:

Here is an example of a way to run msbuild targets in parallel. The idea is same ... presenting this msbuild file itself as a project to build. I copied it from my own question: Evaluate item defined in msbuild task via C#

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
<Target Name="Build" DependsOnTargets="PrepareEnvironmentForBuild;MapDrives">
    <Exec Command="$(MSBuildBinPath)\msbuild /nologo /clp:Verbosity=quiet $(MSBuildThisFileFullPath) /t:TargetWithConfidentialSteps"/>
    <ItemGroup>
        <StepsToRunInParallel Include="$(MSBuildThisFileFullPath)">
            <Properties>TargetToInvoke=ParallelTarget1</Properties>
        </StepsToRunInParallel>
        <StepsToRunInParallel Include="$(MSBuildThisFileFullPath)">
            <Properties>TargetToInvoke=ParallelTarget2</Properties>
        </StepsToRunInParallel>
    </ItemGroup>
    <MSBuild Projects="@(StepsToRunInParallel)" BuildInParallel="true" StopOnFirstFailure="true" Targets="InvokeInParallelWithinThisProject"/>

</Target>
<Target Name="InvokeInParallelWithinThisProject">
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="$(TargetToInvoke)" StopOnFirstFailure="true"/>
</Target>
<Target Name="ParallelTarget1">
    <Message Text="Hello from ParallelTarget1"/>
</Target>
<Target Name="ParallelTarget2">
    <Message Text="Hello from ParallelTarget2"/>
</Target>
<Target Name="PrepareEnvironmentForBuild">
    <Message Text="Hello from PrepareEnvironmentForBuild"/>
</Target>
<Target Name="MapDrives">
    <Message Text="Hello from MapDrives"/>
</Target>
<Target Name="TargetWithConfidentialSteps">
    <Message Text="Hush! Verbosity on the wrapper does not affect the Exec call." Importance="High"/>
</Target>