开发者

MSBuild Change config value based on debug / release build

In my app.config, I have

<endpo开发者_Go百科int address="http://debug.example.com/Endpoint.asmx" stuff />

How can I modify the build tasks so that when I do a release build it changes the endpoint address to

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


If your debug/release configurations are named Debug and Release respectively, this should do it:

<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>


If you use the MSBuild extension pack the Xml task will allow you to change an entry in an XML file. Import the custom tasks in your MSBuild file:

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

and update an XML value:

<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)"
/>

Substitute your XPath and only execute this during a release build using Condition .


If you're using VS 2012 or above, you can add a config transform to replace the values on build. If you're using 2010 this is available for web.config automatically, but for app.configs you'll need to do a slight hack on your *.csproj file outlined here: http://www.andrewdenhertog.com/msbuild/setting-web-app-settings-configuration-transforms-ducks-nuts-msbuild-part-8/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜