A complete Java Ant MacOS Jarbundler build script

Java Mac application FAQ: Can you share a Java/Mac Ant build script that uses the Jarbundler task to make my Java application look like a native Mac OS X application?

NOTE: This solution is for Mac OS X systems running versions of Java prior to Java 7. If I remember right, it only works on those systems, and therefore only on Mac OS X systems 10.6 and earlier. I'm working on new tutorials for Java 7 and Mac OS X 10.7, 10.8, 10.9, and newer.

I've written an awful lot over the last several years about how to build a Java application on a Mac OS X system so the application will look like a native Mac OS X application. I've even released some Ant build scripts to demonstrate how to build a Java application on Mac OS X using the Ant Jarbundler task.

Today, I just released the source code for a small but complete "Java on Mac OS X" project. This project includes all the Java source code, as well as a complete Ant Jarbundler build script that you can use to make your Mac Java applications look and feel just like a native Mac OS X application.

My Mac OS X Java/Ant Jarbundler build script

If you're just interested in the source code for my Mac OS X Java/Ant Jarbundler build script, here it is. You can copy and paste it as desired. However, be warned that no Ant build script will make complete sense unless you see it within the context of its Java project, so I encourage you to download the source code for my complete Mac Java project (linked to below).

<project name="XYLocator" default="dist" basedir="..">

  <!-- properties: basic -->
  <property name="build.dir" value="build" />
  <property name="jar.dir" value="jar" />
  <property name="lib.dir" value="lib" />
  <property name="release.dir" value="release" />
  <property name="src.dir" value="src" />
  <property name="classes.dir" value="classes" />
  <property name="resource.dir" value="resources" />

  <!-- properties: application-specific properties -->
  <property name="jar-file-name" value="XYLocator.jar" />
  <property name="manifest-file-name" value="XYLocator.manifest" />
  <property name="mac.aboutname" value="XYLocator" />
  
  <!-- properties: jarbundler properties (mac-only) -->
  <property name="jarbundler.name" value="${mac.aboutname}" />
  <property name="jarbundler.shortname" value="${mac.aboutname}" />
  <property name="jarbundler.signature" value="Alvin Alexander, devdaily.com" />
  <property name="jarbundler.mainclass" value="com.devdaily.xylocator.XYLocator" />
  <property name="jarbundler.icon" value="build/XYLocator.icns" />
  <property name="jarbundler.jvmversion" value="1.5+" />
  <property name="jarbundler.version" value="0.10" />
  <property name="jarbundler.infostring" value="DevDaily.com XYLocator v1.0, May, 2010" />
  <property name="jarbundler.build" value="100" />
  <property name="jarbundler.bundleid" value="com.devdaily.xylocator.XYLocator" />
  <property name="jarbundler.apple.laf.useScreenMenuBar" value="true"/>
  <property name="jarbundler.apple.awt.brushMetal" value="true"/>
  <property name="jarbundler.apple.awt.showGrowBox" value="true"/>
  <property name="jarbundler.com.apple.mrj.application.apple.menu.about.name" value="${mac.aboutname}"/>
  <property name="jarbundler.apple.awt.textantialiasing" value="true"/>
 
  <taskdef name="jarbundler" classname="net.sourceforge.jarbundler.JarBundler" />

  <path id="class.path">
    <fileset dir="${lib.dir}">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset>
  </path>

  <target name="init">
    <tstamp/>
  </target>

  <target name="create-classes-dir" depends="init">
    <mkdir dir="${classes.dir}"/>
  </target>

  <target name="clean">
    <delete dir="${classes.dir}"/>
    <!-- remove all the old jars from the jar directory -->
    <delete>
      <fileset dir="${jar.dir}">
        <include name="**/*.jar"/>
        <include name="**/*.zip"/>
      </fileset>
    </delete>
    <!-- remove the old version of the app -->
    <delete>
      <fileset dir="${release.dir}">
        <include name="**/*.app"/>
      </fileset>
    </delete>
  </target>

  <!-- COMPILE -->
  <!-- compile: compile all our code to the "classes" directory -->
  <!-- debug is off by default. see http://ant.apache.org/manual/CoreTasks/javac.html -->
  <target name="compile" depends="clean,create-classes-dir">
    <javac destdir="${classes.dir}" source="1.5" includeAntRuntime="false">
      <src path="${src.dir}"/>
      <exclude name="**/_*.java"/>
      <exclude name="**/Test*.java"/>
      <classpath refid="class.path"/>
    </javac>
  </target>

  <!-- CREATE-JAR -->
  <target name="create-jar" depends="compile">

    <!-- copy any image/resource files in our src directories to the same "classes" directory -->
    <copy todir="${classes.dir}">
      <fileset dir="${src.dir}">
        <include name="**/*.gif"/>
        <include name="**/*.jpg"/>
        <include name="**/*.png"/>
      </fileset>
    </copy>
    
    <!-- create the jar file from our compiled classes and manifest file -->
    <jar basedir="${classes.dir}" jarfile="${jar.dir}/${jar-file-name}" manifest="${build.dir}/${manifest-file-name}"/>
    
    <!-- copy any libraries our application depends on -->
    <copy todir="${jar.dir}">
      <fileset dir="${lib.dir}">
        <include name="**/*.jar"/>
        <include name="**/*.zip"/>
      </fileset>
    </copy>
  </target>

  <!-- BUNDLE -->
  <!-- mac/jar bundle: create our mac os x bundle based on our jar file, our libraries, and these properties -->
  <target name="dist" depends="create-jar">
    <jarbundler dir="${release.dir}"
                name="${jarbundler.name}"
                shortname="${jarbundler.shortname}"
                signature="${jarbundler.signature}"
                mainclass="${jarbundler.mainclass}" 
                icon="${jarbundler.icon}"
                jvmversion="${jarbundler.jvmversion}"
                version="${jarbundler.version}"
                infostring="${jarbundler.infostring}"
                build="${jarbundler.build}"
                bundleid="${jarbundler.bundleid}" >

      <jarfileset dir="${jar.dir}">
        <include name="**/*.jar" />
        <exclude name="**/CVS" />
      </jarfileset>

      <javaproperty name="apple.laf.useScreenMenuBar" value="${jarbundler.apple.laf.useScreenMenuBar}"/>
      <javaproperty name="apple.awt.brushMetal" value="${jarbundler.apple.awt.brushMetal}"/>
      <javaproperty name="apple.awt.showGrowBox" value="${jarbundler.apple.awt.showGrowBox}"/>
      <javaproperty name="com.apple.mrj.application.apple.menu.about.name" value="${jarbundler.com.apple.mrj.application.apple.menu.about.name}"/>
      <javaproperty name="apple.awt.textantialiasing" value="${jarbundler.apple.awt.textantialiasing}"/>
    </jarbundler>
  </target>

</project>

Source code for the complete Mac Java project

You can download the source code for my complete Java on Mac OS X project here. Just go to the Download section of that article, and download the free Java source code.

I hope this Ant build script (and accompanying source code) for Mac Java applications is helpful. At the very least I hope it gets you through some of the Java on Mac OS X build problems I ran into when first using the JarBundler Ant task for OS X.

Comments

Big THX for the build script. It was very helpful for my Project.

I hope you don't mind me adding some options to it for SWT Apps on OSX and reposting it. :)