【问题标题】:Using ModiTect to generate module info and create runtime image使用 ModiTect 生成模块信息并创建运行时映像
【发布时间】:2020-12-13 02:51:19
【问题描述】:

尝试使用 JLink 创建运行时映像,但是:

Error: automatic module cannot be used with JLink: graphviz.java

我正在使用以下library,它只有自动模块。

做了一些搜索,看来我可以使用ModiTect 为库生成模块信息并创建运行时映像。

但是,由于我对模块不熟悉,所以我无法弄清楚。 我想我需要比在 ModiTect 的 GitHub 页面上找到的更深入的解释。

我的模块信息.java:

module elitonlais {
    requires javafx.controls;
    requires javafx.fxml;
    requires graphviz.java;

    opens elitonlais to javafx.fxml;
    opens elitonlais.controller to javafx.fxml;
    opens elitonlais.model to javafx.fxml;

    exports elitonlais;
    exports elitonlais.controller;
    exports elitonlais.model;
}

我的 pom 文件:

<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>elitonlais</groupId>
    <artifactId>minafd</artifactId>
    <version>1.0-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>14</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>guru.nidi</groupId>
            <artifactId>graphviz-java</artifactId>
            <version>0.17.0</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.3</version>
                <configuration>
                    <mainClass>elitonlais.App</mainClass>
                    <launcher>launch.sh</launcher>
                    <compress>2</compress>
                    <jlinkZipName>MinAFD</jlinkZipName>
                    <jlinkImageName>MinAFD</jlinkImageName>
                    <includePathExceptionsInClasspath>true</includePathExceptionsInClasspath>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.moditect</groupId>
                <artifactId>moditect-maven-plugin</artifactId>
                <version>1.0.0.Beta2</version>
                <executions>
                    <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>module-info.java</module>
                                <!--   <module>javafx.controls</module>
                                <module>javafx.graphics</module> -->
                            </modules>
                            <launcher>
                                <name>test33</name>
                                <module>module-info.java/elitonlais.app</module>
                            </launcher>
                            <compression>2</compression>
                            <stripDebug>true</stripDebug>
                            <outputDirectory>${project.build.directory}/jlink-image</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

整个项目结构: https://github.com/EMachad0/MinAFD

【问题讨论】:

    标签: java maven graphviz jlink moditect


    【解决方案1】:

    您应该可以按照链接 Adding module descriptors to existing JAR files 上的文档添加它。

    将以下内容添加到现有的pom.xml 作为ModiTect 的附加&lt;execution&gt; 步骤应该可以工作:

    <executions>
        <execution>
            <!-- your existing code here -->
        </execution>
        <execution>
            <id>add-module-infos</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>add-module-info</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/modules</outputDirectory>
                <modules>
                    <module>
                        <artifact>
                            <groupId>guru.nidi</groupId>
                            <artifactId>graphviz-java</artifactId>
                            <version>0.17.0</version>
                        </artifact>
                        <!-- only use one of either moduleInfo or moduleInfoSource -->
                        <moduleInfo>
                            <name>guru.nidi.graphviz</name>
                            <exports>
                                *;
                            </exports>
                            <!-- possibly other fine-tuning -->
                        </moduleInfo>
                        <!-- only use one of either moduleInfo or moduleInfoSource -->
                        <moduleInfoSource>
                            module guru.nidi.graphviz {
                                requires ...;
                                exports ...;
                                provides ...
                                    with ...;
                            }
                        </moduleInfoSource>
                        <!-- or use moduleInfoFile instead of the above -->
                    </module>
                    <module>
                        ...
                    </module>
                </modules>
            </configuration>
        </execution>
    </executions>
    

    另请注意,该项目已指定自动模块名称 guru.nidi.graphviz,因此您需要更改自己的 module-info 以要求它,而不是自动派生的 graphviz.java

    【讨论】:

    • 我应该如何执行目标?运行 moditect:create-runtime-info 给我这个错误:``` Failed to execute goal org.moditect:moditect-maven-plugin:1.0.0.Beta2:create-runtime-image (default-cli) on project MinAFD:目标 org.moditect:moditect-maven-plugin:1.0.0.Beta2:create-runtime-image 的参数“模块”丢失或无效```
    • 运行create-runtime-info 目标需要单独的配置。但另一个目标应该在构建的generate-resources 阶段运行。
    • 如果您在构建过程中不想要它,目标moditect:add-module-info 不会将该类添加到现有JAR 中吗?
    猜你喜欢
    • 2013-11-30
    • 2020-05-10
    • 2020-12-08
    • 2011-06-09
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 2020-12-14
    相关资源
    最近更新 更多