CaubleStone Ink

.Net development and other geeky stuff

nAnt: The project build file

Posted on February 13th, 2009


Next in the series surrounding nAnt is the process of creating the individual build files for each project. This is assuming of course that you are wanting a DLL, EXE, or what have you for each project in your visual studio solution. If you just want everything to compile into 1 dll or exe you can do that too with some minor adjustments of course.

This project file will assume that you have the common.xml file that we built in the last nAnt article. If you do not have it that is ok, it is included with the sample code for this article.

First things first lets create our build file by adding a new xml document object to our project.

Below is a basic project file we will use.

<?xml version="1.0" encoding='iso-8859-1' ?>
<project
	name="NantSupportLibrary"
	default="build"
	xmlns="http://nant.sf.net/release/0.85/nant.xsd"
>

	<property name="root.dir" value="../." />
	<include buildfile="${root.dir}/common.xml" />

	<target name="init" depends="common.init">
		<assemblyfileset id="project.references" basedir="${build.output.dir}">
			<include name="System.dll" />
			<include name="System.Core.dll" />
		</assemblyfileset>
    <resourcefileset id="project.resources">
    </resourcefileset>
	</target>

	<target name="build" description="Build ${project::get-name()}" depends="init common.compile.dll.cs" >
	</target>
</project>

You will notice that the first thing we have is the Project tag. This is the most root level element you can have. Just like the common.xml file you need to set the name and default command to run for the project. Also, make sure you have the xmlns element set correctly for the version of nAnt you are running.

The very first thing we need to do is set our root element. Basically we have the root.dir element to help ground everything to a consistent path structure. Makes having a common file and build process much easier. The root.dir property should use the syntax needed to get back to the root of your source tree. So if root level for the project is down one level you use “..” if it’s two levels “../..” etc.

Next we need to include our common.xml build file. The syntax as you can see if very straight forward. Now if you have not seen it before anything enclosed in a “${}” is special command for nAnt to do something. It is effectively a token, variable, parameter, etc. In the case of this include line it is concatenating the value of the variable root.dir with “/common.xml”.

Once we have that our project file will have access to all the targets we setup in the common file. So let’s setup a “init” target to initialize the filesets and other various things we need for this particular project. Since this project is pretty basic, as you can see, we do not have a whole lot in here. First we depend upon the “common.init” target from our common build file that defines our fileset for build files. Next we setup our assembly and resource file sets. In the Assembly fileset you will add a line for each reference you need in your project. If you have project references to other projects in your solution then add the appropriate project output reference here. In the sample I have this so you can see what I mean. If this was a windows forms app you would have resx files included in your resourcefileset element.

Last but not least we create our build target. This is what will be called when we run this build file. As you can see it depends on our “init” target we just created and “common.compile.dll.cs” from our common.xml build file. The description element actually will output the project name from the top of the build file.

One thing to remember the name attribute of the project element must be the name of the compiled object that you want to output. So if you wanted it to be “MyApp.Is.Awesome.dll” your project name would be “MyApp.Is.Awesome”.

In the next article we will walk through creating the master build file which will actually run all this stuff and provide what you want. A working build of a project.

Download the sample code