Will There Be Module Hell?
Project Jigsaw aims to address some of the common issues Java developers face with JAR files, but it may not be a silver bullet, as Nicolai Parlog explains.
Join the DZone community and get the full member experience.Join For Free
to know what we are talking about we’ll start with a quick recap of jar hell. we will then discuss what aspects jigsaw touches on and how that might not change the big picture. lastly we will have a look at the official stance on the topic and formulate a proposal to prevent looming module hell.
i discussed jar hell in detail in my last post , which you might want to read if you haven’t already. it ends with this list of the different circles of jar hell:
- unexpressed and transitive dependencies
- version conflicts
- complex class loading
based on what build tools and component systems (called containers by the jdk developers) bring to the game and how widely they are used it concludes that unexpressed and transitive dependencies are largely solved, shadowing at least eased and complex class loading not commonplace.
this leaves version conflicts as the most problematic aspect of jar hell, influencing everyday update decisions in many, many projects.
what will change with jigsaw?
i have already written about all the features project jigsaw was planned to bring to java 9 but this post will take a different angle. first, it is influenced by experiments with the current early access build and, second, it only looks at the aspects pertaining jar/module hell.
the core concept jigsaw brings to java are modules . overly simplified, a module is like a jar with some additional information and features. some of those pieces of information are a module’s name and the names of other modules it depends on.
the information is interpreted by the compiler and the jvm when they process a module. tasked to compile or launch one, they will transitively resolve all dependencies within a universe of modules specified via the module path . roughly said, this is analogue to a class path scan but now we are looking for entire modules instead of individual classes and, in case of the jvm, we are doing it at launch-time not at runtime.
jigsaw solves the problem of unexpressed and endlessly transitive dependencies.
resolving the transitive dependencies of a module fails with an error if not all modules are found on the module path. this clearly solves the problem of unexpressed and endlessly transitive dependencies.
i see it as a material benefit that the java language now officially knows about dependencies and that all the tools, starting with the compiler and jvm, understand and work with them! this should not be understated.
but i assume it will have little effect on the typical developer’s everyday work since this is already sufficiently addressed by existing infrastructure, i.e. the build tool.
this becomes even clearer when we consider where the module information will come from. it already exists as part of the build information, e.g. in the pom.xml. it would be redundant to additionally specify names and dependencies for the module system and it is hence assumed that the build tool will use its information to automatically generate the module information. (i am sure mark reinhold or alan bateman repeatedly stated this but can’t find a quote right now. store this as hearsay for now.)
jigsaw eliminates the problem of shadowing:
the module system ensures that every dependence is fulfilled by precisely one other module, […] that every module reads at most one module defining a given package, and that modules defining identically-named packages do not interfere with each other.
to be more precise, the module system quits and reports an error as soon as it encounters ambiguous situations, e.g. two modules exporting the same package to the same module.
we identified conflicting versions of third party libraries as the most daunting aspect of jar hell. the most straight forward solution would be a module system able to load different versions of the same module. it would have to prove that these versions can not interact but given the strong promises regarding encapsulation and readability it looks like it should be able to do that.
now, here is the problem:
it is not necessary to support more than one version of a module within a single configuration.
indeed the current build neither creates nor understands module version information.
for some time it looked like there would be workarounds. the ugliest but most promising one renames the conflicting artifacts so that they are no longer two different versions of the same module but appear as two different modules, coincidently exporting the same packages.
jigsaw does nothing to help with the problem of conflicting versions.
but this approach fails. apparently ensuring “that modules defining identically-named packages do not interfere with each other” is solved by roundly rejecting any launch configuration where two modules export the same packages. even if no module would read them both!
so apparently jigsaw does nothing to help with the problem of conflicting versions unless one resorts to component-system-like behavior at runtime. what a disappointment!
complex class loading
discussing how modules and class loaders interact and how that might change the complexity of class loading deserves its own post. preferably by someone more experienced with class loaders.
let’s just have a look at the basics.
the module system, in fact, places few restrictions on the relationships between modules and class loaders. a class loader can load types from one module or from many modules, so long as the modules do not interfere with each other and the types in any particular module are loaded by just one loader.
so there will be a 1:n-relationship of class loaders to modules.
then there is the new notion of layers , which component systems can use to structure class loader hierarchies.
a layer encapsulates a module graph and a mapping from each module in that graph to a class loader. the boot layer is created by the java virtual machine at startup by resolving the application’s initial module against the observable modules built-in to the run-time environment and also against those found on the module path.
layers can be stacked: a new layer can be built on top of the boot layer, and then another layer can be built on top of that. as a result of the normal resolution process the modules in a given layer can read modules in that layer or in any lower layer.
so while the class loader system gets more elements, the mechanics and best practices might improve, possibly resulting in less complexity of well designed systems. at the same time the new fail-fast properties regarding dependencies and shadowing will make problems more obvious and troubleshooting easier.
so all in all it looks like this problem does not go away but becomes less vexing.
with dependencies and shadowing solved and class loading improved why would i talk about module hell? just because of version conflicts? short answer: yes!
if jigsaw wants to solve jar hell, it has to address version conflicts.
long answer: take a look at the search results for jar hell – the topic of conflicting versions is by far the most common motivator for discussing this. of all the aspects we talked about so far it is the only one that commonly plagues the majority of projects (at least by my conjecture).
so if jigsaw wants to solve jar hell, it has to address version conflicts! otherwise not much might change for many projects. they will still struggle with it and they will continue to get themselves into custom built class loader nightmares. sounds like module hell to me.
official stance on versions
so what is the official stance on the topic of versions?
it is not necessary to support more than one version of a module within a single configuration.
most applications are not containers and, since they currently rely upon the class path, do not require the ability to load multiple versions of a module.
i strongly disagree with this assessment! as i said before, i am convinced that this is a problem for pretty much any project. in fact, i believe that the quoted rationale reverses cause and effect.
in my opinion it’s more like this:
most applications decide against the complexity of running a container and, since they are consequently stuck with the class path, are not able to load multiple versions of a module.
and why does the current early access build go even further and completely abandon version information?
a module’s declaration does not include a version string, nor constraints upon the version strings of the modules upon which it depends. this is intentional: it is not a goal of the module system to solve the version-selection problem, which is best left to build tools and container applications.
it is easy to agree with the premise. many tools have tackled the non-trivial problem of version selection and there is no need to bake one of those solutions into the vm.
but i fail to see what this has to do with completely ignoring version information. and it does also not exclude letting an external tool select the versions and pass its solution to the launching vm.
summarized, the official stance regarding conflicting versions is this:
the module system isn’t suggesting any solutions, it is instead leaving this problem to the build tools and containers.
which sounds great except that the module system does currently not provide any new mechanisms for build tools to solve this longstanding and fundamental problem.
given only an initial module and a universe of modules to resolve dependencies within, the current jvm refuses to launch if any ambiguities, e.g. two versions of the same module, are encountered. this is very reasonable behavior and i would not change it.
my proposal is to enable developers and build tools to pass additional information that solve ambiguous situations. (while i thought through the proposal ali ebrahimi independently made the same one .)
the two common ways to pass such information are the command line and configuration files.
command line arguments would have to be repeated on every launch. depending on how comprehensive the information and how large the project is, this could be tedious.
a configuration file could be created by the build tool and later specified via command line. this looks like the best approach to me.
currently, the initial module and all transitive dependencies are resolved as a single configuration , which is used to create a single layer. but it is already straight forward to load multiple versions of the same module into different layers at runtime. (this is what component systems might do in the future.)
all that is needed are explicit configurations with multiple layers.
so all that is needed is to allow users to explicitly specify configurations with multiple layers. the jvm would parse this when it launches and create the layers accordingly.
looking at the current goals, requirements and capabilities this fits in quite nicely. especially since it does not implement version selection and does not require new module system capabilities. and it would be a nice feature to enable complex configurations at launch-time regardless of version conflicts. i am sure there are other use cases.
as an add-on, it might be interesting to think about partial configurations. they would only specify those parts of the module graph that are of special interest, e.g. because of conflicting versions. everything else could be resolved relative to them.
this is not meant to replace existing component systems! users of osgi, wildfly, … most likely have more reasons to use them than just version conflicts. instead it would be an entry-level mechanism usable by every project out there without much additional complexity.
in the first part we assessed how project jigsaw addresses jar hell:
- unexpressed and transitive dependencies : solved
- shadowing : solved
- version conflicts : untouched
- complex class loading: remains to be seen
since version conflicts are the most relevant aspect of jar hell today, we concluded that they will give rise to module hell tomorrow.
to prevent that, a proposal was made that requires no notable changes to the module system and utilizes already existing features:
enable explicitly specified configurations with multiple layers.
you can give this proposal more weight by sharing it with the community:
Published at DZone with permission of Nicolai Parlog, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.