我有一个Visual 2012解决方案,有12种解决方案配置。每个解决方案配置都是独立的(也就是说,每个配置的输出是完全不相关的)。
问题:如何在一步内构建所有12个配置,即通过在命令行上运行单个MSBuild命令,以及如何获得并行构建的配置?
例如,如果只有两种配置,Release和Debug/AnyCPU,我希望这两个配置同时并行构建。
为了完整起见,下面是我尝试过的,我还没有解决这个问题的方法。
为了一次构建所有项目,我创建了一个新的项目文件,其中包含一个构建目标,该目标为每个配置在解决方案文件上运行MSBuild任务:
<Target Name="Build">
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Release;Platform=Win32" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Release;Platform=x64" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Release;Platform=ARM" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Release(ZW);Platform=Win32" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Release(ZW);Platform=x64" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Release(ZW);Platform=ARM" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Debug;Platform=Win32" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Debug;Platform=x64" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Debug;Platform=ARM" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Debug(ZW);Platform=Win32" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Debug(ZW);Platform=x64" Targets="$(BuildCommand)" />
<MSBuild Projects="cxxreflect.sln" Properties="SolutionDir=$(MSBuildProjectDirectory)\;Configuration=Debug(ZW);Platform=ARM" Targets="$(BuildCommand)" />
</Target>这很好,只不过每个MSBuild任务都是按顺序调用的,所以没有并行性(是的,每个配置构建中都存在并行性,但我真的很想在配置构建中获得并行性)。
为了获得并行构建的配置,我尝试使用MSBuild任务的BuildInParallel属性。我编写了一个预构建任务,为每个配置生成项目文件,然后尝试并行构建所有这些生成的项目:
<Target Name="PreBuild" Outputs="%(ProjectConfiguration.Identity)" Returns="%(BuildProject.Identity)">
<Message Text="Cloning Solution for Configuration: %(ProjectConfiguration.Identity)" />
<PropertyGroup>
<BuildProjectPath>$(IntPath)\%(ProjectConfiguration.Platform)\%(ProjectConfiguration.Configuration)\build.proj</BuildProjectPath>
</PropertyGroup>
<ItemGroup>
<BuildProject Include="$(BuildProjectPath)" />
</ItemGroup>
<MakeDir Directories="$(IntPath)\%(ProjectConfiguration.Platform)\%(ProjectConfiguration.Configuration)" />
<WriteLinesToFile
File="$(BuildProjectPath)"
Lines="<?xml version='1.0' encoding='utf-8'?>
<Project DefaultTargets='Build' ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Target Name='Build'>
<MSBuild
Projects='$(SlnPath)'
Properties='
SolutionDir=$(MSBuildProjectDirectory)\%3b
Configuration=%(ProjectConfiguration.Configuration)%3b
Platform=%(ProjectConfiguration.Platform)
'
Targets='$(BuildCommand)'
/>
</Target>
</Project>"
Overwrite="true" />
</Target>
<Target Name="Build" DependsOnTargets="PreBuild">
<Message Text="%(BuildProject.Identity)" />
<MSBuild Projects="%(BuildProject.Identity)" Properties="BuildCommand=$(BuildCommand)" BuildInParallel="true" />
</Target>不幸的是,虽然这确实构建了所有的12种配置,但它是按顺序构建的。我的猜测是,当MSBuild对将要构建的项目集执行依赖分析时,它会确定它们都依赖于同一个解决方案文件,因此它决定不能并行构建它们。(这只是猜测,我可能完全错了。我对MSBuild知之甚少。)
在构建时,我还使用了/m开关。
发布于 2012-10-20 04:19:11
利用MSBuild任务的BuildInParallel选项,在一个调用中传递所有项目。示例here给出了基本方法:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectToBuild Include="a1.sln">
<Properties>Configuration=Debug</Properties>
</ProjectToBuild>
<ProjectToBuild Include="a1.sln">
<Properties>Configuration=Release</Properties>
</ProjectToBuild>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="@(ProjectToBuild)" BuildInParallel="true" />
</Target>
</Project>https://stackoverflow.com/questions/12984882
复制相似问题