Parallelism in MSBuild

Suppose I have two goals that take a lot of time, and I want to fulfill them in parallel. Let's say one goal runs unit tests, and the other creates some documentation. I tried this approach:

root.targets

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Default">
    <Target Name="Default">
        <MSBuild Projects="$(MSBuildProjectFile)" Targets="RunTests;BuildDocumentation" BuildInParallel="True"/>
    </Target>

    <Target Name="RunTests">
        <Message Text="Running tests"/>
    </Target>

    <Target Name="BuildDocumentation">
        <Message Text="Building documentation"/>
    </Target>
</Project>

And then called like this (on a dual-core machine):

msbuild root.targets /m

But I get this output:

1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" on node 1 (default targets).
1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1) is building "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1:2) on node 1 (RunTests;BuildDocumentation target(s)).
1>RunTests:
    Running tests
  BuildDocumentation:
    Building documentation

From this and some googling, I realized that parallelization occurs only at the project level. So I tried this:

root.targets

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Default">
    <Target Name="Default">
        <MSBuild Projects="test.targets;documentation.targets" BuildInParallel="True"/>
    </Target>
</Project>

test.targets

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="Default" DependsOnTargets="RunTests"/>

    <Target Name="RunTests">
        <Message Text="Running tests"/>
    </Target>
</Project>

documentation.targets

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="Default" DependsOnTargets="BuildDocumentation"/>

    <Target Name="BuildDocumentation">
        <Message Text="Building documentation"/>
    </Target>
</Project>

Running it the same way, I get:

1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1) is building "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\test.t
  argets" (2) on node 1 (default targets).
2>RunTests:
    Running tests
2>Done Building Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\test.targets" (default targets).
1>Project "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\root.targets" (1) is building "C:\Repository\depot\EDG\DEW\branches\dna.dev.br\DnA\client\src\docume
  ntation.targets" (3) on node 2 (default targets).
3>BuildDocumentation:
    Building documentation

Thus, goals are built in parallel.

But dividing targets into separate files just for parallelization purposes seems awkward. Am I missing something? Is there a way to avoid creating additional goals files and still achieve parallelism?

+3
1

, .

, . , , .

properties.xml.Targets, . , .

0

Source: https://habr.com/ru/post/1776684/


All Articles