Using NAnt Targets, Building an MSI in NAnt
I received another question about NAnt, asking what Targets were, and for direction in building a custom MSI package. Targets are a fairly simple concept so I'll tackle that first. As far as building MSI's, I can offer an example of how I do it, and pointers to where you can find additional information. Here goes!
Targets
Targets are basically a way of grouping tasks in way that can be called from elsewhere. You can call a target from elsewhere in your NAnt file, or refer to a specific target when calling NAnt.exe. Typically a NAnt file will contain a target called "build" that contains tasks and/or calls to other targets in your build file. Here is a simplified version of a build target that I use:
<target name="build" description="Build the application.">
<call target="clean" />
<call target="compile" />
<call target="discsetup" />
<call target="rununittests" />
<call target="fxcop" />
</target>
This target simply calls some other targets that contain tasks to do various things. For example, the "clean" target looks like this:
<target name="clean" description="Remove all files from the Working directory">
<delete>
<fileset basedir="${working.dir}\">
<include name="**" />
</fileset>
</delete>
<mkdir dir="${working.dir}" />
</target>
You can see that it executes a couple of tasks. First, the working directory (as in the working.dir property) is removed. Then a new working directory is created. BTW, I think that the current version of NAnt will do this just fine using the dir attribute of the delete task - feel free to improve on it!
Building Setup (MSI) Files
There are several ways of creating setup files through NAnt. If you use Visual Studio to configure your setup file (using a Setup Project), you can invoke Visual Studio through NAnt to build the file. If you use a third party application, you would build it in a similar fashion. You can also use a NAnt file with XML tags describing your MSI file in order to generate one, using a feature of NAntContrib.
Visual Studio
First of all, the Visual Studio method. Here's an example of how my NAnt script is set up to handle this:
<target name="build-msi" description="Build an MSI file for installation of the program">
<property name="msi-file-name" value="application\TellerSetup\${build.config}\TellerForPOSSE.msi" />
<delete>
<fileset basedir="application\TellerSetup\${build.config}">
<include name="*" />
</fileset>
</delete>
<mkdir dir="${working.dir}\log" />
<exec failonerror="false" basedir="c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE"
program="devenv.exe" commandline="${build.solution} /build ${build.config} /project TellerSetup /out ${working.dir}\log\msi.log" />
<fail if="${not file::exists(msi-file-name)}">The MSI failed to build. See log\msi.log for more details.</fail>
</target>
First of all, I set a property called "msi-file-name" with the path to the msi file that I am going to build. This is used later on to check that the file exists.
Next, I use a delete task to remove anything that currently exists in the folder where I'll be creating the MSI. This is to clean up any files left over from the last build, so that we can start with a clean slate.
mkdir is used to create the directory that will contain the log, if it doesn't already exist. We will use this to capture a log file of the Visual Studio activity, so that we can troubleshoot it later on if needed.
Finally, the exec task is used to run Visual Studio (devenv.exe) and tell it to build the setup project. There are various command-line options that you can pass into devenv.exe. This demonstrates a few of them. Note that the output from the command is sent to a log file in our log directory.
In order to determine if the MSI was built successfully or not, I simply test to see if the file exists. Ideally I would let the exec task fail if there were problems, but I found that there were valid conditions that caused devenv.exe to set the errorlevel anyways, causing NAnt to fail the build. A fail task that raises a build error if the file doesn't exist did the trick OK - and demonstrates calling functions from expressions as well. Since Visual Studio creates the actual .msi file at the end of the build process, I figured this was pretty safe.
That should do it! Note that you will need Visual Studio installed on your build machine in order for this to work - in my case, I actually have it installed on my build server (which isn't ideal, but hasn't caused any problems for me either). This method seems to work reasonably well=
NAntContrib
NAntContrib is a project that supplements NAnt by providing a bunch of additional tasks. You can download it and access the help from http://nantcontrib.sourceforge.net/. Among other cool things, NAntContrib includes an msi task to build MSI files.
I haven't used this task and don't know how well it works. You can get the documentation for it here. I believe that there is also a tool included in the download that allows you to generate the necessary XML from a Visual Studio Setup project - looks like the slingshot task, though the documentation says that it has been deprecated, so I'm not sure what you're supposed to do now. It would be interesting if you could have NAnt generate the XML from a Setup Project and then run it using MSI, without the need for Visual Studio on the server. Feel free to post comments here if you have more information or decide to try it out.
You can find more information about NAnt on the main NAnt page: http://nant.sourceforge.net. There's a simple example build file at http://nant.sourceforge.net/release/latest/help/fundamentals/buildfiles.html. All the best!