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

2

21

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?

Schismatic answered 24/9, 2017 at 17:5 Comment(12)
You could always use an underscore instead. Hyphens are not allowed in package/module names. See here.Tycho
You mean to rename the artificat in the local repository ?Schismatic
You would have to rename it in some way.Tycho
You should submit that as a bug to ApacheArose
The (maven) group id name is not necessarily the same as the (java) module name. They are separate concepts with different restrictions for their names.Kidder
openjdk.java.net/projects/jigsaw/spec/sotms/#automatic-modules Mentions that automatic module names are derived from jar names (which I assume you are using?), just not how. Could try commons.ioCalcariferous
@JornVernee here: download.java.net/java/jigsaw/docs/api/java/lang/module/……​- look for ofChristensen
@JornVernee , mentioning "commons.io" worked. If you post this as an answer, i can mark it as answered.Schismatic
It's mentioned in the javadocs 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.Techy
@cobra I just guessed correctly, but I didn't have the explanation. I'll leave answering up to someone who does.Calcariferous
@JornVernee the explanation on how dot works is in the javadoc for java.lang.module.ModuleFinder I linked aboveChristensen
mvn dependency:resolve will show you the module name per dependency when running with Java 9Flybynight
D
18

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.

Dry answered 24/9, 2017 at 17:50 Comment(1)
with the latest version you can use requires org.apache.commons.io;Reflector
A
10

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.

[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
Ajay answered 24/9, 2017 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.