使用GNU / Linux的maven为Mac用户捆绑Java程序 [英] Bundle Java program for Mac users with maven from GNU/Linux

查看:106
本文介绍了使用GNU / Linux的maven为Mac用户捆绑Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以我加入了我的pom:

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>

(我还发现这篇文章解释了有关Mac捆绑包的一些细节以及使用appbundler的原因)

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

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

The only issue is that on every example I found, I see <jrePath>xxx.jdk</jrePath>. But I am running this under Ubuntu, so I only have the GNU/Linux jdk. Where can I find the Mac jdk ? On the oracle website, I can only find the dmg file. I extracted the dmg and got an hfs. I mounted the hfs and got a pkg. I extracted the pkg, and have now more file I don't know what to do with...

推荐答案

这里有一步按照我在 Ubuntu 16.04.1 LTS 上的测试项目所做的工作。

Here are step by step what I did to do it with a test project on Ubuntu 16.04.1 LTS.

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

In your case steps 1 to 3 will be done on your GNU/Linux env and the last one on Mac OS X.

因为你只需要 JRE ,最简单的方法是:

As you only need the JRE, the easiest thing to do is:


  1. 转到下载区域

  2. 点击 JRE DOWNLOAD

  3. Mac选择 tar.gz 版本的 JRE OS X 目前 jre-8u112-macosx-x64.tar.gz

  4. 解开您选择的文件夹中的存档内容我们将调用 $ {jre-folder} (例如 /foo/bar/jre1.8.0 _112.jre )。

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



2。创建我的测试项目



我的典型maven项目结构:

2. Create my test project

My typical maven project structure:


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

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

My class my.pkg.MyClass which actually does an arbitrary task. Here, it simply dumps the system properties into a temporary file, just to be able to easily check that it has been 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);
                        }
                    }
                );
        }
    }
}

我的 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。构建我的测试项目



只需从目录的根目录<$ c启动命令 mvn package appbundle:bundle $ c> TestProject 。

3. Build my test project

Simply launch the command mvn package appbundle:bundle from the root of the directory TestProject.

这将在目标<中构建 dmg文件 / code>包含 JRE 的文件夹,包含 Mac OS X ,在这种特殊情况下,它将被称为 TestProject-0.1-SNAPSHOT.dmg

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

目标 Mac OS X


  1. 双击 dmg文件,它会自动挂载图片,

  2. 然后你就可以加倍了点击 TestProject.app ,你会看到一个图标出现并迅速消失,因为测试程序很短

  3. 你可以检查一下通过从终端启动 cat $ TMPDIR / MyClass * ,它可以正常工作,然后您将看到测试应用程序创建的临时文件的内容。

  1. Double click on the dmg file, it will automatically mount the image,
  2. Then you will be able to double click on TestProject.app, you will see an icon appear and quickly disappear as the test program is rather short
  3. You can check that it worked properly by launching cat $TMPDIR/MyClass* from a terminal, you will then see the content of the temporary file that has been created by the test application.






5。将资源添加到dmg文件



要将资源添加到生成的 dmg文件,您可以使用 additionalResources 带有 fileSet


5. Add resources to the dmg file

To add resources to the generated dmg file, you can use additionalResources with a 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>

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

This example will add all the pdf files from /path/to/my/resources/folder into the generated dmg file.

要向生成的应用程序文件添加资源,您可以使用 additionalResources 带有 fileSet

To add resources to the generated app file, you can use additionalResources with a 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>

此示例将添加所有 pdf 个文件从 / path /到/ my / resources / folder 到生成的应用程序文件 / Contents / Java / lib ,它们将自动包含在您的应用程序的类路径中,以便您可以轻松访问它们。

This example will add all the pdf files from /path/to/my/resources/folder to the generated app file into /Contents/Java/lib, they will be automatically included to the classpath of your application such that you will be able to access them easily.

这篇关于使用GNU / Linux的maven为Mac用户捆绑Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆