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

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

Using ModiTect to generate module info and create runtime image

问题

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

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

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

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

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

我的 modules-info.java 文件:

  1. module elitonlais {
  2. requires javafx.controls;
  3. requires javafx.fxml;
  4. requires graphviz.java;
  5. opens elitonlais to javafx.fxml;
  6. opens elitonlais.controller to javafx.fxml;
  7. opens elitonlais.model to javafx.fxml;
  8. exports elitonlais;
  9. exports elitonlais.controller;
  10. exports elitonlais.model;
  11. }

我的 pom 文件:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>elitonlais</groupId>
  5. <artifactId>minafd</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <maven.compiler.source>11</maven.compiler.source>
  10. <maven.compiler.target>11</maven.compiler.target>
  11. </properties>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.openjfx</groupId>
  15. <artifactId>javafx-controls</artifactId>
  16. <version>14</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.openjfx</groupId>
  20. <artifactId>javafx-fxml</artifactId>
  21. <version>14</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>ch.qos.logback</groupId>
  25. <artifactId>logback-classic</artifactId>
  26. <version>1.2.3</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>guru.nidi</groupId>
  30. <artifactId>graphviz-java</artifactId>
  31. <version>0.17.0</version>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.apache.maven.plugins</groupId>
  38. <artifactId>maven-compiler-plugin</artifactId>
  39. <version>3.8.0</version>
  40. <configuration>
  41. <release>11</release>
  42. </configuration>
  43. </plugin>
  44. <!-- 其他插件略 -->
  45. </plugins>
  46. </build>
  47. </project>

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

英文:

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

  1. 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:

  1. module elitonlais {
  2. requires javafx.controls;
  3. requires javafx.fxml;
  4. requires graphviz.java;
  5. opens elitonlais to javafx.fxml;
  6. opens elitonlais.controller to javafx.fxml;
  7. opens elitonlais.model to javafx.fxml;
  8. exports elitonlais;
  9. exports elitonlais.controller;
  10. exports elitonlais.model;
  11. }

My pom file:

  1. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  2. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
  3. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  4. &lt;groupId&gt;elitonlais&lt;/groupId&gt;
  5. &lt;artifactId&gt;minafd&lt;/artifactId&gt;
  6. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  7. &lt;properties&gt;
  8. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  9. &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
  10. &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
  11. &lt;/properties&gt;
  12. &lt;dependencies&gt;
  13. &lt;dependency&gt;
  14. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  15. &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
  16. &lt;version&gt;14&lt;/version&gt;
  17. &lt;/dependency&gt;
  18. &lt;dependency&gt;
  19. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  20. &lt;artifactId&gt;javafx-fxml&lt;/artifactId&gt;
  21. &lt;version&gt;14&lt;/version&gt;
  22. &lt;/dependency&gt;
  23. &lt;dependency&gt;
  24. &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
  25. &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
  26. &lt;version&gt;1.2.3&lt;/version&gt;
  27. &lt;/dependency&gt;
  28. &lt;dependency&gt;
  29. &lt;groupId&gt;guru.nidi&lt;/groupId&gt;
  30. &lt;artifactId&gt;graphviz-java&lt;/artifactId&gt;
  31. &lt;version&gt;0.17.0&lt;/version&gt;
  32. &lt;/dependency&gt;
  33. &lt;/dependencies&gt;
  34. &lt;build&gt;
  35. &lt;plugins&gt;
  36. &lt;plugin&gt;
  37. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  38. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  39. &lt;version&gt;3.8.0&lt;/version&gt;
  40. &lt;configuration&gt;
  41. &lt;release&gt;11&lt;/release&gt;
  42. &lt;/configuration&gt;
  43. &lt;/plugin&gt;
  44. &lt;plugin&gt;
  45. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  46. &lt;artifactId&gt;javafx-maven-plugin&lt;/artifactId&gt;
  47. &lt;version&gt;0.0.3&lt;/version&gt;
  48. &lt;configuration&gt;
  49. &lt;mainClass&gt;elitonlais.App&lt;/mainClass&gt;
  50. &lt;launcher&gt;launch.sh&lt;/launcher&gt;
  51. &lt;compress&gt;2&lt;/compress&gt;
  52. &lt;jlinkZipName&gt;MinAFD&lt;/jlinkZipName&gt;
  53. &lt;jlinkImageName&gt;MinAFD&lt;/jlinkImageName&gt;
  54. &lt;includePathExceptionsInClasspath&gt;true&lt;/includePathExceptionsInClasspath&gt;
  55. &lt;/configuration&gt;
  56. &lt;/plugin&gt;
  57. &lt;plugin&gt;
  58. &lt;groupId&gt;org.moditect&lt;/groupId&gt;
  59. &lt;artifactId&gt;moditect-maven-plugin&lt;/artifactId&gt;
  60. &lt;version&gt;1.0.0.Beta2&lt;/version&gt;
  61. &lt;executions&gt;
  62. &lt;execution&gt;
  63. &lt;id&gt;create-runtime-image&lt;/id&gt;
  64. &lt;phase&gt;package&lt;/phase&gt;
  65. &lt;goals&gt;
  66. &lt;goal&gt;create-runtime-image&lt;/goal&gt;
  67. &lt;/goals&gt;
  68. &lt;configuration&gt;
  69. &lt;modulePath&gt;
  70. &lt;path&gt;${project.build.directory}/modules&lt;/path&gt;
  71. &lt;/modulePath&gt;
  72. &lt;modules&gt;
  73. &lt;module&gt;module-info.java&lt;/module&gt;
  74. &lt;!-- &lt;module&gt;javafx.controls&lt;/module&gt;
  75. &lt;module&gt;javafx.graphics&lt;/module&gt; --&gt;
  76. &lt;/modules&gt;
  77. &lt;launcher&gt;
  78. &lt;name&gt;test33&lt;/name&gt;
  79. &lt;module&gt;module-info.java/elitonlais.app&lt;/module&gt;
  80. &lt;/launcher&gt;
  81. &lt;compression&gt;2&lt;/compression&gt;
  82. &lt;stripDebug&gt;true&lt;/stripDebug&gt;
  83. &lt;outputDirectory&gt;${project.build.directory}/jlink-image&lt;/outputDirectory&gt;
  84. &lt;/configuration&gt;
  85. &lt;/execution&gt;
  86. &lt;/executions&gt;
  87. &lt;/plugin&gt;
  88. &lt;/plugins&gt;
  89. &lt;/build&gt;
  90. &lt;/project&gt;

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

答案1

得分: 1

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

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

  1. &lt;executions&gt;
  2. &lt;execution&gt;
  3. &lt;!-- 这里放置你现有的代码 --&gt;
  4. &lt;/execution&gt;
  5. &lt;execution&gt;
  6. &lt;id&gt;add-module-infos&lt;/id&gt;
  7. &lt;phase&gt;generate-resources&lt;/phase&gt;
  8. &lt;goals&gt;
  9. &lt;goal&gt;add-module-info&lt;/goal&gt;
  10. &lt;/goals&gt;
  11. &lt;configuration&gt;
  12. &lt;outputDirectory&gt;${project.build.directory}/modules&lt;/outputDirectory&gt;
  13. &lt;modules&gt;
  14. &lt;module&gt;
  15. &lt;artifact&gt;
  16. &lt;groupId&gt;guru.nidi&lt;/groupId&gt;
  17. &lt;artifactId&gt;graphviz-java&lt;/artifactId&gt;
  18. &lt;version&gt;0.17.0&lt;/version&gt;
  19. &lt;/artifact&gt;
  20. &lt;moduleInfo&gt;
  21. &lt;name&gt;guru.nidi.graphviz&lt;/name&gt;
  22. &lt;exports&gt;
  23. *;
  24. &lt;/exports&gt;
  25. &lt;!-- 可能还有其他微调 --&gt;
  26. &lt;/moduleInfo&gt;
  27. &lt;/module&gt;
  28. &lt;module&gt;
  29. ...
  30. &lt;/module&gt;
  31. &lt;/modules&gt;
  32. &lt;/configuration&gt;
  33. &lt;/execution&gt;
  34. &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:

  1. &lt;executions&gt;
  2. &lt;execution&gt;
  3. &lt;!-- your existing code here --&gt;
  4. &lt;/execution&gt;
  5. &lt;execution&gt;
  6. &lt;id&gt;add-module-infos&lt;/id&gt;
  7. &lt;phase&gt;generate-resources&lt;/phase&gt;
  8. &lt;goals&gt;
  9. &lt;goal&gt;add-module-info&lt;/goal&gt;
  10. &lt;/goals&gt;
  11. &lt;configuration&gt;
  12. &lt;outputDirectory&gt;${project.build.directory}/modules&lt;/outputDirectory&gt;
  13. &lt;modules&gt;
  14. &lt;module&gt;
  15. &lt;artifact&gt;
  16. &lt;groupId&gt;guru.nidi&lt;/groupId&gt;
  17. &lt;artifactId&gt;graphviz-java&lt;/artifactId&gt;
  18. &lt;version&gt;0.17.0&lt;/version&gt;
  19. &lt;/artifact&gt;
  20. &lt;!-- only use one of either moduleInfo or moduleInfoSource --&gt;
  21. &lt;moduleInfo&gt;
  22. &lt;name&gt;guru.nidi.graphviz&lt;/name&gt;
  23. &lt;exports&gt;
  24. *;
  25. &lt;/exports&gt;
  26. &lt;!-- possibly other fine-tuning --&gt;
  27. &lt;/moduleInfo&gt;
  28. &lt;!-- only use one of either moduleInfo or moduleInfoSource --&gt;
  29. &lt;moduleInfoSource&gt;
  30. module guru.nidi.graphviz {
  31. requires ...;
  32. exports ...;
  33. provides ...
  34. with ...;
  35. }
  36. &lt;/moduleInfoSource&gt;
  37. &lt;!-- or use moduleInfoFile instead of the above --&gt;
  38. &lt;/module&gt;
  39. &lt;module&gt;
  40. ...
  41. &lt;/module&gt;
  42. &lt;/modules&gt;
  43. &lt;/configuration&gt;
  44. &lt;/execution&gt;
  45. &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:

确定