gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:28:47 25 4
gpt4 key购买 nike

我创建了一个 JavaFX 应用程序。它在我的 Intellij IDE 中完美运行。现在我想分发应用程序 - 即我想获得一个用户可以下载的安装程序,然后它会为他们安装应用程序。
我发现了一篇关于此的非常有趣的文章 here .这篇博客文章基本上描述了我想要实现的目标。但是有两个区别:

  • 我正在使用 Maven 而不是 Gradle
  • 我有使用自动模块的依赖项,例如 iText7 和 apache.commons.lang3

  • 自动模块的使用使事情变得非常复杂。有一个名为 ModiTect ( here ) 的 GitHub 项目已被编写来解决这些问题。我没有使用 ModiTect 的经验,甚至我的 Maven 知识也几乎不存在(意思是:我真的不知道我在 pom.xml 中在做什么)。
    我正在寻找的是关于如何将 ModiTect(以及必要时 jpackage)集成到我的 pom.xml 以便为我的 JavaFX 应用程序获取安装程序的解释(逐步)数据库,虽然这应该不是问题)。
    有人可以提供此解释或向我推荐教程吗?
    我在这个问题的末尾提供了一个 MWE。 MWE 是一个 TestApp。要说明问题,请运行应用程序并按“打印 PDF”按钮。在资源中创建 pdf --> pdf
    MWE 将在执行 javafx:ru​​n 时编译并运行
    执行javafx:jlink时会出现automodules使用相关的错误
    我不知道如何解决这个问题。 ModiTect 似乎是一个很有前途的插件。另一种可能的方法可以在 this 中找到GitHub 存储库。但正如我之前所说:我的 Maven 知识不足以真正掌握这里发生的事情。任何帮助对我来说意义重大!
    MWE:
    Project Structure
    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>
    模块信息.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;
    }

    }
    }
    测试应用程序.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 参数)
    也许这有助于让你前进。

    关于javafx - 分发使用自动模块的 JavaFX 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67035353/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com