· build nant

Nant: Populating templates

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="http://my-host.com:8080"/>


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

We would call the build file like so:

nant -buildfile:build-file.build GenerateConfigFiles -D:environment=dev

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

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket