Knight Talks Tech

Starting with NAnt

As part of our build process we utilise NAnt scripts so I thought it would be useful (even just for my own purposes!) to make some notes as I get started with it.
Firstly, we are using NAnt 0.90 which last modified in May 2010, it can be download from SourceForge, the most recent version, 0.92, was modified in June 2012 and is available from this SourceForge link. I’m sticking with 0.90 as this is what we have in our environment.
Once download, extract and move to a suitable location on your computer.
The nant.exe itself is located in the bin folder, i.e. C:\Program Files\NANT-0.90\bin. We will call this executable and  pass two parameters; the build file name and a target. It is also possible to overwrite properties within the build by adding -D:<property name>=<value>. If the project has multiple targets, specific ones can be called by listing them on the command line or if a target is omitted, the default will run.
In this example we will create a simple build file that writes a message, sleeps for a short time and then writes a second message.
The build files are written in XML and it is imperative to get this XML correct as even missing a “-” from a closing comment “–>” will cause NAnt to fail. Also, it doesn’t like white space before the XML tag so this has to be fully left-justified.
Our basic build file looks like this:

<?xml version="1.0"?>
<project
 name="sleepTest"
 default="sleepTest"
 basedir=".">
 <description>Testing the sleep command.</description>
 <property name="debug" value="true" overwrite="false" />
 <target name="sleepMe" description="sleep for a determined period">
    <echo message="Sleeping" />
    <sleep minutes = "1"/>
    <echo message="Awake" />
 </target>
</project>

After the XML tag, we have our project, here we define the project name, the default target to execute if one is not supplied and the base directory from where files will be referenced.
The build file can contain only one project, each project can contain multiple targets and each target can perform multiple tasks.
As we have set the “debug” property to true, NAnt will build the project in debug mode – this can be overridden but only if the overwrite flag is set to false by passing -D:debug=false on the command line.
We have one target defined, “sleepMe”, containing three tasks. The first task will echo “Sleeping” to the window where NAnt is called from, the second task sleeps the process for one minute before the third task writes another message, “Awake”, to the window.
You’ll notice that the tasks are self-closing and the build file finishes up by closing the target and project tags.
Now we just need to call NAnt and have it build the project:

nant.exe -buildfile:"C:\Users\john.knight\OneDrive\Work\NAnt Scripts\sleep.build" sleepMe


Hope this helps you get started with NAnt, from this basic exercise we can create more functional build files resulting in fewer manual tasks being performed.

Exit mobile version