如何为名称中带有“ - ”(连字符)的工件添加“要求”

问题描述 投票:20回答:2

我在Maven pom.xml中包含了这些依赖项:

 <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
</dependency>

<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
</dependency>

我试图在module-info.java中添加这种依赖,如下所示:

module io.github.wildcraft.restclient {
    requires httpcore; // no compilation problem
    requires httpclient; // no compilation problem
    requires commons-io; // shows compilation error
}

对于commons-io,我收到编译错误。我怎样才能做到这一点?

java maven java-9 java-module jigsaw
2个回答
13
投票

Short Version

使用requires commons.io。 (一般情况下,请参阅nullpointer's answer如何学习模块的名称。)

Long Version

由于commons-io.jar尚未模块化,因此您正在创建一个自动模块,模块系统必须为其创建一个名称。 Javadoc of ModuleFinder描述了这种情况:

此方法返回的模块查找器支持打包为JAR文件的模块。 [...]在顶级目录中没有module-info.class的JAR文件定义了一个自动模块,如下所示:

  • 如果JAR文件在其主清单中具有属性“Automatic-Module-Name”,则其值为模块名称。否则,模块名称是从JAR文件的名称派生的。
  • 版本和模块名称[...]是从JAR文件的文件名派生的,如下所示: [...] 模块名称中的所有非字母数字字符([^ A-Za-z0-9])将替换为点(“。”),所有重复点将替换为一个点,并删除所有前导点和尾随点。

最后两个项目符号适用于未为Java 9准备的自动模块,例如到commons.io。来自同一个Javadoc的这个例子解释了你的情况会发生什么:

  • 例如,名为“foo-bar.jar”的JAR文件将派生模块名称“foo.bar”而没有版本。名为“foo-bar-1.2.3-SNAPSHOT.jar”的JAR文件将派生模块名称“foo.bar”和“1.2.3-SNAPSHOT”作为版本。

因此requires commons.io应该工作。


8
投票

添加到Nicolai提供的较短版本的answer。为了找出项目中使用的依赖项(jar)的模块名称,可以从命令行使用jar tool

jar --file=<jar-file-path> --describe-module 

由于这些工具被理解为自动模块,因此输出有点像:

$ / jar --file=commons-lang3-3.6.jar --describe-module
No module descriptor found. Derived automatic module.

[email protected] automatic // this is what you need to use without the version

requires java.base mandated
contains org.apache.commons.lang3
contains org.apache.commons.lang3.arch
contains org.apache.commons.lang3.builder
contains org.apache.commons.lang3.concurrent
contains org.apache.commons.lang3.event
contains org.apache.commons.lang3.exception
contains org.apache.commons.lang3.math
contains org.apache.commons.lang3.mutable
contains org.apache.commons.lang3.reflect
contains org.apache.commons.lang3.text
contains org.apache.commons.lang3.text.translate
contains org.apache.commons.lang3.time
contains org.apache.commons.lang3.tuple
© www.soinside.com 2019 - 2024. All rights reserved.