【问题标题】:Visual Studio 2010 cannot load a project where the <ProjectConfiguration> elements are importedVisual Studio 2010 无法加载导入 <ProjectConfiguration> 元素的项目
【发布时间】:2011-04-08 14:11:12
【问题描述】:

我们有一个大型(约 800 个单独的项目)系统,我们正在从旧的构建系统迁移到 Visual Studio 2010。过去几周,我们为每个项目手动创建了 Visual Studio 项目文件(.vcxproj 格式)的项目,我们能够从命令行构建整个系统只使用 MSBuild.exe (WIN!!)。由于需要转换的项目数量众多,因此手动创建项目文件比使用 VS 项目向导创建它们更有效,因为我们之前的构建系统没有使用 Visual Studio 项目文件。

为了维护和遵守 DRY,我们将大部分构建配置(编译器/链接器开关、包含路径等)重构为常见的 .targets 和 .props 文件。由于每个项目的配置集都是相同的,我们还将包含 &lt;ProjectConfiguration&gt; 项的 &lt;ItemGroup&gt; 放入通用 .props 文件中,并且对于我们无人值守的夜间构建来说一切正常。

很遗憾,Visual Studio 2010 IDE(RTM 版本)无法加载这些项目。当我尝试加载项目时,我收到一条错误消息,提示“项目 [Foo] 不包含任何配置”。如果我手动将 .props 文件中的 &lt;ItemGroup&gt; 复制到任何项目中,IDE 就可以加载该项目。

在搜索时,我在 MS Connect 上发现了this 问题,但它被标记为“已关闭为外部”,并且 MS 回复说正在调查下一个 Visual Studio 公开版本的问题。手动将 完全相同 &lt;ItemGroup&gt; 元素添加到大约 800 个项目是不可接受的解决方法。由于项目在 VS 之外构建并完美运行,我不得不假设问题出在 Visual Studio 解析/加载项目文件的方式上。

有没有人能够找到解决此问题的方法?有没有人知道何时/是否会在 Visual Studio 中修复此问题?

【问题讨论】:

    标签: visual-studio-2010 msbuild


    【解决方案1】:

    我已经为这个问题创建了一个解决方法。这仅适用于 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>
    

    此文件是您单独的“属性表”,其中包含项目的所有常见属性,包括配置。根据您的需要定制它。重要的部分是&lt;Target&gt; 节点(它创建了一个名为Configure 的新目标)和&lt;Project&gt; 节点中的InitialTargets 属性(它告诉MSBuild 最初执行Configure 目标)。

    之后,将Configure.xsltDefault.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,它们就不会被替换或删除。

    【讨论】:

      【解决方案2】:

      这在 VS2012 上是可能的,但是您必须记住导入/初始化变量的顺序非常重要。

      你需要很早就在项目文件中,第一项。我认为系统 .props 文件可能需要配置才能正常工作,这就是您收到该错误的原因。在我破解了我的 VS proj 之前,我在 VS2012 上遇到了和你一样的错误。

      【讨论】:

        【解决方案3】:

        在这个问题中回答:Is it possible to move configuration definitions into a property sheet with MSBuild?

        这里是连接问题,表明它没有进入 RTM:https://connect.microsoft.com/VisualStudio/feedback/details/514008/vcxproj-doesnt-examine-imports-to-find-project-configurations-beta-2

        除了生成项目之外,我没有找到任何解决方法。一个技巧是在项目文件中放置一个标记值,例如 ${CommonConfigurations},然后使用字符串属性函数来替换它,生成全新的项目文件以用于 VS 解决方案。在这种情况下,我也会生成解决方案文件,因此您不必在两个地方维护项目列表。

        【讨论】:

        • 抱歉转贴。据我所知,此更改也没有使 SP1 成为(我还没有安装 SP1,所以我无法确认或拒绝)。
        • 进一步详细说明...就像我说的,我们已经能够使用 MSBuild 构建我们的系统,并在导入的文件中包含 &lt;ProjectConfiguration&gt; 项。我希望有人找到了在 VS IDE 中加载项目的方法。由于我们要尽快完成这项工作,因此我正在编写批处理脚本和 XSLT,以将 &lt;ItemGroup&gt; 添加到 678 个 .vcxproj 项目中。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多