Multiple config transforms (at build time)

The web.config transforms always looked like a really nice way to remove duplication. I had a few concerns though: they didn’t work with app.configs, and I don’t want to use MS Deploy.

There are a few solutions available for the first problem, e.g. SlowCheetah. But it turns out there’s a much simpler option available (as long as you’re not afraid of butchering a csproj file!).

Adding a transform is just a matter of a new content node:

<None Include="App.config" />
<None Include="App.DEV.config">
  <DependentUpon>App.config</DependentUpon>
</None>

Then, to generate the transform at build time (rather than “publishing”) add the following at the end of your csproj file:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile">
  <TransformXml Source="App.config" Transform="App.DEV.config" Destination="$(OutDir)\App.DEV.config" StackTrace="true" />
</Target>

Now, all your transformed config files will be in the bin dir after building, ready for deployment!

If you haven’t got VS installed on the build agent, you might need to copy the required DLLs. (Also, make sure you have SP1 installed, there’s a nasty bug with excess line breaks).

(All credit for this belongs to a co-worker of mine, but he doesn’t have a blog and it seems a shame to let it go to waste. Unless he found it on the internet, in which case “nul point”).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s