多读书多实践,勤思考善领悟

用于运行JavaFX 11+应用程序的Maven插件

本文于1657天之前发表,文中内容可能已经过时。

安装

该插件可通过Maven Central获得。

如果要构建和安装最新快照,可以克隆项目,设置JDK 11并运行

1
mvn install

用法

创建一个新的Maven项目,使用现有的项目,如HelloFX,或使用原型

该项目可以是模块化的或非模块化的。

像往常一样添加JavaFX依赖项:

1
2
3
4
5
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.2</version>
</dependency>

添加插件:

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>hellofx/org.openjfx.App</mainClass>
</configuration>
</plugin>

编译项目(可选):

1
mvn javafx:compile

或者,maven-compiler-plugin可以使用:

1
mvn compile

请注意,包含此插件可以方便在IDE中进行更好的项目集成。

要运行项目:

1
mvn javafx:run

对于模块化项目,要创建和运行自定义映像:

1
2
3
mvn javafx:jlink

target/image/bin/java -m hellofx/org.openjfx.App

javafx:编译选项

编译时javafx:compile,可以设置Java编译器的源级别,目标级别和/或发行级别。默认值为11。

此配置将这些级别更改为12,例如:

1
2
3
4
5
6
7
8
9
10
11
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<source>12</source>
<target>12</target>
<release>12</release>
<mainClass>org.openjfx.hellofx/org.openjfx.App</mainClass>
</configuration>
</plugin>

如果需要,可以设置编译器参数。例如:

1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<compilerArgs>
<arg>--add-exports</arg>
<arg>javafx.graphics/com.sun.glass.ui=org.openjfx.hellofx</arg>
</compilerArgs>
<mainClass>org.openjfx.hellofx/org.openjfx.App</mainClass>
</configuration>
</plugin>

javafx:运行选项

该插件默认包括:--module-path--add-modules-classpath选项。

(可选)可以使用以下命令修改配置:

  • mainClass:主类,完全限定名称,带或不带模块名称
  • workingDirectory:当前的工作目录
  • skip:跳过执行。值:false(默认值),true
  • outputFile 用于重定向过程输出的文件
  • options:传递给可执行文件的VM选项列表。
  • commandlineArgs:由执行程序的空格分隔的参数
  • includePathExceptionsInClasspath:解析模块路径时,将此值设置为true将包括在类路径中生成路径异常的依赖项。默认情况下,该值为false,并且不包括这些依赖项。

例如,以下配置添加了一些VM选项和命令行参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>org.openjfx.hellofx/org.openjfx.App</mainClass>
<options>
<option>--add-opens</option>
<option>java.base/java.lang=org.openjfx.hellofx</option>
</options>
<commandlineArgs>-Xmx1024m</commandlineArgs>
</configuration>
</plugin>

注意

可以使用本地SDK而不是Maven Central。这对于尝试测试OpenJFX的本地构建的开发人员很有帮助。由于未解决传递依赖性,因此需要将所有必需的jar添加为单独的依赖项,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<properties>
<sdk>/path/to/javafx-sdk</sdk>
</properties>

<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx.base</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${sdk}/lib/javafx.base.jar</systemPath>
</dependency>
...
</dependencies>

javafx:jlink选项

jlink可以设置相同的命令行选项:

  • stripDebug:删除调试信息。值:false(默认值)或true

  • compress:正在使用的资源的压缩级别。值:0(默认值),1,2。

  • noHeaderFiles:删除includes生成的运行时映像中的目录。值:false(默认值)或true

  • noManPages:删除man生成的运行时映像中的目录。值:false(默认值)或true

  • bindServices:添加绑定服务的选项。值:false(默认值)或true

  • ignoreSigningInformation:添加忽略签名信息的选项。值:false(默认值)或true

  • jlinkVerbose:添加详细选项。值:false(默认值)或true

  • 1
    launcher

    :添加具有给定名称的启动程序脚本。

    • 如果options已定义,则这些将作为vm选项传递给启动程序脚本。
    • 如果commandLineArgs已定义,则这些将作为命令行参数传递给启动程序脚本。
  • jlinkImageName:具有生成的运行时映像的文件夹的名称

  • jlinkZipName:设置后,创建生成的运行时映像的zip

  • jlinkExecutablejlink可执行文件。它可以是完整路径或可执行文件的名称(如果它在PATH中)。

  • jmodsPath:使用本地JavaFX SDK时,设置本地JavaFX jmods的路径

例如,使用以下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<stripDebug>true</stripDebug>
<compress>2</compress>
<noHeaderFiles>true</noHeaderFiles>
<noManPages>true</noManPages>
<launcher>hellofx</launcher>
<jlinkImageName>hello</jlinkImageName>
<jlinkZipName>hellozip</jlinkZipName>
<mainClass>hellofx/org.openjfx.MainApp</mainClass>
</configuration>
</plugin>

可以创建自定义图像并将其运行为:

1
2
3
mvn clean javafx:jlink

target/hello/bin/hellofx