Bundle Java program for Mac users with maven from GNU / Linux

I am trying to bundle a java program for Mac users. I first found this article explaining how to do this with Ant, and then I found this one that seems perfect for Maven.

So, I added to my pom:

<plugin> <groupId>sh.tak.appbundler</groupId> <artifactId>appbundle-maven-plugin</artifactId> <version>1.1.0</version> <configuration> <mainClass>xxx</mainClass> <iconFile>xxx</iconFile> <jrePath>???</jrePath> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>bundle</goal> </goals> </execution> </executions> </plugin> 

(I also found this article that explains some details about Mac packages and why to use appbundler)

The only problem is that in every example I find, I see <jrePath>xxx.jdk</jrePath> . But I run this under Ubuntu, so I only have GNU / Linux jdk. Where can i find mac jdk? On oracle site, I can only find the dmg file. I extracted dmg and got hfs. I climbed to hfs and got pkg. I extracted pkg and now I have more files, I don’t know what to do with ...

+5
source share
1 answer

Here is the step-by-step that I did to do this with a test project on Ubuntu 16.04.1 LTS .

In your case, steps 1 through 3 will be executed on your GNU / Linux env and last on Mac OS X.

1. Download the JRE

Since you only need a JRE , the easiest way to do this is:

  • To go to the download area ,
  • Click JRE DOWNLOAD ,
  • Choose tar.gz the JRE version for Mac OS X , which is currently jre-8u112-macosx-x64.tar.gz .
  • Disconnect the contents of the archive in the folder of your choice, which we will call ${jre-folder} (for example, /foo/bar/jre1.8.0_112.jre ).

2. Create your test project

My typical maven project structure:

  Testproject
 └── src
 |  └── main
 |  └── java
 |  └── my
 |  └── pkg
 |  └── MyClass.java
 └── pom.xml

My class is my.pkg.MyClass , which actually performs an arbitrary task. Here, it simply removes the system properties into a temporary file, just to be able to easily verify that it was called:

 package my.pkg; import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class MyClass { public static void main(String[] args) throws IOException { Path path = Files.createTempFile("MyClass", "txt"); try (BufferedWriter writer = Files.newBufferedWriter(path)) { System.getProperties() .entrySet() .stream() .forEach( entry -> { try { writer.write(entry.getKey() + "=" + entry.getValue() + "\n"); } catch (IOException e) { throw new IllegalStateException(e); } } ); } } } 

My pom file:

 <?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>TestProject</groupId> <artifactId>TestProject</artifactId> <version>0.1-SNAPSHOT</version> <packaging>jar</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>sh.tak.appbundler</groupId> <artifactId>appbundle-maven-plugin</artifactId> <version>1.1.0</version> <configuration> <mainClass>my.pkg.MyClass</mainClass> <!-- For example <jrePath>/foo/bar/jre1.8.0_112.jre</jrePath> --> <jrePath>${jre-folder}</jrePath> <generateDiskImageFile>true</generateDiskImageFile> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>bundle</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 

3. Create your test project

Just run the mvn package appbundle:bundle command from the root of the TestProject directory.

This will create a dmg file in the target folder with the JRE for Mac OS X included , in this specific case it will be called TestProject-0.1-SNAPSHOT.dmg .

4. Check the test project

On target Mac OS X :

  • Double click the dmg file, it will automatically install the image,
  • Then you can double-click TestProject.app , you will see that the icon appears and disappears quickly, since the test program is quite short.
  • You can verify that it worked correctly by running cat $TMPDIR/MyClass* from the terminal, you will see the contents of a temporary file created by the test application.

5. Add resources to the dmg file

To add resources to the generated dmg file, you can use additionalResources with fileSet .

 <plugin> <groupId>sh.tak.appbundler</groupId> <artifactId>appbundle-maven-plugin</artifactId> <version>1.1.0</version> <configuration> ... <additionalResources> <fileSet> <directory>/path/to/my/resources/folder</directory> <includes> <include>*.pdf</include> </includes> </fileSet> </additionalResources> </configuration> ... </plugin> 

In this example, all pdf files from /path/to/my/resources/folder will be added to the generated dmg file.

6. Add resources to the application file

To add resources to the generated application file , you can use additionalResources with fileSet .

 <plugin> <groupId>sh.tak.appbundler</groupId> <artifactId>appbundle-maven-plugin</artifactId> <version>1.1.0</version> <configuration> ... <additionalBundledClasspathResources> <fileSet> <directory>/path/to/my/resources/folder</directory> <includes> <include>*.pdf</include> </includes> </fileSet> </additionalBundledClasspathResources> </configuration> ... </plugin> 

In this example, all pdf files from /path/to/my/resources/folder will be added to the generated application file in /Contents/Java/lib , they will be automatically included in the class path of your application, so you can easily get them .

+8
source

All Articles