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!

Published Fri, Jul 29 2005 4:18 PM by Joshua Langemann
Filed under:

Comments

Tuesday, June 05, 2007 5:39 PM by Dee

# re: Using NAnt Targets, Building an MSI in NAnt

Ok, so I know how to build and msi file. How do I now execute it so I can install the application. I tried this:

<target name="test">

<echo message="Now executing installer"/>

<exec basedir="StudioLibsInstaller\Debug" program="StudioLibsInstaller.msi" verbose="true">

</exec>

</target>

But it gives this error:

'StudioLibsInstaller.msi' failed to start.

   The specified executable is not a valid Win32 application.

I can go to the dos prompt and do the install by simply typing StudioLibsInstaller.msi, without problems. I am also able to execute this from an msbuild project file (build.proj) using the task:

<Target Name="test">

<Message Text="This is a test"/>

<Exec Command="StudioLibsInstaller\Debug\StudioLibsInstaller.msi " ></Exec>

</Target>

So, why is nant failing to execute the msi file while I can run it from the dos prompt or even via an MSBuild's build.proj file ?

Friday, February 29, 2008 5:24 PM by Moinul Islam

# re: Using NAnt Targets, Building an MSI in NAnt

The following works for me:

<property name="VisualStudio8" value="C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe" />

<property name="build.base.dir" value="My_Build"/>

<property name="build.dir" value="${build.base.dir}\Release"/>

<target name="Build">

   <msbuild project="${solution.path}\myproj1\myproj1.csproj" target="Build" />

   <msbuild project="${solution.path}\myproj2\myproj2.csproj" target="Build" />

   <msbuild project="${solution.path}\myproj3\myproj3.csproj" target="Build" />

   <msbuild project="${solution.path}\myproj4.csproj" target="Build" />

</target>

<target name="BuildSetup" DependsOnTargets="Build" >

   <exec program="${VisualStudio8}"

         commandline="${solution.path}\MySetup\MySetup.vdproj /build release"

         workingdir="${build.dir}">

   </exec>

</target>

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 2 and 1 and type the answer here: