java - Maven、JavaFX、Launch4J-Maven-plugin - 如何捆绑 JRE?

标签 java maven javafx launch4j

我正在使用 maven 和 openJDK 12 在 JavaFX 中构建一个小型应用程序。但是,我需要我的 .Jar 包含捆绑的 JRE,以便它可以在 Windows 操作系统上运行,而无需下载适当的 JRE。

我也在使用 launch4j-maven-plugin

我在 SOF 上尝试了几种解决方案,但没有成功。有些主题提到了 maven-shade-plugin 但它对我不起作用。我无法用它打包外部文件夹。

我当前拥有的 POM,部分完成工作:

  • 它打包了“C:/Program .jar 内的 Files/Java/openjdk-12.0.2_windows-x64_bin/bin”文件夹 但不包含“.exe”,这很糟糕。

  • 它将POM的依赖打包在.jar内

  • launcher4j-maven 将其全部打包在 .exe 中

我想要的是将JRE打包到.jar中并设置launcher4J来使用它。有人会帮助我吗?我还需要它才能工作,因为 javaFX 在用户/客户端计算机中执行起来很痛苦。

JRE 必须位于 .exe 之外吗?如果是,那么我如何设置maven为我做这件事?

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.sky</groupId>
    <artifactId>Maven-FX</artifactId>
    <version>2.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
        </dependency>


        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>11.0.2</version>
            <classifier>win</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx</artifactId>
            <version>11.0.2</version>
            <type>pom</type>
        </dependency>
    </dependencies>

    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/application.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/JRE</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>C:/Program Files/Java/openjdk-12.0.2_windows-x64_bin/bin</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>br.sky.main.Main</mainClass>
                    <executable>C:\Program Files\Java\openjdk-12.0.2_windows-x64_bin\bin\java.exe</executable>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>br.sky.main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>exe</nonFilteredFileExtension>
                        <nonFilteredFileExtension>dll</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>

                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>br.sky.main.Main</mainClass>
                        </manifest>
                    </archive>
                    <fileSet>
                        <directory>${basedir}/target/JRE</directory>
                        <outputDirectory>/jre/</outputDirectory>
                        <includes>
                            <include>/jre</include>
                        </includes>
                    </fileSet>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>




            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.7.25</version>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>gui</headerType>
                            <outfile>${project.build.directory}/${project.artifactId}-${project.version}.exe</outfile>
                            <jar>${project.build.directory}/${project.artifactId}-${project.version}-jar-with-dependencies.jar</jar>
                            <errTitle>Maven FX</errTitle>
                            <classPath>
                                <mainClass>br.sky.main.Main</mainClass>
                                <addDependencies>true</addDependencies>
                            </classPath>
                            <jre>
                                <path>./jre</path>
                                <minVersion>11.0.1</minVersion>
                            </jre>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


        </plugins>
    </build>
</project>

相关 SOF 主题:

How to get the JRE to bundle with launch4j?

Bundle a JRE into an exe using Launch4J

https://openjfx.io/openjfx-docs/#install-javafx

https://github.com/lukaszlenart/launch4j-maven-plugin/blob/master/src/main/resources/README.adoc

最佳答案

该内容在 30 天内被观看了 108 次。因此,我将发布我找到并且当前正在使用的解决方案。

我按照用户“José Pereda”的解决方案 https://stackoverflow.com/a/54065502/2280645

使用 OpenJFX 和 https://github.com/beryx/badass-runtime-plugin

但是,在这个答案中,我展示了一些对我来说不太清楚的选项,我敢打赌对于从 JavaFX 开始的学生或开发人员来说也不清楚:

  • 在 javaFX{} 中添加了模块:modules = [ 'javafx.controls','javafx.graphics', 'javafx.fxml'] 避免 jPackage 完成创建时出现编译错误和失败 设置。

  • 设置变量jpackageHome = 'C:/Program Files/Java/openjdk-14-jpackage+1-49_windows-x64_bin/' 以指出 jPackage 的位置。当您使用另一个 JDK 作为其他项目或 IDE 本身的主要构建时,这非常有用。这就是我的情况。

  • imageOptions = ['--icon', 'src/main/resources/images/logo.ico'] 正确设置图标。我不知道 Windows 需要 .ico 而不是 .png

  • compileJava.options.encoding 因为由于我使用拉丁符号,所以它破坏了我的几个文件。

我希望这些技巧以及 Jose pereda 的回答能够节省其他迷失或刚接触 JavaFx 的人的时间。

<小时/>
plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.runtime' version '1.7.0'
    id "com.github.johnrengelman.shadow" version "5.1.0"
}

repositories {
    mavenCentral()
}

ext {
    openJfxVersion = '13'
}

dependencies {
    compile 'org.openjfx:javafx-base:${openJfxVersion}:win'
    compile 'org.openjfx:javafx-controls:${openJfxVersion}:win'
    compile 'org.openjfx:javafx-graphics:${openJfxVersion}:win'
    compile 'org.openjfx:javafx-fxml:13'
}


javafx {
    version = "13"
    modules = [ 'javafx.controls','javafx.graphics', 'javafx.fxml'  ]
}

mainClassName = 'Main'

runtime {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']

    jpackage {
        jpackageHome = 'C:/Program Files/Java/openjdk-14-jpackage+1-49_windows-x64_bin/'


        if(org.gradle.internal.os.OperatingSystem.current().windows) {
            installerType = 'msi'
            imageOptions = ['--icon', 'src/main/resources/images/logo.ico']
            installerOptions = ['--win-per-user-install',
                '--win-dir-chooser',
                '--win-menu',
                '--win-shortcut',
                '--verbose',
                '--description','x ',
                '--name', 'x',
                '--vendor','x',
                '--win-upgrade-uuid','x']
        }
    }

}

compileJava {
    compileJava.options.encoding = 'ISO-8859-1'
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls,javafx.fxml'
        ]
    }
}

run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls,javafx.fxml'
        ]
    }
}



group = 'br.x'
version = '0.1'

tasks.withType(JavaCompile) {
    options.encoding = 'ISO-8859-1'
}
<小时/>

关于java - Maven、JavaFX、Launch4J-Maven-plugin - 如何捆绑 JRE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57613874/

相关文章:

java - 处理巨大的管道分隔文件

JAVA FXCollections LoadException 类不是有效类型

java - 如何在 JavaFX GUI 中显示二维数组的元素

java - 守护线程启动的线程也是守护线程吗?

javascript - 将命令注入(inject) JavaFX 托管的 Javascript 中?

java.lang.NoClassDefFoundError : org/apache/poi/hssf/usermodel/HSSFWorkbook 错误

java - 用于 Java 5 的 Selenium 的最新版本是什么?

java - 如何使用 CSS 更改controlsfx SegmentedButton 的边框?

java - Hadoop - 使用 Java 将 reducer 输出合并到单个文件

java - 如何为 openimaj 创建 pom.xml?