Over the first four articles of our Gradle Under the Hood series, we explored source-driven incremental tracking, network-immune classloader harvesting, compile-only dependency isolation, and our federated plugin architecture.
Today, in Part 5, we tackle one of the biggest friction points in modern enterprise Java builds: How do you run build-time JPA tooling—like bytecode enhancement, schema validation, and query linting—against modular Java applications (module-info.java) without forcing developers to weaken their module encapsulation?
Let’s look at how we engineered JPersist v1.9.0 to support hybrid JPMS execution, dynamic ModuleDescriptor reflection opens, and vendor-specific provider settings out of the box.
The JPMS Build-Time Tooling Wall
The Java Platform Module System (JPMS) provides strong encapsulation and reliable configuration. However, build-time persistence tools—especially Hibernate—rely heavily on deep reflection (Field.setAccessible(true)) to inspect entity classes, generate metamodels, and validate schemas.
When a project introduces a module-info.java descriptor, standard Gradle workers running on a flat classpath hit a wall:
- Encapsulation Blockades: Unless the user explicitly adds
opens com.example.domain to io.github.jpersist.tools.hibernate.orm;inside their productionmodule-info.java, Hibernate throws anInaccessibleObjectExceptionduring schema validation or query linting. Forcing developers to pollute production module descriptors just to satisfy build-time linters is an anti-pattern. - Process Fork Overhead: Running every project inside a forked JVM process (
processIsolation) guarantees modular boundaries, but adds unnecessary startup latency for traditional, non-modular classpath projects.
The Solution: Hybrid Execution Isolation (JPMSWorkerSupport)
To get the best of both worlds, we designed JPMSWorkerSupport—a hybrid execution router that inspects the target source set's compiled output directory at task execution time:
static boolean isModularProject(File classesDir) { return classesDir != null && new File(classesDir, "module-info.class").exists() } Depending on whether module-info.class is present, JPersist dynamically chooses the optimal Gradle WorkerExecutor isolation strategy:
- Non-Modular Projects (Classpath): Executes in-process using fast
classLoaderIsolation, avoiding any JVM fork overhead. - Modular Projects (JPMS): Switches automatically to hermetic OS-level
processIsolation, placing the user's classes and dependencies onto--module-pathwhile keeping Gradle's worker action bridge files on the bootstrap classpath.
Dynamic ModuleDescriptor Inspection & --add-opens Injection
To prevent users from having to manually open their entity packages in module-info.java, JPMSWorkerSupport reads the compiled module-info.class binary directly using Java's standard java.lang.module.ModuleDescriptor API before spawning the worker JVM:
private static void appendDynamicUserModuleOpens(File moduleInfoFile, List<String> jvmArgs) { try { byte[] bytes = Files.readAllBytes(moduleInfoFile.toPath()) ModuleDescriptor descriptor = ModuleDescriptor.read(ByteBuffer.wrap(bytes)) String moduleName = descriptor.name() descriptor.packages().each { String pkg -> jvmArgs.add('--add-opens') jvmArgs.add("\${moduleName}/pkg={HIBERNATE_MODULES}".toString()) } } catch (Exception ignored) { // Safe fallback if module-info.class cannot be parsed } } At runtime, JPersist automatically constructs the exact --add-opens <userModule>/<package>=io.github.jpersist.tools.hibernate.orm,io.github.jpersist.tools.hibernate flags for the forked worker JVM. Production encapsulation stays completely intact, while build-time tools get temporary, scoped reflective access.
Dual-Layer SPI Resolution (ModuleLayer vs. ClassLoader)
Because workers can now boot either on a JPMS --module-path or a traditional ClassLoader, our underlying SPI resolution layer (PersistenceServiceResolver) adapts dynamically inside the worker process:
- When
ModuleLayer.boot()contains ourio.github.jpersist.tools.apimodule, factories (NamedObjectParserFactory,SchemaValidatorFactory,EnhancerProvider) are discovered viaServiceLoader.load(ModuleLayer.boot(), serviceType). - Otherwise, it falls back seamlessly to
Thread.currentThread().contextClassLoader.
Fine-Tuning Provider Behavior with Custom settings
Real-world enterprise schemas often rely on custom SQL dialects, specific default schemas, or strict JPA compliance flags. In v1.9.0, we also exposed @Input @Optional MapProperty<String, Object> getSettings() across both LinterExtension and ValidationExtension:
persistence { main { validation { settings = [ 'hibernate.default_schema': 'APP_CORE', 'hibernate.physical_naming_strategy': 'org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy' ] } linter { settings = [ 'hibernate.dialect': 'org.hibernate.dialect.PostgreSQLDialect', 'hibernate.jpa.compliance.query': 'true' ] } } } These properties are serialized cleanly across the Gradle Configuration Cache boundary and passed directly into the isolated worker's SPI factory initialization.
The Engineering Win
By combining hybrid worker isolation with bytecode-level ModuleDescriptor inspection, JPersist v1.9.0 removes the friction of adopting JPMS in enterprise persistence projects:
- Clean Production Descriptors: Zero need to pollute
module-info.javawith build-tool-onlyopensdirectives. - Zero Overhead for Legacy Modules: Non-modular subprojects inside the same multi-module build continue running in-process without spawning extra JVMs.
- Full Vendor Control: Custom dialects, naming strategies, and compliance settings propagate cleanly into isolated worker sandboxes.
Are you adopting JPMS (module-info.java) in your enterprise Java builds, and what build-tool hurdles have you run into? Let's discuss in the comments below!
🔗 Explore the complete JPersist architecture on GitHub: https://github.com/jpersist/persistence

