将属性传递给 MSBUILD 任务 [英] Passing properties to MSBUILD Task
问题描述
我正在尝试为所有具有属性的项目文件调用 msbuild 任务.我使用硬编码配置和平台组合四次调用 msbuild 任务.类似的东西
I am trying to call msbuild task for all the project files with properties. I call the msbuild task four times with hardcoded configurations and platform combination. Something like
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" Properties="Configuration=Debug;Platform=Win32" BuildInParallel="true"/>
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" Properties="Configuration=Debug;Platform=x64" BuildInParallel="true"/>
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" Properties="Configuration=Release;Platform=Win32" BuildInParallel="true"/>
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Build" Properties="Configuration=Release;Platform=x64" BuildInParallel="true"/>
但我想将此属性作为 ItemGroup 提供,就像这样
But I want to provide this property as ItemGroup something like this
Configuration=%(BUILD_CONFIG.Identity);Platform=%(BUILD_PLATFORM.Identity)
代码示例MyProject.vcxproj
Code sample MyProject.vcxproj
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="BuildAll" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="BuildAllConfiguration.vcxproj"/>
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{E6B6F967-3BE3-428F-9288-3F838B8E726A}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>MyProject</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
...
Similar Configuration Details for release and Platforms x64
该项目文件包含 BuildAllConfiguration.vcxproj
This project file includes BuildAllConfiguration.vcxproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BUILD_PLATFORMS>Win32;x64</BUILD_PLATFORMS>
<BUILD_CONFIGURATION>Debug;Release</BUILD_CONFIGURATION>
</PropertyGroup>
<Target Name="BuildAll">
<ItemGroup>
<CONFIGURATION Include="$(BUILD_CONFIGURATION.Split(';'))"/>
<PLATFORM Include="$(BUILD_PLATFORMS.Split(';'))"/>
<ProjectToBuild Include="$(MSBuildProjectFile)">
<Properties>Configuration=%(CONFIGURATION.Identity);Platform=%(PLATFORM.Identity)</Properties>
<Targets>Build</Targets>
</ProjectToBuild>
</ItemGroup>
<Message Text="MSBUILD TASK input @(ProjectToBuild)"/>
<MSBuild Projects="@(ProjectToBuild)" />
</Target>
</Project>
此项目将调用 MyProject.vcxproj,其中包含格式不正确的目标构建和属性.我的期望是属性如下
This project will call MyProject.vcxproj with Target Build and Properties which is not getting wellformed. My expectation is that the properties goes as following
Properties=Configuration=Debug;Platform=Win32
Properties=Configuration=Release;Platform=Win32
Properties=Configuration=Debug;Platform=x64
Properties=Configuration=Release;Platform=x64
相反,属性按如下方式传递
Instead the properties are passed as following
Properties=Configuration=Debug;Platform=
Properties=Configuration=Release;Platform=
Properties=Configuration=;Platform=Win32
Properties=Configuration=;Platform=x64
推荐答案
您在这里需要一个交叉产品,如果您搜索它,您应该会找到很多 答案,但我认为如果您不知道它的名字,可能很难找到.像这样:
You need a cross-product here, if you search for that you should find plenty of answers, though I reckon if you don't know it's called that it might be hard to find. Something like this:
<Target Name="BuildAll">
<ItemGroup>
<CONFIGURATION Include="$(BUILD_CONFIGURATION.Split(';'))"/>
<PLATFORM Include="$(BUILD_PLATFORMS.Split(';'))"/>
<!-- cross product of both -->
<ConfigAndPlatform Include="@(CONFIGURATION)">
<Platform>%(PLATFORM.Identity)</Platform>
</ConfigAndPlatform>
<ProjectToBuild Include="$(MSBuildProjectFile)"/>
</ItemGroup>
<MSBuild Projects="@(ProjectToBuild)" Properties="Configuration=%(ConfigAndPlatform.Identity);Platform=%(ConfigAndPlatform.Platform)" />
</Target>
一些注意事项:大写会使东西更难阅读,也许不要使用它们?此外,如果您将配置/平台放在 ItemGroup 而不是 PropertyGroup 中,则不需要额外的拆分逻辑:
Some notes: capitals make things harder to read, maybe don't use them? Also if you put your configurations/platforms in an ItemGroup instead of a PropertyGroup you don't need extra splitting logic:
<ItemGroup>
<Configuration Include="Debug;Release"/>
<Platform Include="Win32;x64"/>
<ItemGroup>
这篇关于将属性传递给 MSBUILD 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!