Mark Needham

Thoughts on Software Development

Nant: Populating templates

without comments

One of the common tasks that we need to do on every project I’ve worked on is ensure that we can create a web.config file for the different environments that we need to deploy our application to.

Nant has quite a neat task called ‘expandproperties‘ which allows us to do this quite easily.

In our build file we would have the following:

  <property name ="configFile" value="${environment}.properties" readonly="true"/>
  <if test="${not file::exists(configFile)}">
    <fail message="Configuration file '${configFile}' could not be found." />
  <include buildfile ="${configFile}" />
  <target name="GenerateConfigFiles">
    <foreach item="File" property="TemplateFile">
          <include name="ProjectDirectory/**.template"/>
        <copy file="${TemplateFile}" tofile="${string::replace(TemplateFile,'.template','')}" overwrite="true">
            <expandproperties />

There would be corresponding ‘.template’ and ‘.properties’ files containing the various variables we want to set, like so:

	 <property name="SomeHost" value=""/>


<?xml version="1.0"?>
      <add key="SomeHost="${SomeHost}"/>

We would call the build file like so:

nant GenerateConfigFiles -D:environment=dev

We can then change that ‘environment’ variable depending which one we need to generate the configuration files for.

Be Sociable, Share!

Written by Mark Needham

January 16th, 2010 at 12:13 am

Posted in Build

Tagged with ,