3

Suppose I have two targets that are time-consuming and I want to execute them in parallel. Let's say one target runs unit tests and the other generates 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 invoking 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 gleaned that parallelization occurs only at the project level. Thus, 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 in the same fashion, 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, the targets are building in parallel.

But separating out targets into separate files just for the purposes of parallelization seems clunky. Am I missing something here? Is there a way I can avoid creating the extra targets files and still achieve parallelism?

1 Answers1

0

I did the same for my time consuming builds and separating targets in different files does not look clumsy to me.

Since you want to build them in parallel, they don't interact. They are part of the build but this aside, they have no reason to be in the the same file.

I also separate property and items in a properties.xml.Targets to maintain them more easily. Doing this I can reference them in my multiple targets files without copying code too.

Benjamin Baumann
  • 4,035
  • 2
  • 25
  • 35
  • Yeah, I have a shared properties file, too. The fact that I've had to separate targets into new files *just for the purposes of parallelization* screams clunky to me because I wouldn't have separated them otherwise. There's no technical reason why MSBuild couldn't parallelize the execution of the targets even if they're in the same file. It would just result in separate processes executing a different target in the same file, rather than separate processes executing different targets in different files. – cantloginfromwork Nov 24 '10 at 15:03