How to build a Mac Java app (OS X 10.9+ and Java 7+)

As a quick note to self, I used the following Ant build.xml file in 2014 to build my Wikipedia Reader client application on Mac OS X 10.9 with Java 7.x:

<project name="WikiReader" default="create-bundle" basedir=".">

  <taskdef name="bundleapp" classname="com.oracle.appbundler.AppBundlerTask"/>
  <property environment="env"/>

  <!-- input stuff -->
  <property name="current.dir" value="." />
  <property name="scala-lib.dir" value="scala-libs" />

  <!-- output stuff -->
  <property name="release.dir" value="release" />

  <!-- clean -->
  <target name="clean">
    <echo message="clean task ..." />
    <!-- just needed for the first-time run -->
    <mkdir dir="${release.dir}"/>
    <!-- remove the old version of the app -->
    <delete dir="${release.dir}/WikiReader.app" />
  </target>

  <!-- create mac osx 'bundle' -->
  <target name="create-bundle" depends="clean">
  <bundleapp outputdirectory="${release.dir}"
        name="WikiReader"
        displayname="WikiReader"
        identifier="com.alvinalexander.wikireader.client.Client"
        shortversion="0.1"
        icon="WikiReader.icns"
        copyright="Alvin J. Alexander"
        applicationCategory="public.app-category.utilities"
        mainclassname="com/alvinalexander/wikireader/client/Client">

        <runtime dir="${env.JAVA_HOME}" />

        <!-- the only jar file needed when using sbt-assembly -->
        <classpath file="../target/scala-2.10/WikiReaderClient-assembly-0.1.jar" />

        <option value="-Xdock:icon=Contents/Resources/WikiReader.icns"/>
        <option value="-Dapple.laf.useScreenMenuBar=true"/>
        <option value="-Dcom.apple.macos.use-file-dialog-packages=true"/>
        <option value="-Dcom.apple.macos.useScreenMenuBar=true"/>
        <option value="-Dapple.awt.application.name=WikiReader"/>
        <option value="-Dcom.apple.smallTabs=true"/>

    </bundleapp>
  </target>

</project>

Once my Scala/Java application is built with this build script, I can run it just like any other Mac OS X application.

Note that I use sbt-assembly to compile my Scala application and convert it into one jar file before running this Ant build script. You can see it referenced as the WikiReaderClient-assembly-0.1.jar file.

(In the real world I have a shell script that (a) builds the application with SBT and sbt-assembly, and (b) runs Ant with this build script to (c) bundle my Mac application. You might be able to do all of that with SBT alone, but I haven’t looked into that.)

This script uses the Oracle “appbundler” technology that’s built into their OS X JDK. You can read more about how this works at this Oracle/Java URL.

Post new comment

The content of this field is kept private and will not be shown publicly.