Configuration file magic via Smith.BuildExtensions

by Bjørn Bouet Smith 3. February 2012 21:00

I am sure everyone have had the "pleasure" of having to maintain configuration files across projects and even solutions, only to copy and paste configuration data between them, to keep them in sync, and have had the same issues that everyone else has had. i.e. Missing variables in one project, missing sections and so forth.

So have I, and in my previous work we used Nant and a custom built script to transform our app.config and web.config into the correct version for the given target we were building.

In a new work we are having the same exact problem, surprise Smile - and instead of "poluting" our code base with nant. (We are running TFS, so nant does not fit well in that) - I decided to build my own MSBuild Task that could do basically the same, i.e. transform a configuration template, exchanging "tokens" or variables with configured elements or values from one or many configuration files.

I have done that now, and you can see it all in its simple splendor at codeplex.

But basically you add a little stuff to the project files of the projects where you want to use the configuration sharing and transformation, and you create your templates for your app.config and web.config and a few files for the variables and the next time you build, you will get a configuration files that matches the Build Target you selected in visual studio - with warnings and errors in the Error List if you have missing configuration variables for a given build target.

The following information is copied from codeplex, where you can see more elaborate examples.

To start using the Smith Build extensions is really easy, simply download the code, build it and copy the Smith.BuildExtensions.dll to a directory of your choosing.


Then either create or copy the provided examples of config files and put those in another directory of your choosing.

Then you need to change all project files that you want transformations for.

Add the following line to the project that you want to have configuration transformations in:

<UsingTask TaskName="ConfigTransformTask" 
AssemblyFile="Smith.BuildExtensions.dll" />


But remember to change the AssemblyFile attribute to point to where you put the compiled Smith.BuildExtensions.dll file.


Uncomment the
<Target Name="BeforeBuild">


target and add the following to the target:

<Target Name="BeforeBuild">
   <ConfigTransformTask ConfigBaseDir="..\Configs" 
ConfigTemplate="App.config.base.config" 
Configuration="$(Configuration)" Outputfile=".\App.config" />
</Target>


Where the ConfigBaseDir is where you have placed the app.config and web.config templates and the build specific settings files.

ConfigTemplate is the name of the template to use for transformation, i.e. if you are doing this in a web project choose your web.config.base.config file, and the app.config.base.config file if its a normal project or test project.

The OutputFile attribute controls what filename to write the file to, i.e. again for a web project use Web.config and App.config for other projects.

To see a full project file example, head over to the Project file example page

To see how to create the xml configuration files, head over to the Xml examples page.

I hope whoever reads this will find it just as exiting that I do, and will be a happy user of it Laughing

Tags: , ,

.NET | c# | Configuration

Comments (1) -

2/8/2012 11:06:12 AM #

trackback

App.config Configuration file magic via Smith.BuildExtensions

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com | Reply

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About me

Even though I have been working with programming for 15 years now, I still get amazed of how little I know :)

That is one of the great things in computers, there are always someone better than you. Someone you can ask for help.

Follow me on twitter

Ads