Use Maven to quickly integrate and build and develop JavaFX for desktop application development

Purpose: Don’t step on the pit you have stepped on once

Nanny-level tutorials, package education package meeting

With this method, you don’t need to worry about manually configuring the jar package every time, and you don’t need to worry about the VM parameter –module-path of the startup class.

With this method, you don’t need to worry about javafx-maven-archetypes anymore (now the official maven warehouse seems to have removed the prototype of this thing, and you can’t find it in archetype-catalog.xml)

Create a new Maven project directly, don’t worry about anything else

Then follow this project structure to create the following files

Project structure:

Note that there is a big pit here. The directory created under the resources directory is not directly called “org.example”, but the org directory is created first, and then the example directory is created. The directory structure created in this way is different

To be precise:

First right-click resources–New–Directory

Right click on org–New–Directory

The directory structure created in this way is correct, and then the resource files will be placed in the correct position in the compilation directory

Then put application.css and other resource files in it.

(The main purpose here is to ensure the success of the first pass corresponding to the default path under getClass() in the follow-up code, and those who understand can ignore it)

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.example</groupId>
    <artifactId>javafx</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <excludes>
                        <exclude>module-info.java</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <configuration>
                    <mainClass>org.example</mainClass>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <!--Pack the properties and xm files in the src/main/java directory into the program -->
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <!--Pack the properties, xml, and css files in the src/main/resources directory into the program -->
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.fxml</include>
                    <include>**/*.css</include>
                    <include>**/*.setting</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <!--Put the third-party jar package in the lib/ directory into the program, such as the jar in the systemPath directory-->
                <directory>lib/</directory>
                <includes>
                    <include>**/*.jar</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>
    <profiles>
        <profile>
            <id>openjfx</id>
            <activation>
                <jdk>[17,)</jdk>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-controls</artifactId>
                    <version>20.0.1</version>
                </dependency>
                <dependency>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-fxml</artifactId>
                    <version>20.0.1</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

</project>

module-info.java

module org. example {
    requires javafx.controls;
    requires javafx.fxml;

    opens org.example to javafx.fxml;
    exports org.example;
}

application.css (default):

/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */

Main class (test minimal project):

package org.example;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.Objects;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("application.css")).toExternalForm());
            primaryStage. setScene(scene);
            primaryStage. show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

After the files are arranged, right-click pom.xml, find Reload Project according to the figure, and click:

If it is too slow to access the maven warehouse in this step, you can refer to another article of mine to change the domestic source:

IDEA completes Maven source change in two steps to speed up downloading – Stallion_X’s Blog – CSDN Blog

The window is successful:

If you want to use other packages in the JavaFX SDK later, in addition to adding them in pom.xml, you also need to add the package name in module-info.java.

This article refers to these articles and improves on them:

Use Maven to build, develop and package JavaFX projects – Programmer Sought

Maven way to develop JavaFX_weixin_44260298’s blog – CSDN Blog

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 118843 people are studying systematically