javafx-maven-plugin 应该能够做你想做的事情。但是,到目前为止还没有这样做,所以我刚刚提交了这两个问题:Options for javafx:run are incompatible with javafx:jlink 和 Missing link vm options parameter。
虽然问题得到解决并发布了新版本,但有一个简单(但手动)的修复方法:
编译时间
在修改 javafx-maven-plugin 之前,您需要允许您的 IDE 使用私有包。您无法从模块信息中执行此操作,但您可以使用 compilerArgs 从maven-compiler-plugin 轻松执行此操作:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>--add-exports</arg>
<arg>javafx.graphics/com.sun.glass.ui=com.andrei</arg>
</compilerArgs>
</configuration>
</plugin>
现在,您可以在您的代码中使用该私有包,而 IntelliJ 不会抱怨。
从 Maven 窗口 Lifecycle -> clean 和 Lifecycle -> compile 运行后,编辑器中允许执行以下操作:
@Override
public void start(Stage stage) throws Exception {
...
stage.setScene(scene);
stage.show();
com.sun.glass.ui.Window.getWindows().forEach(System.out::println);
}
运行时
但是,如果你这样做mvn clean compile javafx:run,上面的代码就会失败:
原因:java.lang.IllegalAccessError:com.andrei.Main 类(com.andrei 模块中)无法访问 com.sun.glass.ui.Window 类(javafx.graphics 模块中),因为 javafx.graphics 模块可以访问不将 com.sun.glass.ui 导出到模块 com.andrei。
正如插件readme 中所述,您可以添加将传递给java 工具的VM 选项:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<options>
<option>--add-opens</option>
<option>javafx.graphics/com.sun.glass.ui=com.andrei</option>
</options>
...
</configuration>
</plugin>
现在您可以运行:mvn clean compile javafx:run,这将起作用,您将获得当前阶段的打印信息。
运行时映像
最后,如果你运行:mvn clean compile javafx:jlink,这将失败,因为<options>中的内容不被jlink(第一个问题提交)识别,所以你必须把它注释掉:
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<!--<options>-->
<!--<option>--add-opens</option>-->
<!--<option>javafx.graphics/com.sun.glass.ui=com.andrei</option>-->
<!--</options>-->
<launcher>launcher</launcher>
<mainClass>com.andrei/com.andrei.Main</mainClass>
...
</configuration>
</plugin>
现在mvn clean compile javafx:jlink 可以工作了,但是运行时你会得到和上面一样的错误,因为私有包没有被导出。
但是,您可以在target/image/bin/launcher 下编辑启动器文件:
#!/bin/sh
JLINK_VM_OPTIONS=
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m com.andrei/com.andrei.Main $@
如您所见,有一个空的 JLINK_VM_OPTIONS 变量可以用您的 vm 选项填充。
在解决第二个问题之前,只需修改该行:
#!/bin/sh
JLINK_VM_OPTIONS="--add-opens javafx.graphics/com.sun.glass.ui=com.andrei"
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m fx/org.openjfx.MainApp $@
保存并运行:target/image/bin/launcher,它将起作用。