Publishing an MSI using Ant in NetBeans

In times of flux for my Java desktop apps (e.g. lots of new features or bug fixes) I used to spend a lot of time manually editing configurations for my favourite installer program (Caphyon Advanced Installer) to build MSIs, doing the same things over and over, incrementing the version number. Enough. As part of my new-found customised NetBeans builds (see earlier posts) I decided to automate this process.

I already have major version numbers in my build scripts, which I update by hand if there is a major release:

<!-- Version info -->
<property name="project.version.major" value="1" />
<property name="project.version.minor" value="0" />

Then I get the latest SVN revision number of my working copy by calling on SVN:

<!-- find out revision number of HEAD -->  
   <echo>Getting SVN log...</echo>
   <exec executable="svn" outputproperty="svnlog.out">
        <arg line="log ${homedir}/.. -r HEAD -q"/> 
    </exec>
   <echo>${svnlog.out}</echo>
  
    <!-- parse revision number  -->
    <propertyregex property="project.version.svnrevision" input="${svnlog.out}" select="\1">
        <regexp pattern="r([0-9]*)"/>
    </propertyregex>
    <echo>Latest revision is: ${project.version.svnrevision}</echo>
    <property name="project.version.full" value="${project.version.major}.${project.version.minor}.${project.version.svnrevision}" />

I replace the old version with the new full version (major.minor.revision) in my application’s config file in the dist directory, before the MSI is built from the build folder, so that the app can access this value when it runs and display it in its ‘About’ menu. YOu can do this using aAnt’s replaceregexp task:

<replaceregexp file="${project.config.file}"
                         match="build\.version=[0-9]+\.[0-9]+\.[0-9]+"
                         replace="build.version=${project.version.full}"/>

Then, I call AdvancedInstaller to edit the MSI version to match the version numbers of my project:

<exec executable="${advanced.installer.exe}" resolveexecutable="true" failonerror="true">
   <arg line="/edit ${project.installer.config} /SetVersion ${project.version.full}"/>
</exec>

Finally I call AdvancedInstaller to build the MSI:

<exec executable="${advanced.installer.exe}" resolveexecutable="true" failonerror="true">
   <arg line="/build ${project.installer.config}"/>
</exec>

So now I can set the project config to ‘Publish’ in netbeans, hit ‘build’, and voila! I have an MSI for my app with the correct version number ready to deploy.

posted 2 years ago