簡體   English   中英

Maven、JavaFX、Launch4J-Maven-plugin - 如何捆綁 JRE?

[英]Maven, JavaFX, Launch4J-Maven-plugin - How to bundle a JRE?

我使用 maven 和 openJDK 12 在 JavaFX 中構建了一個小應用程序。但是,我需要我的 .Jar 包含捆綁的 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 以使用它。 有人會幫助我嗎? 我也需要它來工作,因為在用戶/客戶端計算機中執行 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主題:

如何讓 JRE 與 launch4j 捆綁在一起?

使用 Launch4J 將 JRE 捆綁到 exe 中

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

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

這在 30 天內被觀看了 108 次。 所以我會發布我找到的並且我目前正在使用的解決方案。

我在https://stackoverflow.com/a/54065502/2280645 上遵循了用戶“José Pereda”的解決方案

使用 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'
}

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM