In Java 9, you can optionally package a source directory as a module by adding a module-info.java, which defines the things packages that it exports, and which other modules it depends on.
Once you do that, however, you must list ALL dependencies in the module descriptor - and the dependencies must all themselves be modules. Therefore, by extension, if you modularize a single source directory, you must modularize every single source directory company wide.
Furthermore, you cannot define modules in Java 8 or earlier, meaning that in addition to modularizing every single Java source directory, you must convert everything to Java 9. At the same time.
This seems catastrophic if you work in a company with a large base of code that is shared by many different projects.
For now, I can work around the problem by just setting a bunch of compiler flags to avoid defining modules, but that seems like a very poor solution.
I hope that I am understanding this incorrectly?
Java 8 applications use packages as a top-level component whereas Java 9 applications use modules as a top-level component. Each Java 9 module has only one module with one module descriptor whereas Java 8 package doesn't build multiple modules into a single module.
Each module-info. java which lives in the default package will be compiled to module-info. class . Therefore, one JAR cannot contain multiple modules.
Project Jigsaw is an umbrella project with the new features aimed at two aspects: the introduction of module system in the Java language. and its implementation in JDK source and Java runtime.
Once you do that, however, you must list ALL dependencies in the module descriptor
True.
and the dependencies must all themselves be modules.
Technically true, but it doesn't imply what you think it does.
Therefore, by extension, if you modularize a single source directory, you must modularize every single source directory company wide.
No, because you can let the module system turn regular old JARs into automatic modules, which will get a name based on a manifest entry or their file name - you can find that out with:
# jar command from Java 9
jar --describe-module --file $JAR_FILE
Furthermore, you cannot define modules in Java 8 or earlier, meaning that in addition to modularizing every single Java source directory, you must convert everything to Java 9. At the same time.
Again, fortunately that's not quite right. You can add a module-info.class
to a JAR built for Java 8 and it will work on both Java 8 (which ignores that file) and Java 9 (which can of course execute Java 8 bytecode).
if you modularize a single source directory, you must modularize every single source directory company-wide.
No, that does not hold true, for the fact, that is what automatic modules are designed for and
(to favor the impulse) Yes, eventually that shall be the goal of modularisation.
Reiterating the need of introducing the automatic modules from The State of the Module System:
Bottom-up migration is straightforward, but it is not always possible. Even if the maintainer of
org-baz-qux.jar
has not yet converted it into a proper module—or perhaps never will—we might still want to modularize ourcom-foo-app.jar
andcom-foo-bar.jar
components.
When you actually say :
you can optionally package a source directory as a module by adding a module-info.java
you tend to migrate that artifact into a module(with module description) and place such modules in the module-path of the libraries using this artifact further.
On the other hand, a .jar
of your library without the module-info.class
is considered to be present at the class-path when included as a dependency in downstream projects.
Edit from comments:-
it's possible to mix Java 9 source with Java 8 compiled jars, but you can't compile a single project containing Java 9 source and Java 8 source?
Yes, its possible to mix Java9 source with Java8 compile jar and you can still compile them into a single project as well.
Example:- take a look at how Maven does this using maven-compiler-plugin
for Java8 projects with module-info.java
included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With