在 JDK11 之后发布 javafx 应用程序是否需要 javafx-maven-plugin?
Is javafx-maven-plugin required for shiping a javafx application after JDK11?
我正在尝试找出用于创建hello-world javafx 应用程序[=45]的最小值pom.xml
=] 可以作为 jar
+ ./lib
和 运行 与 java -jar
.
提供给客户
我用过:
pom.xml
<?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>com.example.javafx</groupId>
<artifactId>javafx-jdk14-example</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.javafx.app.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics </artifactId>
<version>14</version>
<classifier>linux</classifier>
</dependency>
</dependencies>
</project>
Main.java
package com.example.javafx.app;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER); // Override default
grid.setHgap(10);
grid.setVgap(12);
Button btn = new Button();
btn.setText("Clickme");
btn.setOnAction((t) -> System.out.println("Clicked!"));
grid.add(btn, 0, 0);
primaryStage.setScene(new Scene(grid, 640, 480));
primaryStage.show();
}
}
请注意,我没有在我的应用程序中使用 module-info.java
。
但是当我运行它时:
mvn clean package
cd target
/jdk14/bin/java -jar javafx-jdk14-example-1.0.0.jar
我得到:
Error: JavaFX runtime components are missing, and are required to run this application
如果我对 JDK10 使用相同的方法(还有 compiler.source/target=10
和注释掉 org.openjfx
deps)那么上面的工作正常。
是否有任何其他方法(在 JDK11+ 上)构建然后能够从目标目录中 运行 应用程序而无需使用 javafx-maven-plugin
?
在@Slaw 的帮助下,我找到了 运行 javafx 应用程序(在没有 javafx-maven-plugin
的情况下创建)的方法:
cd target
java -p lib/ --add-modules javafx.controls -jar javafx-jdk14-example-1.0.0.jar
我需要明确添加 javafx.controls
模块。
底线:
javafx-maven-plugin
不需要构建和发布 javafx 应用程序。
我正在尝试找出用于创建hello-world javafx 应用程序[=45]的最小值pom.xml
=] 可以作为 jar
+ ./lib
和 运行 与 java -jar
.
我用过:
pom.xml
<?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>com.example.javafx</groupId>
<artifactId>javafx-jdk14-example</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.javafx.app.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics </artifactId>
<version>14</version>
<classifier>linux</classifier>
</dependency>
</dependencies>
</project>
Main.java
package com.example.javafx.app;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER); // Override default
grid.setHgap(10);
grid.setVgap(12);
Button btn = new Button();
btn.setText("Clickme");
btn.setOnAction((t) -> System.out.println("Clicked!"));
grid.add(btn, 0, 0);
primaryStage.setScene(new Scene(grid, 640, 480));
primaryStage.show();
}
}
请注意,我没有在我的应用程序中使用 module-info.java
。
但是当我运行它时:
mvn clean package
cd target
/jdk14/bin/java -jar javafx-jdk14-example-1.0.0.jar
我得到:
Error: JavaFX runtime components are missing, and are required to run this application
如果我对 JDK10 使用相同的方法(还有 compiler.source/target=10
和注释掉 org.openjfx
deps)那么上面的工作正常。
是否有任何其他方法(在 JDK11+ 上)构建然后能够从目标目录中 运行 应用程序而无需使用 javafx-maven-plugin
?
在@Slaw 的帮助下,我找到了 运行 javafx 应用程序(在没有 javafx-maven-plugin
的情况下创建)的方法:
cd target
java -p lib/ --add-modules javafx.controls -jar javafx-jdk14-example-1.0.0.jar
我需要明确添加 javafx.controls
模块。
底线:
javafx-maven-plugin
不需要构建和发布 javafx 应用程序。