ascheman opened a new pull request, #1080:
URL: https://github.com/apache/maven-enforcer/pull/1080

   Implements the three deterministic Java module rules proposed in #995.
   
   > [!NOTE]
   > **Supersedes #996**, whose description no longer matched its contents 
after the scope grew. The review history there is still worth reading and is 
referenced below; this PR carries the same commits plus the follow-ups from 
that discussion.
   
   ## Rules
   
   - **`requireExplicitModules`** — fails a project that compiles classes but 
ships no module descriptor, so an artifact never silently resolves as an 
*automatic module*.
   - **`requireMinimalExports`** — fails a module that `exports` an 
internal/impl package (configurable `internalPackagePattern`, `allowedExports` 
whitelist, `ignoreQualifiedExports`).
   - **`banUnjustifiedOpens`** — fails an unqualified `opens` (or an `open 
module`); qualified `opens … to` and an `allowedOpens` whitelist pass.
   
   ## Where the descriptor is read, and when
   
   Each rule reads the compiled `module-info.class`, which exists only once the 
project has been built — so the `enforce` execution must be bound accordingly. 
Two layouts are supported: the classic one (descriptor in the output directory, 
one Maven project = one Java module) and the **Maven 4 module source 
hierarchy** (POM model 4.1.0), where one project compiles several modules into 
`${project.build.outputDirectory}/<module-name>/`.
   
   Where a plugin **injects the descriptor into the packaged artifact** after 
compilation — as [Moditect](https://github.com/moditect/moditect) does, 
typically on a Java 8 baseline where a `module-info.java` cannot be compiled at 
all — the output directory legitimately never holds one. The rules then fall 
back to the packaged artifact: the root `module-info.class`, otherwise the 
highest-versioned `META-INF/versions/<n>/module-info.class`. The descriptor is 
parsed rather than recognised by entry name, so an unreadable one counts as 
absent and keeps the stricter outcome. That fallback covers the classic 
one-module layout only — one main artifact cannot stand for the several modules 
of a source hierarchy. Such a project must bind the execution at or after 
`package`, which the rule documentation now spells out.
   
   **A rule that cannot inspect anything says so.** The `enforce` goal defaults 
to the `validate` phase, at which no class is compiled yet: previously the 
rules found nothing and reported success, which in a log is indistinguishable 
from a clean result. They now emit a warning naming their documentation page 
when they had nothing at all to inspect, and where a configured plugin known to 
inject descriptors has not run yet, the warning names that plugin. Projects 
that build no JAR, and built projects that are simply not modular, stay silent.
   
   ## `module-info` reader — design note
   
   The rules read `module-info.class` through 
`java.lang.module.ModuleDescriptor`, accessed **reflectively**. The plugin 
compiles with `--release 8` and so cannot reference the Java 9 API directly. 
The obvious alternative — a multi-release JAR overlay compiled for Java 9 — is 
deliberately avoided: MRJAR support in Maven 3 is incomplete and can produce 
invalid JARs (MNG-6892, MNG-6293, `maven-jar-plugin#484`), and is cleanly 
solved only in Maven 4.
   
   The rules therefore stay usable on **Maven 3 with a Java 8 baseline**. 
Reading a descriptor requires a Java 9+ runtime, and the guard for that fires 
only once a descriptor has actually been found, so a Java 8 build that has no 
descriptor is unaffected. A `module-info.class` that is present but unparseable 
fails with a clear diagnostic instead of being silently treated as absent.
   
   ## Tests
   
   - Unit tests for the reader and each rule, guarded to Java 9+; ASM is used 
in **test scope only** to generate `module-info.class` fixtures, so production 
code stays dependency-free.
   - Six ITs under `maven-enforcer-plugin/src/it/projects/`: four classic 
single-module (one pass, three fail) and two Maven 4 module-source-hierarchy 
(pass/fail), gated by JDK and Maven version so they skip cleanly on older 
toolchains.
   - Site pages for the three rules, listed in the built-in rules index, 
including the phase-binding guidance and the Moditect case.
   
   ## Review history on #996
   
   Worth not losing, and all of it is already folded into these commits:
   
   - **@Bukama** asked whether the Java 8 guard holds for a Moditect-style 
build 
([comment](https://github.com/apache/maven-enforcer/pull/996#issuecomment-6397207488)).
 The guard did hold, but the question uncovered a different bug: the rules 
never looked at the artifact, so `requireExplicitModules` failed any project 
whose descriptor is injected, on every JDK 
([reply](https://github.com/apache/maven-enforcer/pull/996#issuecomment-6725986586)).
 That is what the artifact fallback above fixes, and following the same thread 
turned up the `validate`-phase false pass.
   - **@elharo** raised ten points — type visibility, defensive copies behind 
the immutable accessors, `&nbsp;` in javadoc, `readAllBytes` vs. the Java 8 
baseline, wording — and questioned whether these rules are needed in a Java 8 
environment at all, which is what the Java 9+ runtime guard answers.
   - **@scordio** [tested the rules against the AssertJ 
build](https://github.com/apache/maven-enforcer/pull/996#issuecomment-5037420928)
 and reported success.
   - **Copilot** contributed five findings: non-deterministic iteration over 
`requires`/`exports`/`opens` across JDKs, null-valued list parameters from 
configuration injection, and a fixture writing class files to the wrong path.
   
   A split-package rule across the module path follows in #1000, which is 
stacked on this branch.
   
   ---
   - [x] Your pull request should address just one issue, without pulling in 
other changes.
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line 
and body.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to