我正在尝试使用 Spring Boot 构建 JavaFX 应用程序并使用 jpackage 进行部署。
当使用javafx-maven-plugin
javafx:run
命令,我可以看到项目启动。但在将其构建为*.msi
安装程序,安装并启动*.exe
,什么也没有发生(无论是双击还是命令行运行)。
我该如何解决这个问题,或者至少知道发生了什么?
我怀疑javafx:run
目标是获得手动 jlink 中缺少的一些参数,但是我无法使用javafx:jlink
因为应用程序本身不是一个模块,因为 Spring Boot 还没有模块化。
这是来源;
EDIT:根据@Slaw的建议,我修改了jpackage
命令包含`--win-console。从而成功运行该应用程序。虽然这是一个很好的步骤,但这不是我想要的,我想在没有提示的情况下运行它。
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>org.test</groupId>
<artifactId>mytest</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>17.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.0</version>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>org.test.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Main:
package org.test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Main extends Application {
private ConfigurableApplicationContext springContext;
private Parent rootNode = new AnchorPane();
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(rootNode, 700, 700));
stage.setMinWidth(700);
stage.setMinHeight(700);
stage.show();
}
public void init() throws Exception{
springContext = SpringApplication.run(Main.class);
}
public void stop() throws Exception{
springContext.close();
}
}
我的构建脚本是:
@ECHO OFF
SETLOCAL
call mvn clean install
set manual_modules=,javafx.controls,javafx.graphics,javafx.fxml
for /f %%i in ('jdeps --multi-release 17 --ignore-missing-deps --print-module-deps --class-path "target/lib/*" target/classes/org/test/Main.class') do set detected_modules=%%i
echo jlink
jlink --no-header-files --no-man-pages --compress=2 --strip-debug --module-path "target/lib" --add-modules %detected_modules%%manual_modules% --output target/java-runtime
echo jpackage
jpackage --type msi --dest target/installer --input target --name mytest --main-class org.test.Main --main-jar mytest-1.0.jar --runtime-image target/java-runtime
endlocal
@ECHO ON