Tuesday, November 27, 2007

NAnt Create SFX Archive Task

I have been working on a installer for a .NET web application with a database back-end. The installer itself deserves a series of posts since it was fairly complicated to get everything to work. An integral part of the installer was using NAnt for various things. The final stage in creating the ready to deploy executable is packaging the files in a self-extracting archive that extracts and runs the setup. With the help of Winrar and NAnt, that task can be automated. Let's get started. Requirements:

WinRar SFX options file sample:

; The path to the setup executable Setup=Setup.msi ; Extract the files to a temporary directory TempMode ; Use semi-silent mode Silent=2 ; Overwrite any existing files Overwrite=1 ; The title of the SFX archive Title=Sample Setup ; The text to show initially when the user clicks on the SFX archive (will only matter if using Silent=0) Text { The installer will extract and run the setup }

NAnt build file sample:

<project name="SampleSFX" default="createSFX" basedir=".">
 <description>createSFX task for creating a sample self-extracting installer</description>

 <!-- The name of the archive to be created -->
 <property name="archiveName" value="SampleSFX" overwrite="false" />

 <!-- The target directory from where the build will be invoked -->
 <property name="targetDirectory" value="." overwrite="false" />

 <!-- The relative path (from the targetDirectory) to directory containing the files to be archived -->
 <property name="archiveDirectory" value="<RelativePathToTheDirectoryToArchive>" overwrite="false">

 <!-- The mask of the file extensions to be archived -->
 <property name="archiveFileMask" value="*.*" overwrite="false" />

 <!-- The path to the winrar executable -->
 <property name="winrarPath" value="c:\program files\winrar\winrar.exe" overwrite="false" />

 <!-- The winrar command line switches for creating the archive -->
 <property name="winrarSwitches" value="a -ep -ep1 -r -sfxdefault.sfx" overwrite="false" />

 <!-- The name of the sfx options file to use while creating the archive -->
 <property name="sfxOptionsFile" value="sfxoptions.txt" overwrite="false" />

 <!-- The relative path (from the targetDirectory) to icon file to be used for the created archive -->
 <property name="sfxIconFile" value="images\setupIcon.ico" overwrite="false" />

 <property name="archiveDirectoryFullPath" value="${path::combine(path::get-full-path(targetDirectory), archiveDirectory)}" overwrite="true" />
 <property name="sfxOptionsFileFullPath" value="${path::combine(path::get-full-path(targetDirectory), sfxOptionsFile)}" overwrite="true" />
 <property name="sfxIconFileFullPath" value="${path::combine(path::get-full-path(targetDirectory), sfxIconFile)}" overwrite="true" />

 <target name="createSFX" description="Creates the self-extracing installer archive">
  commandline="${winrarSwitches} -z&quot;${sfxOptionsFileFullPath}&quot; -iicon&quot;${sfxIconFileFullPath}&quot; &quot;${archiveName}&quot; &quot;${archiveDirectoryFullPath}\${archiveFileMask}&quot;"
  failonerror="true" />
To run this NAnt task:
  1. Open a command prompt in the targetDirectory you specified in the above build file.
  2. Run the build file with: nant -buildfile:<>

This will create a single executable file. When the user runs the executable, the setup files will be extracted and the "Setup.msi" (or whatever you specified in the sfx options file) will be executed.


  1. Worked perfectly. Only needed to correct the end tag on line 11. Excellent example of both how to use nant for creating sfx installers, and how to use nant / sfx options. Thanks.

  2. Glad you found it useful. I have a better way to do this now so I should make a new post about it.


// //]]>