使用GNU/Linux的maven为Mac用户捆绑Java程序

Sha*_*oux 8 java macos maven

我正在尝试为Mac用户捆绑一个java程序.我首先发现这篇文章解释了如何使用Ant,然后,我发现对于Maven来说似乎是完美的.

所以我加入了我的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>
Run Code Online (Sandbox Code Playgroud)

(我还发现这篇文章解释了有关Mac软件包的一些细节以及为什么要使用appbundler)

唯一的问题是,在我发现的每一个例子中,我都知道<jrePath>xxx.jdk</jrePath>.但是我在Ubuntu下运行它,所以我只有GNU/Linux jdk.我在哪里可以找到Mac jdk?在oracle网站上,我只能找到dmg文件.我提取了dmg并获得了一个hfs.我安装了hfs并获得了一个pkg.我提取了pkg,现在有更多文件,我不知道该怎么办...

Nic*_*tto 12

以下是我一步一步完成的测试项目Ubuntu 16.04.1 LTS.

在您的情况下,步骤1到3将在您的GNU/Linux环境中完成,最后一个在Mac OS X上完成.

1.下载 JRE

因为你只需要JRE,最简单的事情是:

  1. 要去下载区,
  2. 点击JRE DOWNLOAD,
  3. 选择tar.gz的版本JREMac OS X这是目前jre-8u112-macosx-x64.tar.gz.
  4. 解压缩我们将要调用的文件夹中的存档内容${jre-folder}(例如/foo/bar/jre1.8.0_112.jre).

2.创建我的测试项目

我典型的maven项目结构:

TestProject
??? src
|   ??? main
|       ??? java
|           ??? my
|               ??? pkg
|                   ??? MyClass.java
??? pom.xml

我的班级my.pkg.MyClass实际上做了一项任意的任务.在这里,它只是将系统属性转储到一个临时文件中,只是为了能够轻松检查它是否已被调用:

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);
                        }
                    }
                );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的pom档案:

<?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>
Run Code Online (Sandbox Code Playgroud)

3.构建我的测试项目

只需mvn package appbundle:bundle从目录的根目录启动命令即可TestProject.

这将在包含for的文件夹中构建dmg文件,在这种特殊情况下将调用它.targetJREMac OS XTestProject-0.1-SNAPSHOT.dmg

4.测试我的测试项目

在目标上Mac OS X:

  1. 双击dmg文件,它会自动挂载图像,
  2. 然后你可以双击TestProject.app,你会看到一个图标出现并迅速消失,因为测试程序很短
  3. 您可以通过cat $TMPDIR/MyClass*从终端启动来检查它是否正常工作,然后您将看到测试应用程序创建的临时文件的内容.

5.将资源添加到dmg文件

将资源添加到生成的dmg文件,你可以使用additionalResources一个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>
Run Code Online (Sandbox Code Playgroud)

此示例将所有pdf文件添加/path/to/my/resources/folder到生成的dmg文件中.

6.向应用程序文件添加资源

将资源添加到生成的应用程序文件,你可以使用additionalResources一个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>
Run Code Online (Sandbox Code Playgroud)

这个例子将添加所有pdf从文件/path/to/my/resources/folder到生成的应用程序文件/Contents/Java/lib,他们将被自动纳入到应用程序的类路径,这样你就可以方便地访问它们.