Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Java 9 module system integrate into Maven, how to handle automatic module

Tags:

maven

java-9

I have already practiced java 9 module system integration with Maven project. and it seems to work well but I was anxious about backward compatibility of maven module compiled before java 9.

For backward compatibility, JPMS(Java Platform Module System) has Automatic Module Concept. In JPMS, Jar file without module-info.java is regarded as Automatic Module and other java module can use this jar file by using its file name as module name.

In case of integration with maven project, I think additional support for JPMS backward compatibility needed. first, basic jar file and maven jar file is not same structure. second, maven jar file naming is incompatible with rule can be compatibility with JPMS module naming rule. see the below example.

<artifactId>previous-version-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

In above cases, We already uses previous-version-module-0.0.1-SNAPSHOT and want to use this module in new java 9 maven project.

module new.module{
  requires previous-version-module-0.0.1-SNAPSHOT
}

but it occur compile error or IDEA tool throw error.

I'm wondering how to use maven jar file which compiled before java 9 in java 9 and maven integration project. I searched some additional module definition rule or maven plugin, I can't find any solution.

Thanks.

like image 367
Junhaeng Heo Avatar asked Aug 07 '17 02:08

Junhaeng Heo


People also ask

How to add the Java module to the Maven project?

The following directory structure is automatically created. Now it is time to add the Java module to the example. Remember, in Part 1, we added the Java module right below the src directory. But with Maven, the convention is to place the sources in the directory src - main - java. So, we will create the Java module inside this directory.

How do I get automatic module names in Java 9?

Check out how automatic module names are derived at the end of Java 9 Automatic Modules tutorial. Above output is taken from Intellij which integrates with maven and runs the following command (you can find that in the Run view, shortcut Alt+4): Intellij also has autocompletion support for automatic module names (even if it's maven project):

What are the advantages of Maven over Java?

With Maven, you can control the versions of these modules and the dependencies between these modules. Each module will produce an artifact. Java modules are a way to strongly encapsulate your classes.

How to create a module-info in Java 9?

I solved it by right-clicking the directory src - main - java and choosing the option Open Module Settings. For some reason, it was set to the language level of Java 5. After changing it to Java 9, the option was available. Now, create the module-info.java and call the module com.mydeveloperplanet.jpmshello.


1 Answers

If the module name isn't obvious then you can use jar --file=<jarfile> --describe-module to see the derived name. Alternatively, use the --list-modules option, i.e. java -p <jarfile> --list-modules.

Once you know the module name then you use requires <module> and it should work. The maven-compiler-plugin puts all dependences on the module path when building a project that has a module-info.java in the source tree.

like image 131
Alan Bateman Avatar answered Oct 29 '22 04:10

Alan Bateman