在 java 9 中使用 jdeps 为自动模块创建模块信息 [英] creating module-info for automatic modules with jdeps in java 9
问题描述
我有 3 罐 jackson 库
I have 3 jar of jackson library
- jackson-core-2.8.10.jar
- jackson-annotations-2.8.0.jar
- jackson-databind-2.8.10.jar
我成功地为 core 和 annotation 创建了 module-info.java 并使用 jdeps 将它们转换为 Named maodule.
I created module-info.java for both core and annotation successfully and converted them to Named maodule using jdeps.
对于 databind ,我尝试了以下命令:
for databind , I tried following command:
jdeps --generate-module-info .--module-path %JAVA_HOME%jomds;jackson.core;jackson.annotations existingmodsjackson-databind-2.8.10.jar
jdeps --generate-module-info . --module-path %JAVA_HOME%jomds;jackson.core;jackson.annotations existingmodsjackson-databind-2.8.10.jar
现在出现以下错误:
Missing dependence: .jackson.databindmodule-info.java not generated
Error: missing dependencies
com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonCreator not found
com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonCreator$Mode not found
com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonFormat not found
com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonFormat$Value not found
com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonIgnoreProperties not found
com.fasterxml.jackson.databind.AnnotationIntrospector -> com.fasterxml.jackson.annotation.JsonIgnoreProperties$Value not found.
如何为 jackson-databind 生成 module-info.java ?
How can I generate module-info.java for jackson-databind ?
推荐答案
简短的回答是,是的,您必须将库转换为显式模块.
The short answer is that, yes, you'll have to convert the libraries to explicit modules.
jlink
工具旨在提供仅包含所需模块的修剪过的二进制图像.问题是自动模块可以访问可以读取所有 JDK 模块的类路径(也称为未命名模块).所以什么都不会被修剪.
The jlink
tool is intended to provide a trimmed binary image that has only the required modules. The issue is that automatic modules have access to the classpath (aka the unnamed module) which can read all JDK modules. So nothing would be trimmed.
这个帖子 也说明了这一点,带有指向 YouTube 视频的链接.
This thread states this as well, with a link to a YouTube video.
这个例子转换commons-lang3-3.5.jar
到 jlink
演示的显式模块.
This example converts commons-lang3-3.5.jar
to an explict module for a jlink
demo.
编辑:更具体地说,这里是一个例子 脚本,按顺序将 jackson-core
、jackson-annotations
和 jackson-databind
遗留 jar 转换为模块化 jar.
Edit: to be more specific, here is an example script that converts, in order, jackson-core
, jackson-annotations
, and jackson-databind
legacy jars to modular jars.
想法是:
- 在旧 jar 上运行
jdeps --generate-module-info
- 将遗留的 jar 解压到一个文件夹中,从上面添加
module-info.java
,重新编译并重新压缩
- run
jdeps --generate-module-info
on the legacy jar - unzip the legacy jar into a folder, add
module-info.java
from above, re-compile, and re-zip
诀窍是具有依赖项的模块化 jar 将需要这些依赖项作为命令行参数.例如,这里是 jackson-databind
(有些抽象):
The trick is that modular jars with dependencies will require those dependencies as command-line parameters. For example, here is jackson-databind
(abstracted somewhat):
# here, jackson-core and jackson-annotations have been built
# jackson-databind
jdeps --module-path $ROOT_DIR/modules
--add-modules jackson.annotations,jackson.core
--generate-module-info work $JACKSON_DATABIND_JAR
javac --module-path $ROOT_DIR/modules
--add-modules jackson.annotations,jackson.core
-d $ROOT_DIR/classes module-info.java
这篇关于在 java 9 中使用 jdeps 为自动模块创建模块信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!