Maven、JavaFX和Launch4J-Maven-plugin - 如何打包JRE?

4
我正在使用maven和openJDK 12构建一个小型JavaFX应用程序。然而,我需要将我的.jar文件打包为含有bundled JRE的文件,以便在Windows操作系统上运行,而无需下载适当的JRE。
我还使用了launch4j-maven-plugin插件。我在SOF上尝试了几个解决方案,但都没有成功。有些主题提到了maven-shade-plugin,但对我并没有起作用。我无法将外部文件夹打包进去。
目前我的POM可以部分完成任务:
- 它将"C:/Program Files/Java/openjdk-12.0.2_windows-x64_bin/bin"文件夹打包到.jar文件中,但是".exes"未包含在内,这很糟糕。 - 它将POM的依赖项打包到.jar文件中。 - launcher4j-maven将它们全部打包成一个.exe文件。
我想要的是将JRE打包到.jar文件中,并设置launcher4J来使用它。是否必须将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>

相关的 Stack Overflow 主题:

如何使用 launch4j 将 JRE 捆绑在一起?

使用 Launch4J 将 JRE 打包成可执行文件

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

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


尝试使用Maven JLlink - seenukarthi
我认为Jlink实际上并没有为我解决这个问题。 - KenobiBastila
1
你是否运行了mvn javafx:jlink?(如果还没有module-info描述符,你将需要它)。如果由于某些非模块依赖项而无法工作,我肯定会选择jpackage(即使是早期访问)。 - José Pereda
使用这个POM,我的jar文件只有在我通过CMD命令行运行java jar时才能打开,我做错了什么? - KenobiBastila
1
不再有JRE了,如果你的意思是双击运行jar文件,那是不可能的。 - José Pereda
1
等等,什么?它不能用了吗?天哪。真的吗? - KenobiBastila
2个回答

3
这篇文章在30天内被浏览了108次。我将发布我找到并正在使用的解决方案。
我按照用户“José Pereda”在https://dev59.com/fFQJ5IYBdhLWcg3wVEZ0#54065502上的解决方案进行了操作。
使用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'
}


0
<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>com.howtodoinjava</groupId>
  <artifactId>JavaExeDemo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>JavaExeDemo</name>
  <url>http://maven.apache.org</url>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.7.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>shaded</shadedClassifierName>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.howtodoinjava.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.akathist.maven.plugins.launch4j</groupId>
            <artifactId>launch4j-maven-plugin</artifactId>
            <version>1.5.1</version>
            <executions>
                <execution>
                    <id>l4j-clui</id>
                    <phase>package</phase>
                    <goals>
                        <goal>launch4j</goal>
                    </goals>
                    <configuration>
                        <headerType>gui</headerType>
                        <jar>${project.build.directory}/${artifactId}-${version}-shaded.jar</jar>
                        <outfile>${project.build.directory}/howtodoinjava.exe</outfile>
                        <downloadUrl>http://java.com/download</downloadUrl>
                        <classPath>
                            <mainClass>com.howtodoinjava.ApplicationMain</mainClass>
                            <preCp>anything</preCp>
                        </classPath>
                        <icon>application.ico</icon>
                        <jre>
                            <minVersion>1.6.0</minVersion>
                            <jdkPreference>preferJre</jdkPreference>
                        </jre>
                        <versionInfo>
                            <fileVersion>1.0.0.0</fileVersion>
                            <txtFileVersion>${project.version}</txtFileVersion>
                            <fileDescription>${project.name}</fileDescription>
                            <copyright>2012 howtodoinjava.com</copyright>
                            <productVersion>1.0.0.0</productVersion>
                            <txtProductVersion>1.0.0.0</txtProductVersion>
                            <productName>${project.name}</productName>
                            <companyName>howtodoinjava.com</companyName>
                            <internalName>howtodoinjava</internalName>
                            <originalFilename>howtodoinjava.exe</originalFilename>
                        </versionInfo>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接