Video in progress

Is it worth moving a large monolith to JPMS (module-info.java)? What criteria do you decide by?

JPMS gives strong encapsulation and explicit dependencies, but in a stack that relies on reflection, like Spring/Hibernate, opens and split-package problems take a lot of work. The benefit is often bigger at the library and platform level than for an application.

📖 Answer

First I separate the question: the JDK itself is modularized (Java 9+) and that affects everyone, which is why you end up needing --add-opens. But turning your own code into modules is optional; code on the classpath keeps working as the "unnamed module".

What JPMS gives you:

  • a package that isn't exportsed is invisible from outside, even its public classes. Another team using your "internal" packages is stopped at compile time;
  • requires: the dependency graph is checked at the compiler level, and cycles are forbidden;
  • a small runtime image with jlink.

The cost:

  • Spring, Hibernate and Jackson get into your classes with reflection, so you have to write opens ... to ... for every package;
  • split packages: if two JARs provide the same package, it doesn't work on the module path. This is common in old libraries;
  • automatic module names (taken from the JAR name when there's no Automatic-Module-Name) are unstable;
  • Maven/Gradle, testing (--patch-module) and IDE settings all get more complicated.

How I decide:

Situation Recommendation
A library/SDK that other teams use At least Automatic-Module-Name, and a full module-info if possible
A Spring Boot application, one team Usually no; holding the boundaries with ArchUnit or Gradle/Maven modules is cheaper
A modular monolith, 10+ teams Spring Modulith, or build modules + ArchUnit; JPMS only as a pilot
You need a small image with jlink (CLI, desktop) Yes

First define the goal. If it's "teams shouldn't reach into each other's internal code", you get 80% of that at no cost with build-tool modules and architecture tests. JPMS gives you the remaining 20% (a strict boundary at runtime), but you'd have to adapt the whole ecosystem to it.

If you do start, go from the bottom up: first the outermost modules without reflection, then the domain modules. At each stage, check the graph with jdeps --check and jdeps -summary.