使用 JavaFX maven 插件,您可以执行两个目标:run
and jlink
。前者只会使用所需的参数运行项目(--module-path
, --add-modules
),这样你就可以在命令行上运行:
mvn clean javafx:run
当然,这不是为了分发。
javafx:jlink
但是,如果您的项目是模块化的(即您有一个module-info.java
文件),你可以设置你的插件,如:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<mainClass>hellofx/org.openjfx.App</mainClass>
<launcher>app</launcher>
<jlinkImageName>appDir</jlinkImageName>
<jlinkZipName>appZip</jlinkZipName>
</configuration>
</plugin>
and run:
mvn clean javafx:jlink
它将使用您可以分发的项目生成自定义运行时映像,并且您可以添加启动器甚至压缩它。提取后,您只需要运行它:
target/appdir/app
查看插件选项here.
阴影插件
您还可以使用maven-shade-plugin
.
正如所解释的here你将需要一个不扩展自的主类Application
:
启动器.java
package org.openjfx;
public class Launcher {
public static void main(String[] args) {
App.main(args);
}
}
现在你可以将阴影插件添加到你的 pom 中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjfx.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Run mvn clean package
,它将生成您的 fat jar,您可以将其分发并运行为:
java -jar target/hellofx-1.0-SNAPSHOT.jar
跨平台
请注意,在这两种情况下(jlink 或shade 插件),您将拥有一个jar,您只能将其分发到与您的平台相同的平台上运行。
但是,如果您还包含其他平台的依赖项,则可以使其成为多平台:
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>12.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12.0.1</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12.0.1</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>12.0.1</version>
<classifier>mac</classifier>
</dependency>