Pages Menu
TwitterRssFacebook

Posted by on May 17, 2010 in Development

Laying NAnt build files out cleanly

When you’re building your NAnt files, you want to have some separation between the different parts of the build process. Not only is this good for keeping things clean and clear, but it also means you can choose to run separate parts of the build process, should you wish to.

Here’s a template of a build file with each part of the process in a separate target. You can use this as a starting point if you wish.

<?xml version="1.0"?>
<project>
  <description>Your project description here.</description>
  <target name="noTarget" description="dummy target to prevent accidental running">
    <echo message="No build target specified."/>
  </target>
  <target name="build" description="entry point. Runs all tasks in order" depends="prepare,get,compile,test,publish"> </target>
  <target name="prepare" description="clear the build environment ready for use"> </target>
  <target name="get" description="retrieve all files needed from source control or elsewhere"> </target>
  <target name="compile" description="perform the actual compilation of code"> </target>
  <target name="test" description="perform any tests needed"> </target>
  <target name="publish" description="move the built application to somewhere useful"> </target>
</project>
 

There’s a couple of specific things I’ve done here, which may be different to what you see elsewhere. Firstly, I’ve specified a default target of “noTarget” which doesn’t do anything. This is to prevent accidentally running your build code. By default NAnt will look for any *.build file in the current folder and if it finds it, will run it. That’s why I’ve specified a default action that doesn’t do anything – if I run it by mistake, I know it won’t do anything damaging.

Secondly, there’s different targets for each of the parts of the build process. This means you can choose to, for example, just run the ‘prepare’ portion with the command nant prepare, or just the publish portion with the command nant publish.

You may also notice there’s another target: build. This is empty apart from a depends property. What this is saying is that, in order for this target to run it depends on the other targets running. This is a really neat way of tying all your other targets together. The depends attribute will ensure that all the targets run in order. If any of the sub-targets fail, the main target will stop and fail – it won’t carry on trying to run the other tasks.

If you’ve copied the template file above, you can test this out with the following commands. Unless you’ve added the NAnt build file to your PATH environment, for now put the build file in your NAnt’s bin directory (though this is just for demo, don’t do this in deployment!). Try the following commands:

nant : you should see nant will pick up the build file and automatically run the default target.

nant prepare : you should see nant will only run the prepare target.

nant build : you should see that, thanks to the depends rule, all the targets are executed in order.

Now that you’ve got a solid framework you can attack each section in turn, filling in tasks to fulfil the work you need each target to do. You can test as you go along by running each section in turn, and then when you’re happy, try integrating them all together. Of course, you can also modify the depends attribute of the build target if you wanted to test, for instance, only running the first two targets together.

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.