Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 418
schoolcoder Avatar asked Sep 24 '17 17:09

schoolcoder


People also ask

What is classifier in POM Xml?

classifier: The classifier distinguishes artifacts that were built from the same POM but differ in content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.

What is Maven type POM?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.

What are Maven coordinates?

Maven coordinates define a set of identifiers which can be used to uniquely identify a project, a dependency, or a plugin in a Maven POM.


2 Answers

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.

like image 114
Nicolai Parlog Avatar answered Oct 21 '22 16:10

Nicolai Parlog


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 
like image 28
Naman Avatar answered Oct 21 '22 15:10

Naman