MSBuild基于调试/发布版本更改配置值

时间:2011-01-17 10:34:47

标签: msbuild

在我的app.config中,我有

<endpoint address="http://debug.example.com/Endpoint.asmx" stuff />

如何修改构建任务,以便在进行发布构建时将端点地址更改为

<endpoint address="http://live.example.com/Endpoint.asmx" stuff />

3 个答案:

答案 0 :(得分:4)

如果您的调试/发布配置分别命名为Debug和Release,则应执行此操作:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <endpoint address="http://debug.example.com/Endpoint.asmx" stuff />
  <!-- other things depending on Debug Configuration can go here -->
</PropertGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
  <endpoint address="http://live.example.com/Endpoint.asmx" stuff />
</PropertGroup>

答案 1 :(得分:2)

如果使用MSBuild extension pack,Xml任务将允许您更改XML文件中的条目。导入MSBuild文件中的自定义任务:

<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks" />

并更新XML值:

<PropertyGroup>
   <OldValue>http://debug.example.com/Endpoint.asmx</OldValue>
   <NewValue>http://live.example.com/Endpoint.asmx</NewValue>
</PropertyGroup>

<MSBuild.ExtensionPack.Xml.XmlFile 
    TaskAction="UpdateAttribute" 
    File="app.config" 
    XPath="/configuration/system.serviceModel/client/endpoint[@address='$(OldValue)']" 
    Key="address"
    Value="$(NewValue)"
/>

替换你的XPath,只在使用Condition的发布版本中执行它。

答案 2 :(得分:0)

如果您使用的是VS 2012或更高版本,则可以添加配置转换以替换构建时的值。如果您使用2010,则可以自动使用web.config,但对于app.configs,您需要对此处概述的* .csproj文件进行轻微破解: http://www.andrewdenhertog.com/msbuild/setting-web-app-settings-configuration-transforms-ducks-nuts-msbuild-part-8/

相关问题