分发使用自动模块的 JavaFX 应用程序

Distributing JavaFX Applications that use automodules

我已经创建了一个 JavaFX 应用程序。它 运行 在我的 Intellij IDE 中非常完美。现在我想分发应用程序 - 即我想获得一个用户可以下载的安装程序,然后它会为他们安装应用程序。

我发现了一篇关于此的非常有趣的文章 here。这篇博客文章基本上描述了我想要实现的目标。但是有两个区别:

  1. 我正在使用 Maven 而不是 Gradle

  2. 我有使用自动模块的依赖项,例如 iText7 和 apache.commons.lang3

自动模块的使用使事情变得非常复杂。有一个名为 ModiTect (here) 的 GitHub 项目旨在解决这些问题。我没有使用 ModiTect 的经验,甚至我的 Maven 知识也几乎不存在(意思是:我真的不知道我在 pom.xml 中做什么)。

我正在寻找的是关于如何将 ModiTect(以及必要时的 jpackage)集成到我的 pom.xml 中以获得我的 JavaFX 应用程序的安装程序的解释(逐步)使用自动模块(以及 sqlite 数据库,不过这应该不是问题)。

有人可以提供此解释或向我推荐教程吗?

我在这个问题的最后提供了一个MWE。 MWE 是一个 TestApp。为了说明问题,运行 应用程序并按“打印 PDF”按钮。在资源中创建了 pdf --> pdf

MWE会在执行javafx:run时编译和运行 执行javafx:jlink

时会出现automodules使用相关的错误

我不知道如何解决这个问题。 ModiTect 似乎是一个很有前途的插件。另一种可能的方法可以在 this GitHub 仓库中找到。但正如我之前所说:我的 Maven 知识不足以真正掌握这里发生的事情。任何帮助对我来说意义重大!

MWE:

pom.xml:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>TestApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>15.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>15.0.1</version>
        </dependency>

        <dependency>
            <groupId>de.jensd</groupId>
            <artifactId>fontawesomefx-fontawesome</artifactId>
            <version>4.7.0-9.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.34.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.1.14</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.1.14</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>io</artifactId>
            <version>7.1.14</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.30</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>15</release>
                    <source>15</source>
                    <target>15</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.5</version>
                <configuration>
                    <mainClass>com.company.TestApp</mainClass>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

模块-info.java:

module com.company {
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires org.apache.commons.lang3;
    requires kernel;
    requires layout;
    requires io;
    requires sqlite.jdbc;
    requires javafx.graphics;

    opens com.company to javafx.fxml;
    opens com.company.controllers to javafx.fxml;

    exports com.company;
    exports com.company.controllers;
}

TestAppController.java:

package com.company.controllers;


import javafx.event.ActionEvent;

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

import org.apache.commons.lang3.StringUtils;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.Document;

import java.io.FileNotFoundException;


public class TestAppController {

    @FXML
    private TextArea taText;

    @FXML
    private Button btnPrint;


    public void handleButtonAction(ActionEvent event) {
        if (event.getSource() == btnPrint) {
            setTaText();
            printPdf();
        }
    }

    public void setTaText() {
        taText.setText(StringUtils.leftPad("Random Text left padded by 50", 50));
    }

    public void printPdf() {
        String directoryString = "src/main/resources/com/company/pdf";

        try {
            String filepath = directoryString + "/" + "pdf_1" + ".pdf";
            PdfWriter writer = new PdfWriter(filepath);
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf);
            document.add(new Paragraph(taText.getText()));
            document.close();
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            return;
        }

    }
}

TestApp.java:

package com.company;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class TestApp extends Application {

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


    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("testApp.fxml"));
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Test");

        primaryStage.show();
    }
}

testApp.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.company.controllers.TestAppController">
   <top>
      <AnchorPane prefHeight="60.0" prefWidth="600.0" style="-fx-background-color: #337DFF;" BorderPane.alignment="CENTER" />
   </top>
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="btnPrint" layoutX="240.0" layoutY="155.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="25.0" prefWidth="120.0" style="-fx-background-color: #337DFF;" text="Print PDF" textFill="WHITE">
               <font>
                  <Font name="System Bold" size="15.0" />
               </font>
            </Button>
            <TextArea fx:id="taText" layoutX="125.0" layoutY="44.0" prefHeight="82.0" prefWidth="350.0" />
         </children>
      </AnchorPane>
   </center>
</BorderPane>

除了 javafx maven 插件之外,您还可以使用 moditect 插件来创建缺少的模块信息以自动依赖模块,然后使用 moditect 构建映像。

这样的 pom 可能是这样的:

<?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>com.company</groupId>
<artifactId>TestApp</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <appName>TestApp</appName>
    <launcherName>testapp</launcherName>
    <moduleName>com.company</moduleName>
    <mainClass>com.company.LoginApp</mainClass>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
    <version.java>15</version.java>
</properties>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>15.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>15.0.1</version>
    </dependency>

    <dependency>
        <groupId>de.jensd</groupId>
        <artifactId>fontawesomefx-fontawesome</artifactId>
        <version>4.7.0-9.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.34.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.1.14</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.1.14</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.1.14</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>${version.java}</release>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/modules</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.moditect</groupId>
            <artifactId>moditect-maven-plugin</artifactId>
            <version>1.0.0.RC1</version>
            <executions>
                <execution>
                    <id>add-module-info-to-dependencies</id>
                    <phase>package</phase>
                    <configuration>
                        <outputDirectory>${project.build.directory}/modules</outputDirectory>
                        <overwriteExistingFiles>true</overwriteExistingFiles>
                        <modules>
                            <module>
                                <artifact>
                                    <groupId>com.itextpdf</groupId>
                                    <artifactId>kernel</artifactId>
                                </artifact>
                                <moduleInfo>
                                    <name>kernel</name>
                                </moduleInfo>
                            </module>
                            <module>
                                <artifact>
                                    <groupId>com.itextpdf</groupId>
                                    <artifactId>layout</artifactId>
                                </artifact>
                                <moduleInfo>
                                    <name>layout</name>
                                </moduleInfo>
                            </module>
                            <module>
                                <artifact>
                                    <groupId>com.itextpdf</groupId>
                                    <artifactId>io</artifactId>
                                </artifact>
                                <moduleInfo>
                                    <name>io</name>
                                </moduleInfo>
                            </module>
                            <module>
                                <artifact>
                                    <groupId>org.xerial</groupId>
                                    <artifactId>sqlite-jdbc</artifactId>
                                </artifact>
                                <moduleInfo>
                                    <name>sqlite.jdbc</name>
                                </moduleInfo>
                            </module>
                            <module>
                                <artifact>
                                    <groupId>org.apache.commons</groupId>
                                    <artifactId>commons-lang3</artifactId>
                                </artifact>
                                <moduleInfo>
                                    <name>org.apache.commons.lang3</name>
                                </moduleInfo>
                            </module>
                            <module>
                                <artifact>
                                    <groupId>org.slf4j</groupId>
                                    <artifactId>slf4j-api</artifactId>
                                </artifact>
                                <moduleInfo>
                                    <name>org.slf4j</name>
                                </moduleInfo>
                            </module>
                        </modules>
                        <module>
                            <mainClass>${mainClass}</mainClass>
                            <moduleInfoFile>${project.build.sourceDirectory}/module-info.java</moduleInfoFile>
                        </module>
                        <jdepsExtraArgs>
                            <args>--multi-release</args> <args>15</args>
                            <args>--ignore-missing-deps</args>
                        </jdepsExtraArgs>
                    </configuration>
                    <goals>
                        <goal>add-module-info</goal>
                    </goals>
                </execution>
                <execution>
                    <id>create-runtime-image</id>
                    <phase>package</phase>
                    <goals>
                        <goal>create-runtime-image</goal>
                    </goals>
                    <configuration>
                        <modulePath>
                            <path>${project.build.directory}/modules</path>
                        </modulePath>
                        <modules>
                            <module>${moduleName}</module>
                        </modules>
                        <launcher>
                            <name>${launcherName}</name>
                            <module>${moduleName}</module>
                        </launcher>
                        <compression>2</compression>
                        <stripDebug>true</stripDebug>
                        <outputDirectory>${project.build.directory}/jlink-image</outputDirectory>
                        <ignoreSigningInformation>true</ignoreSigningInformation>
                    </configuration>
                </execution>

            </executions>
        </plugin>
        <plugin>
            <groupId>com.github.akman</groupId>
            <artifactId>jpackage-maven-plugin</artifactId>
            <version>0.1.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jpackage</goal>
                    </goals>
                    <configuration>
                        <name>${appName}</name>
                        <type>IMAGE</type>
                        <runtimeimage>${project.build.directory}/jlink-image</runtimeimage>
                        <module>${moduleName}/${mainClass}</module>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

</project>

我的第一个测试似乎是成功的,但一些项目可能需要更多的工作(例如,我不喜欢使用 --ignore-missing-deps 参数)

也许这对你前进有一点帮助。