Visual Studio 2010无法加载其中< ProjectConfiguration>元素已导入 [英] Visual Studio 2010 cannot load a project where the <ProjectConfiguration> elements are imported

查看:88
本文介绍了Visual Studio 2010无法加载其中< ProjectConfiguration>元素已导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个大型(〜800个独立项目)系统,我们正在从一个旧的构建系统迁移到Visual Studio2010.在过去的几周里,我们已经为每个手动创建了Visual Studio项目文件(.vcxproj格式)项目,我们仅使用MSBuild.exe(WIN !!)就可以从命令行构建整个系统.由于需要转换大量项目,因此手动创建项目文件比使用VS项目向导创建项目文件更为有效,因为我们以前的构建系统并未使用Visual Studio项目文件.

We have a large (~800 individual projects) system that we are migrating from an old build system up to Visual Studio 2010. Over the last few weeks, we have manually created Visual Studio project files (.vcxproj format) for each of the projects and we are able to build the entire system from the command line using only MSBuild.exe (WIN!!). Because of the large number of projects that needed to be converted, it was more efficient to create the project files by hand than to create them using the VS project wizard, since our previous build system was not using Visual Studio project files.

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

For maintenance and to adhere to DRY, we have the majority of the build configuration (compiler/linker switches, include paths, etc.) refactored into common .targets and .props files. Since the set of configurations is the same for every project, we also put the <ItemGroup> containing the <ProjectConfiguration> items in the common .props file and everything works fine for our unattended nightly build.

不幸的是,Visual Studio 2010 IDE(RTM版本)无法加载这些项目.尝试加载项目时,出现错误消息"Project [Foo]不包含任何配置".如果我将<ItemGroup>从我们的.props文件中手动复制到任何项目中,则IDE可以加载该项目.

Unfortunately, the Visual Studio 2010 IDE (RTM version) is not able to load these projects. When I attempt to load the project, I get an error saying "Project [Foo] does not contain any configurations." If I manually copy the <ItemGroup> from our .props file into any of the projects, the IDE is able to load the project.

在搜索时,我发现该问题在MS Connect上出现,但是标记为已关闭为外部",MS答复说该问题正在为Visual Studio的下一个公共发行版进行调查.手动将完全相同 <ItemGroup>元素添加到约800个项目中是无法接受的解决方法.由于项目是在VS外部构建的并且可以完美运行,因此我必须假定问题出在Visual Studio解析/加载项目文件的方式中.

While searching I found this issue on MS Connect, but it is marked "Closed as External" with a reply from MS that the issue is being investigated for the next public release of Visual Studio. Manually adding the exact same <ItemGroup> element to ~800 projects is not an acceptable workaround. Since the projects build outside of VS and run perfectly, I have to assume that the issue is in the way Visual Studio is parsing/loading the project files.

有人能找到解决此问题的方法吗?是否有人知道何时/是否将在Visual Studio中修复该信息?

Has anyone been able to find a workaround for this issue? Does anyone have information about when/if this will be fixed in Visual Studio?

推荐答案

我已经创建了解决此问题的方法.这仅适用于VS2010,不适用于以前的版本,因为它需要MSBuild 4.

I've created a workaround for this problem. This only works on VS2010, and not on previous versions, since it requires MSBuild 4.

首先,创建一个名为Configure.xslt的文件,其中包含以下内容.

First, create a file named Configure.xslt with the following contents.

<?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的文件,其中包含以下内容.

Now, create a file named Default.props with the following contents.

<?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目标).

This file is your separate "property sheet" containing all of the common properties for your projects, including configurations. Customize it to your needs. The important parts are the <Target> node (which creates a new target named Configure) and the InitialTargets attribute in the <Project> node (which tells MSBuild to execute the Configure target initially).

然后,将Configure.xsltDefault.props都移到您选择的目录中.

After that, move both Configure.xslt and Default.props to a directory of your choice.

最后,在所有项目中使用以下模板.

Finally, use the following template in all your projects.

<?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能够首先打开项目-效果是相同的.

From the VS prompt, run an initial msbuild to populate your file with configurations, or else VS IDE won't open your project (because of the same bug which motivated this workaround in the first place). Alternatively, your template might contain any dummy configuration, just to enable the IDE to initially open the project -- the effect is the same.

已知的警告:您不能使用IDE创建每个项目的配置(因为IDE会强制将任何配置分组在ProjectConfigurations标签下).您可以直接在项目文件的XML数据中创建它们,并且一旦不标记它们ProjectConfigurations,它们就不会被替换或删除.

Known caveats: You can't create per-project configurations using the IDE (because the IDE will forcibly group any configurations found under the ProjectConfigurations label). You can create them directly in the project file's XML data, and, as soon as you don't label them ProjectConfigurations, they won't be replaced or deleted.

这篇关于Visual Studio 2010无法加载其中&lt; ProjectConfiguration&gt;元素已导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆