Tuesday, 30 December 2008

NAnt HowTo #2: How To Split Your Build File

Let's assume you have a large build file with many-many targets, properties, etc. After some time it becomes pretty hard to support and extend it. What can we do? As for me, the best option here is to split your build file to several pieces. Below goes an example on how you can do this.

My default.build file:

<?xml version="1.0"?>

<project
name="NAnt HowTo 2" default="all" xmlns="http://nant.sf.net/release/0.85-rc2/nant.xsd">
<!--
Includes -->
<include
buildfile="build.include" />
<include
buildfile="test.include" />

<target
name="all" description="Default target, calls all deployment tasks.">
<call
target="rebuild" />
<call
target="test" />
</target>

<target
name="rebuild" descripton="Rebuilds all projects." >
<call
target="clean" />
<call
target="build" />
</target>

<target
name="test" description="Runs all tests.">
<call
target="tests.unit.run" />
<call
target="tests.integration.run" />
</target>
</project>
build.include file:
<project xmlns="http://nant.sf.net/release/0.85-rc2/nant.xsd">
<target
name="clean">
<echo
message="Rebuild: clean" />
</target>

<target
name="build">
<echo
message="Rebuild: build" />
</target>
</project>
test.include file:

<project
xmlns="http://nant.sf.net/release/0.85-rc2/nant.xsd">
 <target name="tests.unit.run">
<echo
message="Test: unit tests" />
</target>

<target
name="tests.integration.run">
<echo
message="Test: integration tests" />
</target>
</project>
As you can see, actual inclusion takes place when you use <include> element and specify it's buildfile attribute to point to some .include file.

.include
files should contain root <project xmlns="http://nant.sf.net/release/0.85-rc2/nant.xsd"> element with necessary xmlns attribute. Without this attribute NAnt won't be able to parse this file and use it's targets.

Included files become a piece of main build file and share all properties with it. This means that properties from included file are visible in main file and vise-versa. I usually place commonly used properties to separate .include files, for example, I have projects.include file with all properties that point to project names and folders.

kick it on DotNetKicks.com

1 comment:

Shyama said...

Thanks for this info.
But I haven't been able to get it working yet. I'm getting an error message when using NAnt.exe, saying the target which I moved into the .include file does not exist in this project. So I'm guessing its not geting included. Should I specify the .include file name when running NAnt? Any idea what I may be doing wrong? Thanks for your help.