如何为名称中包含“-”(连字符)的工件添加 "requires"

How to add "requires" for artifact having "-"(hyphen) in its name

我已经在我的 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>

我正在尝试在模块-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,我收到一个编译错误。我怎样才能使这项工作?

精简版

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

长版

由于commons-io.jar 尚未模块化,您正在创建一个自动模块,模块系统必须为其命名。 Javadoc of ModuleFinder 描述了这是如何发生的:

The module finder returned by this method supports modules packaged as JAR files. [...] A JAR file that does not have a module-info.class in its top-level directory defines an automatic module, as follows:

  • If the JAR file has the attribute "Automatic-Module-Name" in its main manifest then its value is the module name. The module name is otherwise derived from the name of the JAR file.

  • The version and the module name [...] are derived from the file name of the JAR file as follows:

    • [...]

    • All non-alphanumeric characters ([^A-Za-z0-9]) in the module name are replaced with a dot ("."), all repeating dots are replaced with one dot, and all leading and trailing dots are removed.

最后两个项目符号适用于未为 Java 9 准备的自动模块,例如至 commons.io。来自同一 Javadoc 的这个示例解释了您的案例中发生的情况:

  • As an example, a JAR file named "foo-bar.jar" will derive a module name "foo.bar" and no version. A JAR file named "foo-bar-1.2.3-SNAPSHOT.jar" will derive a module name "foo.bar" and "1.2.3-SNAPSHOT" as the version.

因此 requires commons.io 应该有效。

从命令行添加到 provided by Nicolai. In order to find out the module name of the dependencies(jar) used in your project, you can use the 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.

org.apache.commons.lang3@3.6 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