javafx的maven项目打jar包配置

1.项目结构

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   ├── com
    │   │   │   └── bingbaihanji
    │   │   │       └── Main.java
    │   │   └── module-info.java
    │   └── resources
    └── test
        └── java

9 directories, 3 files

2.使用springboot的maven打包插件[简单快捷]来打成一个可执行的jar包

maven的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>

    <!--1.引入springboot的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.bingbaihanji</groupId>
    <artifactId>javaFxDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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</artifactId>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.2</version>
        </dependency>
    </dependencies>

    <build>
        <!--修改编译出来的jar包名,仅为{artifactId}.jar-->
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!--springboot的打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动类

加入 @SpringBootApplication 注解和启动函数

@SpringBootApplication
public class Main extends Application {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        ······
    }
}

然后在当前项目下运行 mvn package 即可在当前目录下生成的target目录中找到可以执行的jar包

jar包大致结构如下

.
├── BOOT-INF
│   ├── classes
│   │   └── com
│   │       └── bingbaihanji
│   │           └── Main.class   // 项目启动类
│   ├── classpath.idx
│   ├── layers.idx
│   └── lib  // 项目运行所依赖的jar包
├── META-INF
│   ├── MANIFEST.MF // 项目清单文件
│   └── maven
│       └── com.bingbaihanji
│           └── javaFxDemo
│               ├── pom.properties
│               └── pom.xml
├── module-info.class
└── org // springboot框架提供的用来启动jar相关的类
    └── springframework
        └── boot
            └── loader

19 directories, 97 files

MANIFEST.MF 文件结构

Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.3.0
Build-Jdk-Spec: 17
Implementation-Title: javaFxDemo
Implementation-Version: 1.0-SNAPSHOT
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.bingbaihanji.Main
Spring-Boot-Version: 3.1.2
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx

3.使用maven打包插将项目打包成分散的jar包 (项目启动包和依赖库)

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>libs/</classpathPrefix>
                        <mainClass>com.bingbaihanji.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/libs
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后在当前项目下运行 mvn package即可打包完成 在 target 目录下生成 libs 和 项目的jar包,

其中libs是项目运行时所依赖的库,其中项目jar包的清单文件内容为

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.5
Built-By: 冰白寒祭
Build-Jdk: 17.0.2
Class-Path: libs/javafx-controls-17.0.2.jar libs/javafx-controls-17.0.2-
 win.jar libs/javafx-graphics-17.0.2.jar libs/javafx-graphics-17.0.2-win
 .jar libs/javafx-base-17.0.2.jar libs/javafx-base-17.0.2-win.jar libs/j
 avafx-fxml-17.0.2.jar libs/javafx-fxml-17.0.2-win.jar
Main-Class: com.bingbaihanji.Main

如果此时直接用 java -jar 命令来运行项目jar包会提示 错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序

是由于从 jdk9 开始 java开始添加了以模块化来构建项目的规范;

解决办法有

1.可以添加jvm参数

可以在当前路径下执行

java -jar --module-path ./libs --add-modules javafx.controls,javafx.fxml ./javaFxDemo.jar

来启动项目。

2.在项目中添加一个启动类

maven打包的时候主类指定为启动类

修改maven插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
                <mainClass>com.bingbaihanji.Start</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

新建一个启动类

package com.bingbaihanji;
import com.bingbaihanji.Main;
/**
 * @author 冰白寒祭
 * @date 2023-12-03 00:09:51
 * @description //TODO
 */
public class Start {
    public static void main(String[] args) {
        Main.main(args);
    }
}

重新打包后即可直接用 java -jar 的方式来启动项目

人生不作安期生,醉入东海骑长鲸