使用 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.

Note

命令列中的 -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 會建立四個背景工作處理序來建置專案。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 使用三個背景工作處理序。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

BuildInParallelMSBuildMSBuild 工作上的選擇性 Boolean 參數。BuildInParallel is an optional boolean parameter on a MSBuildMSBuild task. BuildInParallel 設為 true (其預設值為 false) 時,會產生多個背景工作處理序,以盡可能同時建置最多個專案。When BuildInParallel is set to true (its default value is false), 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