开发者

Parallelism in MSBuild

开发者 https://www.devze.com 2023-01-27 06:02 出处:网络
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:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消