如何在2021年使用Maven构建JavaFX应用程序



我需要支持无法正确构建和运行的遗留JavaFX应用程序。这个应用程序相当复杂,所以我在试验它时做了简单的";你好世界"项目我也无法编译它。我希望有人能指出我的错误。

我从JavaFX文档中提到的这个例子开始:https://github.com/openjfx/samples

owner@owner-desktop:~$ mkdir example
owner@owner-desktop:~$ cd example/
owner@owner-desktop:~/example$ git clone https://github.com/openjfx/samples/
Cloning into 'samples'...
remote: Enumerating objects: 1870, done.
remote: Counting objects: 100% (215/215), done.
remote: Compressing objects: 100% (154/154), done.
remote: Total 1870 (delta 75), reused 183 (delta 56), pack-reused 1655
Receiving objects: 100% (1870/1870), 385.56 KiB | 3.38 MiB/s, done.
Resolving deltas: 100% (702/702), done.
owner@owner-desktop:~/example$ cd samples/HelloFX/Maven/hellofx/
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.openjfx:hellofx >-------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hellofx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/owner/example/samples/HelloFX/Maven/hellofx/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hellofx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/owner/example/samples/HelloFX/Maven/hellofx/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.042 s
[INFO] Finished at: 2021-06-23T10:10:38+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project hellofx: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

好吧,也许pom文件有点过时了,它缺少maven.comfiler.source和maven.comiler.target。让我们添加它们:

<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>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javafx.version>11</javafx.version>
<javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

现在它确实编译了。。。

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ mvn package
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.openjfx:hellofx >-------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
...CUT FOR BREVITY ...
[INFO] Building jar: /home/owner/example/samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.561 s
[INFO] Finished at: 2021-06-23T10:28:19+03:00
[INFO] ------------------------------------------------------------------------

但似乎<mainClass>javafx-maven插件的指令对创建和填充JAR的清单没有帮助。

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.10)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, mixed mode, sharing)
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ java -jar /home/owner/example/samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT.jar 
no main manifest attribute, in /home/owner/example/samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT.jar

让我们复制粘贴一些我以前在stackoverflow上找到的pom片段。我的理解是,它将指示maven组装插件制作自定义jar,除了普通的jar之外,还包括其中的所有依赖项。

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ cat pom.xml 
<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>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javafx.version>11</javafx.version>
<javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>HelloFX</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ mvn package
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.openjfx:hellofx >-------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
...CUT FOR BREVITY...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.313 s
[INFO] Finished at: 2021-06-23T10:42:40+03:00
[INFO] ------------------------------------------------------------------------
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ java -jar target/hellofx-1.0-SNAPSHOT-jar-with-dependencies.jar 
Error: JavaFX runtime components are missing, and are required to run this application
owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ 

太好了,我想它是想告诉我它缺少jmods。让我们下载它们:

owner@owner-desktop:~/example/samples/HelloFX/Maven/hellofx$ cd ~/example/
owner@owner-desktop:~/example$ ls -la openjfx-11.0.2_linux-x64_bin-jmods(1).zip 
-rw-rw-r-- 1 owner owner 38699001 юни 23 10:46 'openjfx-11.0.2_linux-x64_bin-jmods(1).zip'
owner@owner-desktop:~/example$ unzip openjfx-11.0.2_linux-x64_bin-jmods(1).zip 
Archive:  openjfx-11.0.2_linux-x64_bin-jmods(1).zip
creating: javafx-jmods-11.0.2/
inflating: javafx-jmods-11.0.2/javafx.graphics.jmod  
inflating: javafx-jmods-11.0.2/javafx.media.jmod  
inflating: javafx-jmods-11.0.2/javafx.controls.jmod  
inflating: javafx-jmods-11.0.2/javafx.fxml.jmod  
inflating: javafx-jmods-11.0.2/javafx.base.jmod  
inflating: javafx-jmods-11.0.2/javafx.web.jmod  
inflating: javafx-jmods-11.0.2/javafx.swing.jmod  

现在让我们开始吧:

owner@owner-desktop:~/example$ java -jar samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT-jar-with-dependencies.jar --module-path ./javafx-jmods-11.0.2/ --add-modules=javafx.base,javafx.graphics,javafx.controls,javafx.fxml
Error: JavaFX runtime components are missing, and are required to run this application
owner@owner-desktop:~/example$ 

到目前为止,我已经没有什么想法了。我做错了什么?

您试图同时做几件事,这从来都不是一个好主意,而且您没有严格遵循文档。您是否尝试过如文档中所示使用mvn clean javafx:run运行您的样本?

当将一个遗留应用程序移植到最近的JDK/JavaFX时,你应该做的第一件事就是编译它,并使用JavaFXMaven插件运行它。如果它是为JDK8及其包含的JavaFX编写的旧应用程序,那么这可能已经是一个挑战,因为有一些API更改和其他事情。

一旦你解决了这些问题,并运行了你的应用程序,你就可以开始考虑打包了。现在的JavaFX应用程序不应该只是被扔进一些大的jar文件中,因为这只会导致很多其他问题。相反,您应该考虑使用jpackage来创建一个合适的应用程序安装程序。你可能想看看这里:https://github.com/dlemmermann/JPackageScriptFX(注意:我对此有偏见。(

最新更新