Java Tip #1 - Build a Mac OSX installer

If you want to distribute your Java application in a Mac OSX installer and you want to do it using Maven the mini recipe below will get you started.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>myproduct-osx-installer</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>MyProduct OS X Installer</name>
    <build>
        <finalName>myproduct-osx-installer</finalName>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>myproduct-main-jar</artifactId>
            <version>${project.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <activation>
                <os>
                    <family>mac</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <execution>
                                <phase>generate-resources</phase>
                                <configuration>
                                    <target>
                                        <mkdir dir="${project.build.directory}/..//package/macosx" />
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>2.10</version>
                        <executions>
                            <execution>
                                <id>validate</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/jars</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.5.0</version>
                        <executions>
                            <execution>
                                <id>prepage-package-1</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>iconutil</executable>
                                    <arguments>
                                        <argument>-c</argument>
                                        <argument>icns</argument>
                                        <argument>-o</argument>
                                        <argument>${project.build.directory}/../package/macosx/MyProduct.icns</argument>
                                        <argument>${project.build.directory}/../src/main/icons/MyProduct.iconset</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>prepage-package-2</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>iconutil</executable>
                                    <arguments>
                                        <argument>-c</argument>
                                        <argument>icns</argument>
                                        <argument>-o</argument>
                                        <argument>${project.build.directory}/../package/macosx/MyProduct-volume.icns</argument>
                                        <argument>${project.build.directory}/../src/main/icons/MyProduct.iconset</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>package</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>${env.JAVA_HOME}/bin/javapackager</executable>
                                    <arguments>
                                        <argument>-deploy</argument>
                                        <argument>-native</argument>
                                        <argument>dmg</argument>
                                        <argument>-name</argument>
                                        <argument>MyProduct</argument>
                                        <argument>-appclass</argument>
                                        <argument>com.mycompany.MyProductMain</argument>
                                        <argument>-srcdir</argument>
                                        <argument>${project.build.directory}/jars</argument> 
                                        <argument>-srcfiles</argument>      
                                        <argument>myproduct-main-jar-${project.version}.jar</argument>
                                        <argument>-srcdir</argument>
                                        <argument>${project.build.directory}/../src/main/license</argument>
                                        <argument>-srcfiles</argument>
                                        <argument>LICENSE.txt</argument>
                                        <argument>-outdir</argument>
                                        <argument>target</argument>
                                        <argument>-outfile</argument>
                                        <argument>myproduct-osx-installer</argument>
                                        <argument>-BlicenseFile=LICENSE.txt</argument>
                                        <argument>-v</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.10</version>
                        <executions>
                            <execution>
                                <id>attach-artifacts</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${project.build.directory}/bundles/MyProduct-1.0.dmg</file>
                                            <type>dmg</type>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

        

And voila you now have a Mac OSX installer!

Posted September 30, 2016

Up