An Introduction to NANT
January 04, 2008 in C# by Michael Chrisman 0 Comments
NANT is a tool that allows you to automate building of your application. It's like Make, or MSBuild or Ant. NANT is a .Net Version of ANT so it has special tags for building your .Net applications. Build tools like this can add great value as you can automatic complex processes as well as automate the setting up the setup of a new developer, run unit tests after a successful compile, etc.
The first step is to install NANT. You can download it on SourceForge. What you get is a ZIP file. You unzip it into a directory (they recommend C:Program FilesNANT). Next you need to create a batch file. You need to place the batch file in a directory that is part of the "path". The C:windows (or C:winnt) folder will work. Call the file NANT.BAT and make it look like this:
1: @echo off2: "C:Program FilesNAntbinNAnt.exe" %*
If you installed NANT in a different location, then you will need to modify line two. That's it, NANT is now installed, let's talk a little bit about how it works. You run it by typing "NANT" in a command window. What it does is look for a file that ends with the extension ".BUILD" in the current directory. You want to make sure that there is only one .BUILD file. You can specify the .BUILD file name to run, but it's just easier to only have one. You can specify command line options with the NANT command. These options you get to define in the .BUILD file. The .BUILD file is an XML file. But really it's more than that, the tags are really a programming language.
Lets do a quick little sample. For this all you will need is to have .Net Framework, NANT and Notepad. First, create a new folder (call it anything you like). In that folder, create two files. The first call it HelloMom.cs. Open this file in Notepad and make it look like this:
1: public class HelloMom2: {3: static void Main()4: {5: System.Console.WriteLine("Hello Mom and Dad…");6: }7: }
Now create a second file and call it HelloMom.build. Open this file in Notepad and make it look like this:
1: xml version="1.0"?>2: <project name="HelloMom" default="build">3: <property name="output.dir" value="bin"/>4: <property name="debug" value="true"/>5:6: <target name="clean">7: <delete dir="${output.dir}"/>8: target>9:10: <target name="build" depends="clean">11: <mkdir dir="${output.dir}"/>12: <csc target="exe" output="${output.dir}/HelloMom.exe" debug="${debug}">13: <sources>14: <include name="HelloMom.cs"/>15: sources>16: csc>17: target>18:19: <target name="run" depends="build">20: <exec program="${output.dir}/HelloMom.exe"/>21: target>22: project>
Now open a command window and change into the folder with these two file. Type NANT and watch it go. NANT will create a folder called "bin" and then compile the program into that folder. If you look in the bin folder, you will see a HelloMom.exe. You could cd to the bin folder and then run the HelloMom.exe, but type this instead. While still in your base folder, type "NANT run". Nant will first make sure the file is compiled, then it will run it. You should see something like this:
There, now you have created your own IDE for .Net applications. (hehe). If you type "NANT clean" then it will just remove the bin folder and "NANT build" will do the same as just "NANT.' Now let's talk about the tags in the HelloMom.build file. On line two, you see the
Lines 3 and 4 are
The next tag is the
Well, that is our first look. But what can you really do with NANT? Well, it would be great for tasks that you have to do over and over. Or if you have a complex task you have to do only once in a while. Instead of trying to remember how to do it each time, you could just run you build file. But where a tool like this really shines is when you have a large team. As a tech lead, you could create .build files to setup each developers programming environments (create the directory structure, check out the code, etc), and/or allow developers to compile and run all unit tests before they check in their code. (If you download NUNIT, it has sample .build files that show you how to run the unit tests from NANT.)
NANT comes with a lot of built in tags, functions and properties. You can even create your own functions using C#. There is also create documentation that comes with NANT. Another great resource is Jean-Paul S. BoodHoo's blog as he does a series on using NANT.