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

寵の児 提交于 2019-12-21 07:08:20

问题


I've included these dependencies in my 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>

I am trying to add this dependency in module-info.java like so:

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

For commons-io, I receive a compilation error. How can I make this work?


回答1:


Short Version

Use requires commons.io. (In general, see nullpointer's answer how to learn a module's name.)

Long Version

Since commons-io.jar is not yet modularized, you are creating an automatic module, for which the module system has to come up with a name. The Javadoc of ModuleFinder describes how that happens:

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.

The last two bullets apply to automatic modules that are not prepared for Java 9, e.g. to commons.io. This example from the same Javadoc explains what happens in your case:

  • 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.

Hence requires commons.io should work.




回答2:


Adding to the shorter version of the answer 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 from command line.

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

Since these would be understood as an automatic module by the tool, the output would be somewhat like:-

$ / 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


来源:https://stackoverflow.com/questions/46392636/how-to-add-requires-for-artifact-having-hyphen-in-its-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!