繁体   English   中英

错误:自动模块不能与 jlink 一起使用:-带有 JavaFX 的 Maven

[英]Error: automatic module cannot be used with jlink: - Maven with JavaFX

我通过 Maven 存储库选择了 Apache Commons IO、JSerialComm 和 Ini4J 库。

但是,当我尝试通过mvn javafx:jlink创建图像时,出现以下错误:

[INFO] --- javafx-maven-plugin:0.0.2:jlink (default-cli) @ JUSBPlotter ---
[WARNING] Required filename-based automodules detected. Please don't publish this project to a public artifact repository!
Error: automatic module cannot be used with jlink: ini4j from file:///root/.m2/repository/org/ini4j/ini4j/0.5.4/ini4j-0.5.4.jar
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:447)

我似乎与此有关:

Error: automatic module cannot be used with jlink:

我的模块文件如下所示:

module org.openjfx.JUSBPlotter {
    requires javafx.controls;
    requires javafx.fxml;
    requires com.fazecast.jSerialComm;
    requires ini4j;
    requires org.apache.commons.io;

    opens org.openjfx.JUSBPlotter to javafx.fxml;
    exports org.openjfx.JUSBPlotter;
}

我的 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>org.openjfx</groupId>
    <artifactId>JUSBPlotter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.fazecast</groupId>
            <artifactId>jSerialComm</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.ini4j</groupId>
            <artifactId>ini4j</artifactId>
            <version>0.5.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.2</version>
                <configuration>
                    <stripDebug>true</stripDebug>
                    <compress>2</compress>
                    <noHeaderFiles>true</noHeaderFiles>
                    <noManPages>true</noManPages>
                    <launcher>JUSBPlotter</launcher>
                    <jlinkImageName>JUSBPlotter</jlinkImageName>
                    <jlinkZipName>JUSBPlotterZip</jlinkZipName>
                    <mainClass>org.openjfx.JUSBPlotter.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

那么 Apache Commons IO、JSerialComm 和 Ini4J 对 Maven 和 Jlink 来说是否过时了?

我应该如何解决这个问题? 我正在使用带有 OpenJDK 11 的 Eclipse IDE。

jlink要求所有依赖项都是模块化的。 生成后,它会生成一个包含所需模块的自定义 JRE 映像。 ini4j似乎是非模块化的。 对于非模块化依赖项,您可以在获得自定义 JRE 后使用旧的 Classpath 方法,该 JRE 已在没有非模块化依赖项的情况下生成。

简而言之,运行 jlink 排除非模块而不是将非模块的 jar 文件添加到生成的 JRE 映像中。 modules 方法和 Classpath 方法可以这样组合。

稍微摆弄一下 maven 插件应该会自动执行此操作。

ini4j的示例

  • 为方便起见定义一些属性。

pom.xml

<properties>
    <jlink-image-name>JUSBPlotter</jlink-image-name>
    <ini4j-jar-name>ini4j.jar</ini4j-jar-name>
</properties>
  1. module-info.java中删除或禁用ini4j
module org.openjfx.JUSBPlotter {
    requires javafx.controls;
    requires javafx.fxml;
    requires com.fazecast.jSerialComm;
    //requires ini4j;
    requires org.apache.commons.io;

    opens org.openjfx.JUSBPlotter to javafx.fxml;
    exports org.openjfx.JUSBPlotter;
}
  1. 配置maven-dependency-plugin ,将ini4j的jar文件复制到ini4jlib/文件夹下。
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <artifactItems>
            <!-- Copy ini4j jar into the jlink image -->
            <artifactItem>
              <groupId>org.ini4j</groupId>
              <artifactId>ini4j</artifactId>
              <version>0.5.4</version>
              <type>jar</type>
              <destFileName>${ini4j-jar-name}</destFileName>
            </artifactItem>
          </artifactItems>
          <!-- Set output directory to lib folder in jlink image -->
          <outputDirectory>${project.build.directory}/${jlink-image-name}/lib</outputDirectory>
          <overWriteReleases>true</overWriteReleases>
          <overWriteSnapshots>true</overWriteSnapshots>
        </configuration>
</plugin>
  1. javafx-maven-plugin中配置jlink启动器选项,以便将非模块化ini4j的 jar 文件添加到 Classpath。
<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.8</version>
    <configuration>
        <stripDebug>true</stripDebug>
        <compress>2</compress>
        <noHeaderFiles>true</noHeaderFiles>
        <noManPages>true</noManPages>
        <launcher>JUSBPlotter</launcher>
        <jlinkImageName>JUSBPlotter</jlinkImageName>
        <mainClass>org.openjfx.JUSBPlotter.Main</mainClass>
        <!-- ini4j jar file will be copied to the {image-folder}/lib/ folder. The launcher script should have this option to add it to the classpath -->
        <options>-cp ../lib/${init4j-jar-name}</options>
    </configuration>
</plugin>

跑:

  • mvn clean javafx:jlink
  • mvn package
  • cd target/JUSBPlotter/bin
  • ./JUSBPlotter

maven-dependeny-plugin将在您运行mvn package时复制 jar 文件。 但是 jlink 图像必须已经生成。 所以先运行mvn javafx:jlink 然后运行mvn package

参考这里看看我是如何在我的项目中申请sqlite-jdbc的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM