我已经为这个问题创建了一个解决方法。这仅适用于 VS2010,不适用于以前的版本,因为它需要 MSBuild 4。
首先,创建一个名为Configure.xslt 的文件,其内容如下。
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msbuild="http://schemas.microsoft.com/developer/msbuild/2003">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/msbuild:Project">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="msbuild:ItemGroup[@Label = 'ProjectConfigurations']">
<xsl:copy-of select="document('Default.props')/msbuild:Project/msbuild:ItemGroup[@Label = 'ProjectConfigurations']" />
</xsl:template>
</xsl:transform>
现在,使用以下内容创建一个名为 Default.props 的文件。
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" InitialTargets="Configure" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- This must come first, or else VS IDE's property sheets will choke. -->
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<!-- The following configurations will be pasted into the importing project file on demand. -->
<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>
</ItemGroup>
<!-- XLST task which manipulates the project file and performs an in-place replacement. -->
<Target Name="Configure" Condition="Exists('$(MSBuildThisFileDirectory)Configure.xslt')" Inputs="$(MSBuildProjectFile);$(MSBuildThisFileFullPath);$(MSBuildThisFileDirectory)Configure.xslt" Outputs="$(MSBuildProjectFile);$(OutDir)">
<Message Text="Configuring $(MSBuildProjectFile)..." />
<XslTransformation XslInputPath="$(MSBuildThisFileDirectory)Configure.xslt" XmlInputPaths="$(MSBuildProjectFile)" OutputPaths="$(MSBuildProjectFile).tmp" />
<Move SourceFiles="$(MSBuildProjectFile).tmp" DestinationFiles="$(MSBuildProjectFile)" />
<MakeDir Directories="$(OutDir)" />
</Target>
<!-- The remaining MSBuild imports. -->
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
此文件是您单独的“属性表”,其中包含项目的所有常见属性,包括配置。根据您的需要定制它。重要的部分是<Target> 节点(它创建了一个名为Configure 的新目标)和<Project> 节点中的InitialTargets 属性(它告诉MSBuild 最初执行Configure 目标)。
之后,将Configure.xslt 和Default.props 移动到您选择的目录。
最后,在您的所有项目中使用以下模板。
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Change the directory below according to the path containing the above files. -->
<Import Project="Directory\Default.props" />
<!-- The ItemGroup below will be effectively replaced by the same ItemGroup inside Default.props. -->
<ItemGroup Label="ProjectConfigurations" />
</Project>
在 VS 提示符下,运行初始 msbuild 以使用配置填充您的文件,否则 VS IDE 将无法打开您的项目(因为最初引发此解决方法的相同错误)。或者,您的模板可能包含任何虚拟配置,只是为了使 IDE 能够初始打开项目——效果是一样的。
已知警告:您不能使用 IDE 创建每个项目的配置(因为 IDE 会强制将ProjectConfigurations 标签下的任何配置分组)。您可以直接在项目文件的 XML 数据中创建它们,并且只要不标记它们ProjectConfigurations,它们就不会被替换或删除。