我可以在一个解决方案配置中构建一个项目的多个配置吗? [英] Can I build multiple configurations of a project within one solution configuration?

查看:220
本文介绍了我可以在一个解决方案配置中构建一个项目的多个配置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在相同的解决方案配置中两次构建相同的项目,更改一些#define标志以切换功能.这两个二进制文件将使用不同的名称进行部署.

I would like to build the same project twice in the same solution configuration, varying some #define flags to toggle features. Both binaries will be deployed with different names.

我知道可以使用的解决方案:

The solutions that I know could work:

  1. 添加解决方案配置-但是我将需要构建两次解决方案,我希望避免这种情况.这两个项目配置都将始终生成.

  1. Add a solution configuration - But I will then need to build the solution twice, which I would prefer to avoid. Both project configurations will always be built.

复制项目-但是当我只想维护其他配置时,我就有了维护新项目的开销.

Copy the project - But then I have the overhead of maintaining a new project when I only want to maintain a different configuration.

批处理构建-我避免使用批处理构建,因为我既使用devenv进行本地开发,也使用msbuild进行持续集成.

Batch build - I avoid using batch build as I use both devenv for local development and msbuild for continuous integration.

还有其他想法或建议吗?

Any other ideas or suggestions?

推荐答案

只是想出了一种方法来完成您所要求的工作.创建一个msbuild文件(我命名为mine multiple.proj)并在下面添加脚本.

Just figured out a way to do what you asked for. Create one msbuild file (I named mine multiple.proj) and add the script below.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Choose>
    <When Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
      <ItemGroup>
        <ProjectToBuild Include="$(MSBuildProjectName).csproj">
          <Properties>Configuration=Release</Properties>
        </ProjectToBuild>
      </ItemGroup>
    </When>
  </Choose>
  <Target Name="BeforeBuild">
    <Message Text="Building configuration $(Configuration)..." />
  </Target>
  <Target Name="AfterBuild">
    <MSBuild Projects="@(ProjectToBuild)"/>
  </Target>
</Project>

</type>
</this>

将脚本导入项目(csproj或vbproj)中:

Import the script on your projects (csproj or vbproj):

<Import Project="..\multiple.proj" />

此脚本告诉msbuild重新构建具有其他配置的项目,作为AfterBuild事件.我使用Debug/Release进行了示例,但是您可以轻松地更改脚本以支持其他配置,或者根据其他变量做出重新构建的决定.

This script tells msbuild to build again your project with another configuration as an AfterBuild event. I used Debug/Release to make the example, but you can easily change the script to support other configurations, or make the decision to build again based on other variables.

要小心,因为您一次要运行两个构建,因此构建错误可能更难理解.

Be careful because you're running two builds at once, so build errors can be harder to understand.

希望这会有所帮助.

这篇关于我可以在一个解决方案配置中构建一个项目的多个配置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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