CaubleStone Ink

.Net development and other geeky stuff

nAnt: The master build file

Posted on February 13th, 2009


If you have been following along with the other nAnt articles, this is the final step of setting up your base project. From here you will be able to add additional projects, processes, etc. Then if you have a CI server (cruise control.net, Team City, or some other solution) you are ready to go.

Ok so without further ado here is the master build file for our sample project.

<?xml version="1.0" encoding='iso-8859-1' ?>
<project

	default="build"
	xmlns="http://nant.sf.net/release/0.85/nant.xsd"
>

	<property name="root.dir" value="${directory::get-current-directory()}" />
	<include buildfile="${root.dir}\common.xml" />

	<fileset id="buildfiles.all">
	  <include name="${root.dir}/NantSupportLibrary/default.build"/>
	  <include name="${root.dir}/NantBuildSample/default.build"/>
	</fileset>

  <target name="build" depends="cleanup common.init display-current-build-info">
    <echo message="${directory::get-current-directory()}"/>
    <echo message="${root.dir}"/>
    <echo message="${root.dir}/common.xml"/>
  	<nant>
    	<buildfiles refid="buildfiles.all" />
	</nant>
  </target>
</project>

As you can see there is not really a whole lot different in this file than a standard build file. However we will start at the top.

The very first thing we do is set our “root.dir” property. Now I did this one a little differently to show you another way to do things if you wanted to. You can also just replace this line with the following in the sample project code and it will work just fine:


Either one will work in this case. Since this file is in the root of our source tree it’s just set to the current working directory.

Next as with the other files we include our common.xml file.

The next thing is a little new. Since this considered to be a master build file what we are going to do is setup a fileset that will tell the build file where all the project level build files are.

Important: The order in which you include the files is important. They will be executed from top to bottom so make sure you have any dependencies figured out and built in the proper order.

Last but not least we have our build target. This is the sucker that starts all the work. As you can see we have several dependencies. The first is a “cleanup” step. This will cleanup our output folder so we can make sure we always have a nice clean build. Next it runs the “common.init” again to make sure our output folders are in place and that we have our project.sources defined (needed to compile the code). Lastly we call the “display-current-build-info” target. This will display some of the settings we have, framework version, etc. you can add to this output in the common.xml file if you feel the need.

Once all those are run we get into the core of the task. Here we are going to essentially have nant spawn instances of itself to run all the build files we defined up above. It will basically loop through that list and run each build file sequentially.

Well that’s about it. Your ready to run the thing. If you downloaded the zip file you can just run nant in the root source folder and it should spit everything out for you.

After it’s done running you should have your output in the output folder.

I hope you enjoyed these posts and it helps you if you plan on using nAnt as a build tool. In the future I will add more articles that build off of these. Things like adding unit testing, calling external objects, using extensions like nAntContrib, etc.

If there is anything you’d like to see drop a line and let me know.

Download the zip sample project.