Visual Studio条件项目配置编辑器 [英] Visual Studio Conditional Project Configuration Editor

查看:107
本文介绍了Visual Studio条件项目配置编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中包含许多引用,并且所有引用都是有条件的,表明我已按如下所示手动编辑了Proj文件,

I have a project, that contains many references and they all are conditional, show I have manually edited Proj files as below,

  <ItemGroup Condition=" '$(Configuration)' == 'Release' ">
    <Reference Include="Assembly1">
      <HintPath>..\Release\Path\Assembly1</HintPath>
    </Reference>
    <Reference Include="Assembly2">
      <HintPath>..\Release\Path\Assembly2</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
    <Reference Include="Assembly1">
      <HintPath>..\Debug\Path\Assembly1</HintPath>
    </Reference>
    <Reference Include="Assembly2">
      <HintPath>..\Debug\Path\Assembly2</HintPath>
    </Reference>
  </ItemGroup>

请注意,这只是一个示例,只有两个配置,即Release和Debug,目前每个平台有4种配置,我们正在处理约12种配置的存储,其中3种用于.NET 3.5,.NET 4和.NET. 4.5

Note, this is just a sample, with only two Configurations, Release and Debug, currently we have 4 configurations per platform and we are dealing with storing near about 12 configurations, 3 for .NET 3.5, .NET 4 and .NET 4.5

我想知道还有其他更好的方法吗?

I wonder if there is any other better way then this?

  1. 是否有任何Visual Studio插件可以使我们在某种类型的UI中进行这些配置,而不是编辑xml,因为它变得越来越复杂.
  2. MSBuild中是否有任何脚本或任何可以读取一些自定义xml或某些数据存储的脚本,我们可以使这一过程自动化?
  3. 如果我为不同的配置创建了不同的VSProj文件,则确保正确添加内容文件非常耗时,但是唯一的问题是引用.
  4. 是否有其他工具可以轻松管理每个配置的引用?

我主要关心的是根据配置类型存储和管理引用.

My major concern is, storing and managing references as per configuration types.

推荐答案

我会这样做:

在名为"Common.props"的文件中

In a file named "Common.props"

<Project ...>
  <ItemGroup> 
    <Reference Include="Assembly1"> 
      <HintPath>..\$(Configuration)\Path\Assembly1</HintPath> 
    </Reference> 
    <Reference Include="Assembly2"> 
      <HintPath>..\$(Configuration)\Path\Assembly2</HintPath> 
    </Reference> 
  </ItemGroup> 
</Project>

您可以将其放入如图所示的单独的Common.props文件中,并使用Import元素将其包括"在所有项目文件中,这样就可以进行单点维护,如下所示:

You can put this into a separate Common.props file as shown and use the Import element to "include" it in all of your project files, so there is a single point of maintenance, like this:

<Project ...>
  <PropertyGroup>
    <Configuration Condition="'$(Configuration)' == ''>Debug</Configuration>
    ...other properties
  </PropertyGroup>
  <Import Project="Common.props" />
  ...rest of your C# project file
</Project>

上面显示的导入位置的重要意义在于,在未指定$(Configuration)默认值之后,如果未指定默认值,则会在导入中提供适当的值.当然,您可以将此默认值声明以及更多内容移动到Common.props文件中.如果始终这样做,项目文件通常会变成4个以上的属性(Project Guid,命名空间,程序集名称,项目类型),导入,要编译的文件列表以及任何不常用的程序集引用或项目引用.我的项目通常不会在其中包含任何条件属性,因为几乎总是可以将它们分解为共享导入.

What is significant about the placement of the import shown above is that it occurs after the definition of the default value for $(Configuration), in the case that none is specified, making a proper value available in the import. You can of course move this default value declaration, plus a whole lot more, into the Common.props file. If you do this consistenly, your project files typically become little more than 4 properties (Project Guid, Namespace, Assembly Name, Project Type), an import, a list of files to compile, and any uncommon assembly references or project references. My projects typically never have any conditional properties in them, since they can almost always be factored out to a shared import.

没有一个好的UI可以做到这一点,VS IDE UI非常有限,而且我从来没有使用过它来更改任何项目属性,而是依靠自定义工具,通常是自定义MSBuild任务来检查项目文件.确保它们是一致的,并使用PowerShell脚本对其进行修改以进行常见更改.

There is no good UI to do this, the VS IDE UI is very limited and I've come to never use it for changing any project properties, but instead rely on custom tooling, generally custom MSBuild tasks to check project files to make sure they are consistent, and PowerShell scripts to modify them for common changes.

摘录自 MSBuild骗术技巧#14-18

Excerpted from the book MSBuild Trickery tricks #14 - 18

添加了导入说明

这篇关于Visual Studio条件项目配置编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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