如何为多个构建配置选择不同的 app.config [英] How to select different app.config for several build configurations

查看:25
本文介绍了如何为多个构建配置选择不同的 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dll 类型的项目,其中包含 MSTest 集成测试.在我的机器上测试通过,我希望在 CI 服务器上也能发生同样的事情(我使用 TeamCity).但是测试失败了,因为我需要调整 app.config 中的一些设置.这就是为什么我想有一个单独的第二个 app.config 文件来保存 CI 服务器的设置.

所以我想要

<前>/Sln/项目app.config(我认为这是VS所要求的)app.Release.config(这是一个独立的独立配置文件)

因此,如果我在 CI 的构建配置中选择发布配置,我想使用 app.Release.config 文件而不是 app.config

问题
对于简单的 .dll 类型项目来说,这似乎并不简单.对于网络项目,我可以进行网络配置转换.我找到了一个关于如何为 dll 类型项目进行这些转换的技巧,但我不是 hack 的忠实粉丝.

问题
根据 .NET 项目(例如调试、发布等)的构建配置调整 app.config 文件的标准方法是什么?

解决方案

使用 、MSBuild) 将提供根据配置转换配置文件的功能.如果您在构建机器上构建解决方案,这将非常有用,您需要在构建机器上更好地控制准备发布产品的内容和方式.

例如你可以使用 web 发布 dll 的任务来转换任何配置文件

<UsingTask AssemblyFile="..	oolsuildMicrosoft.Web.Publishing.Tasks.dll"TaskName="TransformXml"/><属性组><!-- 输入配置文件的路径--><TransformInputFile>app.config的路径</TransformInputFile><!-- 转换文件的路径--><TransformFile>app.$(Configuration).config</TransformFile>的路径<!-- 输出网络配置文件的路径--><TransformOutputFile>输出project.dll.config的路径</TransformOutputFile></PropertyGroup><目标名称="转换"><TransformXml Source="$(TransformInputFile)"转换 =$(转换文件)"目的地="$(TransformOutputFile)"/></目标>

I have a dll-type project that contains MSTest integration tests. On my machine the tests pass, and I want the same to happen on a CI server (I use TeamCity). But the tests fail, because I need to tweak some settings in app.config. This is why I was thinking to have a separate second app.config file that will hold the settings for CI server.

So I would like to have

/Sln
 /Proj
  app.config (I think this is required by VS)
  app.Release.config (This is a standalone independent config file)

Thus if I select Release configuration in build config on CI, I would like to use app.Release.config file instead of app.config

Problem
This doesn't seem to be straightforward for simple .dll type projects. For web projects, I can do web config transformations. I found a hack how to do these transformations for a dll type project, but I am not a big fan of hacks.

Question
What is a standard approach to tweak app.config files depending on build config for .NET projects (such as Debug, Release, ...)?

解决方案

Use SlowCheetah plugin. For more options and details of how to use SlowCheetah keep reading.

As you have already noticed, there is no default and easy way to use different config files for a Library type (.dll) project. The reason is that the current thinking is: "You don't need to"! Framework developers reckon you need configuration for the executable file: be it a console, desktop, web, mobile app or something else. If you start providing configuration for a dll, you may end up with something I can call a config hell. You may no longer understand (easily) why this and that variables have such weird values coming seemingly from nowhere.

"Hold on", - you may say, "but I need this for my integration/unit testing, and it is a library!". And that is true and this is what you can do (pick only one, don't mix):

1. SlowCheetah - transforms current config file

You can install SlowCheetah - a Visual Studio plug-in that does all low level XML poking (or transformation) for you. The way it works, briefly:

  • Install SlowCheetah and restart Visual Studio (Visual Studio > Tools > Extensions and Updates ... > Online > Visual Studio Gallery > search for "Slow Cheetah" )
  • Define your solution configurations (Debug and Release are there by default), you can add more (right click on the solution in Solution Explorer > Configuration Manager... > Active Solution Configuration > New...
  • Add a config file if needed
  • Right click on config file > Add Transform
    • This will create Transformation files - one per your configuration
    • Transform files work as injectors/mutators, they find needed XML code in the original config file and inject new lines or mutate needed value, whatever you tell it to do

2. Fiddle with .proj file - copy-renames a whole new config file

Originally taken from here. It's a custom MSBuild task that you can embed into Visual Studio .proj file. Copy and paste the following code into the project file

<Target Name="AfterBuild">
    <Delete Files="$(TargetDir)$(TargetFileName).config" />
    <Copy SourceFiles="$(ProjectDir)ConfigApp.$(Configuration).config"
          DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>

Now create a folder in the project called Config and add new files there: App.Debug.config, App.Release.config and so on. Now, depending on your configuration, Visual Studio will pick the config file from a Config folder, and copy-rename it into the output directory. So if you had PatternPA.Test.Integration project and a Debug config selected, in the output folder after the build you will find a PatternPA.Test.Integration.dll.config file which was copied from ConfigApp.Debug.config and renamed afterwards.

These are some notes you can leave in the config files

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <!-- This file is copied and renamed by the 'AfterBuild' MSBuild task -->

    <!-- Depending on the configuration the content of projectName.dll.config 
        is fully substituted by the correspondent to build configuration file 
        from the 'Config' directory. -->

</configuration>

In Visual Studio you can have something like this

3. Use scripting files outside Visual Studio

Each build tool (like NAnt, MSBuild) will provide capabilities to transform config file depending on the configuration. This is useful if you build your solution on a build machine, where you need to have more control on what and how you prepare the product for release.

For example you can use web publishing dll's task to transform any config file

<UsingTask AssemblyFile="..	oolsuildMicrosoft.Web.Publishing.Tasks.dll"
    TaskName="TransformXml"/>

<PropertyGroup>
    <!-- Path to input config file -->  
    <TransformInputFile>path to app.config</TransformInputFile>
    <!-- Path to the transformation file -->    
    <TransformFile>path to app.$(Configuration).config</TransformFile>
    <!-- Path to outptu web config file --> 
    <TransformOutputFile>path to output project.dll.config</TransformOutputFile>
</PropertyGroup>

<Target Name="transform">
    <TransformXml Source="$(TransformInputFile)"
                  Transform="$(TransformFile)"
                  Destination="$(TransformOutputFile)" />
</Target>

这篇关于如何为多个构建配置选择不同的 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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