Example example_naming-modules
Part of the full Java 9 Jigsaw modules example suite.
|
Authors
Originally written by Martin Lehmann, Kristine Schaal and RĂ¼diger Grammes (cf. original repository). Migrated for Java Modules support documentation of Apache MavenTM in the course of the Maven Support & Care program by Gerd Aschemann (and other team members) as forked repository. Please add discussions, requirements, bugfixes, etc. to the fork instead of the original. |
What is this example about?
Modules in this example
-
automatic-whatever, automatic-whatever2-47.11, automatic.sourcecode, java.whatever, jdk.whatever, mod-client, mod.client, mod.interface, modjava, modmain, mod_client
-
modmain has a Main class which is started in run.sh
Output
This example uses golden master testing to ensure output consistency.
The expected output is compared with actual output using verify.sh.
Expected Output
Main class pkgmain.Main from module java.whatever@0.1
Main class pkgmain.Main from module jdk.whatever@0.1
Main class pkgmain.Main from module mod.client@0.1
Main class pkgmain.Main from module mod_client@0.1
Main class pkgmain.Main from module modjava@0.1
Main class pkgmain.Main from module modmain@0.1
Main class pkgmain.Main from module automatic.whatever
Main class pkgmain.Main from module automatic.whateverX@47.11
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for ./automatic-whateverX48.12.jar
Caused by: java.lang.IllegalArgumentException: automatic.whateverX48.12: Invalid module name: '12' is not a Java identifier
Cannot execute Module automatic.whateverX48
Main class pkgmain.Main from module automatic.whateverX49@13
Actual Output
Main class pkgmain.Main from module java.whatever@0.1
Main class pkgmain.Main from module jdk.whatever@0.1
Main class pkgmain.Main from module mod.client@0.1
Main class pkgmain.Main from module mod_client@0.1
Main class pkgmain.Main from module modjava@0.1
Main class pkgmain.Main from module modmain@0.1
Main class pkgmain.Main from module automatic.whatever
Main class pkgmain.Main from module automatic.whateverX@47.11
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for ./automatic-whateverX48.12.jar
Caused by: java.lang.IllegalArgumentException: automatic.whateverX48.12: Invalid module name: '12' is not a Java identifier
Cannot execute Module automatic.whateverX48
Main class pkgmain.Main from module automatic.whateverX49@13
Maven 4 Output
Main class pkgmain.Main from module java.whatever@0.1
Main class pkgmain.Main from module jdk.whatever@0.1
Main class pkgmain.Main from module mod.client@0.1
Main class pkgmain.Main from module mod_client@0.1
Main class pkgmain.Main from module modjava@0.1
Main class pkgmain.Main from module modmain@0.1
Main class pkgmain.Main from module automatic.whatever
Main class pkgmain.Main from module automatic.whateverX@47.11
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for ./automatic-whateverX48.12.jar
Caused by: java.lang.IllegalArgumentException: automatic.whateverX48.12: Invalid module name: '12' is not a Java identifier
Cannot execute Module automatic.whateverX48
Main class pkgmain.Main from module automatic.whateverX49@13
Maven 4 Migration
This example required a special migration approach due to mixing explicit modules with automatic modules.
Standard Migration
The explicit modules (java.whatever, jdk.whatever, mod.client, mod_client, modjava, modmain) were migrated using the standard Module Source Hierarchy approach documented in the central Maven 4 Migration guide.
Special Handling: Automatic Modules
This example demonstrates automatic module naming conventions by compiling non-modular JARs without module descriptors.
- Problem
-
Maven 4’s Module Source Hierarchy requires module descriptors (module-info.java) for all source entries. The automatic modules (automatic-whatever, automatic-whateverX-47.11, automatic-whateverX48.12, automatic-whateverX49-13) intentionally lack module-info.java to demonstrate how module names are derived from JAR filenames. They cannot be included in the
<sources>element. - Solution
-
Hybrid compilation approach in
m4/compile.sh:-
Manual javac invocation compiles automatic modules to separate amlib1-4/ directories:
for dir in automatic-whatever automatic-whateverX-47.11 \ automatic-whateverX48.12 automatic-whateverX49-13 do javac -d classes/${dir} --release 11 $(find ../src/${dir} -name "*.java") jar --create --file=amlib${counter}/${dir}.jar -C classes/${dir} . done -
Maven compiles the explicit modules using Module Source Hierarchy
-
Runtime tests each module independently from its respective directory
-
This hybrid approach is necessary because:
-
Maven 4’s Module Source Hierarchy is designed for modular code only
-
Automatic modules must be compiled as non-modular JARs (without module-info.java)
-
Each automatic module is placed in a separate amlib*/ directory to demonstrate distinct naming scenarios
-
The example intentionally includes invalid module names (automatic-whateverX48.12) to demonstrate module naming validation