使用ModiTect生成模块信息并创建运行时镜像

huangapple go评论81阅读模式
英文:

Using ModiTect to generate module info and create runtime image

问题

尝试使用 JLink 创建运行时镜像时出现了以下错误:

错误:无法使用 JLink 创建自动模块:graphviz.java

我正在使用以下 ,该库仅包含自动模块。

经过一些搜索,似乎我可以使用 ModiTect 为该库生成 module-info,并创建运行时镜像。

然而,由于我对模块的了解有限,我无法弄清楚。
我想我可能需要一个比我在 ModiTect 的 GitHub 页面上找到的更详细的解释。

我的 modules-info.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>
            <!-- 其他插件略 -->
        </plugins>
    </build>
</project>

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

英文:

Tried to use JLink to create a runtime image, however:

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

I'm using the following library which has only automatic modules.

Did some searches, and it seems that I can use ModiTect to generate module-info for the library and create a runtime image.

However, as I am new to modules I couldn't figure it out.
I guess I would need a more in-depth explanation then what I could find on ModiTect's GitHub page.

my modules-info.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;
}

My pom file:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;elitonlais&lt;/groupId&gt;
    &lt;artifactId&gt;minafd&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
            &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
            &lt;version&gt;14&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
            &lt;artifactId&gt;javafx-fxml&lt;/artifactId&gt;
            &lt;version&gt;14&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
            &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
            &lt;version&gt;1.2.3&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;guru.nidi&lt;/groupId&gt;
            &lt;artifactId&gt;graphviz-java&lt;/artifactId&gt;
            &lt;version&gt;0.17.0&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.0&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;release&gt;11&lt;/release&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
                &lt;artifactId&gt;javafx-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;0.0.3&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;mainClass&gt;elitonlais.App&lt;/mainClass&gt;
                    &lt;launcher&gt;launch.sh&lt;/launcher&gt;
                    &lt;compress&gt;2&lt;/compress&gt;
                    &lt;jlinkZipName&gt;MinAFD&lt;/jlinkZipName&gt;
                    &lt;jlinkImageName&gt;MinAFD&lt;/jlinkImageName&gt;
                    &lt;includePathExceptionsInClasspath&gt;true&lt;/includePathExceptionsInClasspath&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.moditect&lt;/groupId&gt;
                &lt;artifactId&gt;moditect-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;1.0.0.Beta2&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;create-runtime-image&lt;/id&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;create-runtime-image&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;modulePath&gt;
                                &lt;path&gt;${project.build.directory}/modules&lt;/path&gt;
                            &lt;/modulePath&gt;
                            &lt;modules&gt;
                                &lt;module&gt;module-info.java&lt;/module&gt;
                                &lt;!--   &lt;module&gt;javafx.controls&lt;/module&gt;
                                &lt;module&gt;javafx.graphics&lt;/module&gt; --&gt;
                            &lt;/modules&gt;
                            &lt;launcher&gt;
                                &lt;name&gt;test33&lt;/name&gt;
                                &lt;module&gt;module-info.java/elitonlais.app&lt;/module&gt;
                            &lt;/launcher&gt;
                            &lt;compression&gt;2&lt;/compression&gt;
                            &lt;stripDebug&gt;true&lt;/stripDebug&gt;
                            &lt;outputDirectory&gt;${project.build.directory}/jlink-image&lt;/outputDirectory&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

The entire project structure:
https://github.com/EMachad0/MinAFD

答案1

得分: 1

你应该能够按照链接中的文档 将模块描述符添加到现有的 JAR 文件中 进行添加。

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

&lt;executions&gt;
    &lt;execution&gt;
        &lt;!-- 这里放置你现有的代码 --&gt;
    &lt;/execution&gt;
    &lt;execution&gt;
        &lt;id&gt;add-module-infos&lt;/id&gt;
        &lt;phase&gt;generate-resources&lt;/phase&gt;
        &lt;goals&gt;
            &lt;goal&gt;add-module-info&lt;/goal&gt;
        &lt;/goals&gt;
        &lt;configuration&gt;
            &lt;outputDirectory&gt;${project.build.directory}/modules&lt;/outputDirectory&gt;
            &lt;modules&gt;
                &lt;module&gt;
                    &lt;artifact&gt;
                        &lt;groupId&gt;guru.nidi&lt;/groupId&gt;
                        &lt;artifactId&gt;graphviz-java&lt;/artifactId&gt;
                        &lt;version&gt;0.17.0&lt;/version&gt;
                    &lt;/artifact&gt;
                    &lt;moduleInfo&gt;
                        &lt;name&gt;guru.nidi.graphviz&lt;/name&gt;
                        &lt;exports&gt;
                            *;
                        &lt;/exports&gt;
                        &lt;!-- 可能还有其他微调 --&gt;
                    &lt;/moduleInfo&gt;
                &lt;/module&gt;
                &lt;module&gt;
                    ...
                &lt;/module&gt;
            &lt;/modules&gt;
        &lt;/configuration&gt;
    &lt;/execution&gt;
&lt;/executions&gt;

还要注意,该项目已经指定了一个自动模块名为 guru.nidi.graphviz,因此您需要将自己的 module-info 更改为需要该模块,而不是自动生成的 graphviz.java

英文:

You should be able to add it following the documentation at the link Adding module descriptors to existing JAR files.

Adding the following to your existing pom.xml as an additional &lt;execution&gt; step for ModiTect should work:

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

Note also that the project has specified an automatic module name of guru.nidi.graphviz so you'll need to change your own module-info to require that, rather than the auto-derived graphviz.java.

huangapple
  • 本文由 发表于 2020年8月24日 20:53:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63561465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定