Build multiple projects in parallel with MSBuild
To build multiple projects faster, you can use MSBuild to run builds in parallel. To do so, use the following settings on multi-core or multiple processor computers:
Set the
-maxcpucount
switch when you run MSBuild at a command prompt.Set the BuildInParallel task parameter to
true
on your MSBuild task.
Note
Your build performance might decrease if you use the -verbosity
(-v
) switch to set the verbosity of your build log information to detailed or diagnostic. This switch is often used for troubleshooting. For more information, see Obtain build logs with MSBuild and MSBuild command-line reference.
-maxcpucount MSBuild switch
If you use the -maxcpucount
(-m
) switch, which was introduced in MSBuild 3.5, MSBuild creates the specified number of MSBuild.exe processes that can be run in parallel. These processes are also known as worker processes. Each worker process uses a separate core or processor, if any are available, to build a project at the same time as other available processors might be building other projects.
For example, setting this switch to a value of 4
causes MSBuild to create four worker processes to build the project.
If you include the -maxcpucount
switch without specifying a value, MSBuild uses up to the number of processors on the computer.
For more information about this switch, see MSBuild command-line reference.
The following example instructs MSBuild to use three worker processes. With this configuration, MSBuild can build three projects at the same time.
msbuild.exe myproj.proj -maxcpucount:3
BuildInParallel MSBuild task parameter
BuildInParallel
is an optional boolean parameter you can set on your MSBuild task. When you set BuildInParallel
to true
(default value), multiple worker processes are generated to build as many projects at the same time as possible. For this parameter to work correctly, set the -maxcpucount
switch to a value greater than one.
When you use MSBuild to building in parallel, it works only for a single invocation of the MSBuild task. Therefore, if you invoke task batching, the parallelism is limited to each batch. For more information, see MSBuild batching.
The following examples show how to build a target in a project file with multiple different property values in parallel by using the BuildInParallel
parameter.
In this example, the project file do_it.proj
has a target that prints a different message for each SourceValue
.
<Project>
<Target Name="DoIt">
<Message Text="For this invocation SourceValue='$(SourceValue)'" Importance="High" />
</Target>
</Project>
The following project builds a specified target DoIt in a project named do_it.proj in parallel, using the item list and AdditionalProperties
metadata to specify different values of the property SourceValue
.
<Project>
<ItemGroup>
<_Project Include="do_it.proj" AdditionalProperties="SourceValue=Test1" />
<_Project Include="do_it.proj" AdditionalProperties="SourceValue=Test2" />
<_Project Include="do_it.proj" AdditionalProperties="SourceValue=Test3" />
<_Project Include="do_it.proj" AdditionalProperties="SourceValue=Test4" />
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="@(_Project)" Targets="DoIt" BuildInParallel="true" />
</Target>
</Project>