MSBuild 中的并行性
假设我有两个耗时的目标,并且我想并行执行它们。假设一个目标运行单元测试,另一个目标生成一些文档。我尝试了这种方法:
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>
然后像这样调用(在双核机器上):
msbuild root.targets /m
但是我得到了这个输出:
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
从这个和一些谷歌搜索中我发现并行化仅发生在项目中等级。因此,我尝试了以下操作:
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>
以相同的方式运行它,我得到:
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
因此,目标是并行建立的。
但是仅仅为了并行化的目的将目标分离到单独的文件中似乎很笨拙。我在这里错过了什么吗?有没有办法可以避免创建额外的目标文件并仍然实现并行性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对耗时的构建做了同样的事情,并且将不同文件中的目标分开对我来说并不显得笨拙。
由于您想并行构建它们,因此它们不会交互。它们是构建的一部分,但除此之外,它们没有理由位于同一个文件中。
我还将properties.xml.Targets 中的属性和项目分开,以便更轻松地维护它们。这样做我可以在我的多个目标文件中引用它们,而无需复制代码。
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.