用 MSBuild 并行生成多个项目Build multiple projects in parallel with MSBuild

通过 MSBuild,可通过并行运行多个项目来更快地生成它们。You can use MSBuild to build multiple projects faster by running them in parallel. 若要并行运行生成,请在一台多核或多处理器计算机上采用以下设置:To run builds in parallel, you use the following settings on a multi-core or multiple processor computer:

  • 在命令提示符处使用 -maxcpucount 开关。The -maxcpucount switch at a command prompt.

  • 在 MSBuild 任务中使用 BuildInParallel 任务参数。The BuildInParallel task parameter on an MSBuild task.

备注

命令行中的 -verbosity (-v) 开关也可能会影响生成性能。The -verbosity (-v) switch in a command line can also affect build performance. 如果将生成日志信息的详细级别设置为详细或诊断以用于疑难解答,生成性能可能会降低。Your build performance might decrease if the verbosity of your build log information is set to detailed or diagnostic, which are used for troubleshooting. 有关详细信息,请参阅获取生成日志命令行参考For more information, see Obtain build logs and Command-line reference.

-maxcpucount 开关-maxcpucount Switch

如果使用 -maxcpucount 开关(简写为 -m),MSBuild 将可以创建指定数量的能够并行运行的 MSBuild.exe 进程。If you use the -maxcpucount switch, or -m for short, MSBuild can create the specified number of MSBuild.exe processes that may 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 may be building other projects. 例如,如果将此开关的值设置为“4”,MSBuild 将创建 4 个辅助进程来生成项目。For example, setting this switch to a value of "4" causes MSBuild to create four worker processes to build the project.

如果包含 -maxcpucount 开关而没有指定值,MSBuild 将使用计算机上的处理器总数作为其值。If you include the -maxcpucount switch without specifying a value, MSBuild will use up to the number of processors on the computer.

有关 MSBuild 3.5 中引入的此开关的详细信息,请参阅命令行参考For more information about this switch, which was introduced in MSBuild 3.5, see Command-line reference.

下面的示例指示 MSBuild 使用 3 个辅助进程。The following example instructs MSBuild to use three worker processes. 如果使用此配置,MSBuild 可以同时生成三个项目。If you use this configuration, MSBuild can build three projects at the same time.

msbuild.exe myproj.proj -maxcpucount:3

BuildInParallel 任务参数BuildInParallel task parameter

BuildInParallel 是 MSBuild 任务上的可选布尔参数。BuildInParallel is an optional boolean parameter on an MSBuild task. 如果 BuildInParallel 设置为 true(默认值为 true),则会生成多个工作进程,以尽可能同时生成多个项目。When BuildInParallel is set to true (its default value is true), multiple worker processes are generated to build as many projects at the same time as possible. 为使此操作能够正常进行,-maxcpucount 开关必须设置为一个大于 1 的值,并且系统必须至少为双核或具有两个或多个处理器。For this to work correctly, the -maxcpucount switch must be set to a value greater than 1, and the system must be at least dual-core or have two or more processors.

以下是摘自 microsoft.common.targets 中的有关如何设置 BuildInParallel 参数的示例。The following is an example, taken from microsoft.common.targets, about how to set the BuildInParallel parameter.

<PropertyGroup>
    <BuildInParallel Condition="'$(BuildInParallel)' ==
        ''">true</BuildInParallel>
</PropertyGroup>
<MSBuild
    Projects="@(_MSBuildProjectReferenceExistent)"
    Targets="GetTargetPath"
    BuildInParallel="$(BuildInParallel)"
    Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration);
        %(_MSBuildProjectReferenceExistent.SetPlatform)"
    Condition="'@(NonVCProjectReference)'!='' and
        ('$(BuildingSolutionFile)' == 'true' or
        '$(BuildingInsideVisualStudio)' == 'true' or
        '$(BuildProjectReferences)' != 'true') and
        '@(_MSBuildProjectReferenceExistent)' != ''"
    ContinueOnError="!$(BuildingProject)">
    <Output TaskParameter="TargetOutputs"
        ItemName="_ResolvedProjectReferencePaths"/>
</MSBuild>

另请参阅See also