gnodet-bot commented on code in PR #13494:
URL: https://github.com/apache/maven/pull/13494#discussion_r4615283217
##########
apache-maven/src/assembly/maven/bin/m2.conf:
##########
@@ -16,23 +16,80 @@
# specific language governing permissions and limitations
# under the License.
+# ============================================================================
+# Classworlds configuration for Apache Maven
+# ============================================================================
+#
+# This file defines the JPMS module layers and classloader realms used by
+# Maven at runtime. It is parsed by the classworlds Launcher before any
+# Maven code runs.
+#
+# Syntax reference
+# ----------------
+#
+# Global directives (before any section):
+# main is <class> from <realm> Entry point class and its realm.
+# set <prop> default <value> Set a system property if not already set.
+#
+# Layer and realm names share a single namespace — duplicates are forbidden.
+#
+# JPMS layer sections — [layer <name>]:
+# module <glob> Add JARs to this layer's module path.
+# Each layer becomes a sibling ModuleLayer
+# under ModuleLayer.boot(). ClassLoader
+# parents chain in declaration order.
+#
+# ClassLoader realm sections — [<name>]:
+# parent <name> Set the realm's parent classloader to
+# the named layer or realm.
+# load <glob> Add JARs to the realm's classpath.
+# optionally <glob> Add JARs if they exist, ignore otherwise.
+# import <pkg> from <realm> Import a package from another realm.
+# import-exported from <realm> Import all packages declared as exported
+# in META-INF/maven/extension.xml
descriptors
+# found on the source realm's classpath.
+#
+# JPMS access directives (any section or top-level):
+# add-exports <mod>/<pkg>=<target> Export a package from a named module.
+# add-opens <mod>/<pkg>=<target> Open a package for deep reflection.
+# add-reads <mod>=<target> Add a reads edge between modules.
+#
+# Property interpolation:
+# ${name} Replaced with the system property value.
+# ${name:-default} Use default when the property is absent or empty.
+#
+# Lines starting with '#' are comments. Blank lines are ignored.
+# ============================================================================
+
main is org.apache.maven.cling.MavenCling from plexus.core
set maven.conf default ${maven.home}/conf
-set maven.installation.conf default ${maven.conf}
+set maven.user.conf default ${user.home}/.m2
+# --- JPMS module layers ---
+# Layers are siblings under ModuleLayer.boot(). Their classloaders chain
+# in declaration order: infra → api, so plexus.core (parented to api)
+# can reach both layers via classloader delegation.
+
+[layer infra]
module ${maven.home}/lib/modules/*.jar
+[layer api]
+module ${maven.home}/lib/api/*.jar
+
+# --- ClassLoader realms ---
+
[plexus.core]
+parent api
load ${maven.conf}/logging
-optionally ${maven.home}/lib/ext/redisson/*.jar
-optionally ${maven.home}/lib/ext/hazelcast/*.jar
-optionally ${user.home}/.m2/ext/*.jar
+optionally ${maven.user.conf}/ext/*.jar
optionally ${maven.home}/lib/ext/*.jar
load ${maven.home}/lib/maven-*.jar
load ${maven.home}/lib/*.jar
-# The `load` below is a classpath fallback for embedders and older JDKs where
-# createModuleLayer() is not invoked. When the module layer IS created, these
-# entries are shadowed by the parent ModuleLayer loader (ParentFirstStrategy),
-# so there is no functional duplication in the normal case.
-load ${maven.home}/lib/modules/*.jar
+
+[maven.api]
+import-exported from plexus.core
+
+[maven.api.v4]
+parent layer api
Review Comment:
⚠️ **Bug: stale `parent layer api` syntax — `maven.api.v4` will silently
lose its parent**
This commit simplifies the parser so everything after `parent ` is taken as
the name verbatim. After the change, `parent layer api` on this line is parsed
as parent name `"layer api"` (not `"api"`). `applyRealmParents()` will then try
`layerClassLoaders.get("layer api")` and `configuredRealms.get("layer api")` —
both return `null` — and `maven.api.v4` ends up with **no parent classloader at
all**, silently. Only `plexus.core` was updated to the new syntax.
```suggestion
parent api
```
##########
impl/maven-classworlds/src/main/java/org/codehaus/plexus/classworlds/launcher/Configurator.java:
##########
@@ -319,28 +358,117 @@ private void createModuleLayer() {
}
}
- private void addModulePathsToClasspath() {
- if (curRealm != null) {
- for (Path path : modulePaths) {
+ private void applyRealmParents() {
+ for (Map.Entry<String, String> entry : realmParents.entrySet()) {
+ String realmId = entry.getKey();
+ String parentName = entry.getValue();
+ ClassRealm realm = configuredRealms.get(realmId);
+ if (realm == null) {
+ continue;
+ }
+ ClassLoader parentLoader = layerClassLoaders.get(parentName);
+ if (parentLoader != null) {
+ realm.setParentClassLoader(parentLoader);
+ } else {
+ ClassRealm parentRealm = configuredRealms.get(parentName);
+ if (parentRealm != null) {
+ realm.setParentRealm(parentRealm);
+ }
Review Comment:
**Silent failure when `parent` name resolves to neither a layer nor a realm**
When `parentName` is not found in either `layerClassLoaders` or
`configuredRealms`, the realm is left without a parent and execution continues
silently. A typo in `m2.conf` (or a user-defined config) produces an incorrect
classloader hierarchy with no diagnostic at all. Add a warning in the else
branch:
```suggestion
ClassRealm parentRealm = configuredRealms.get(parentName);
if (parentRealm != null) {
realm.setParentRealm(parentRealm);
} else {
LOGGER.log(System.Logger.Level.WARNING,
"classworlds: parent ''{0}'' not found (not a
layer, not a realm) for realm ''{1}'' — no parent set",
parentName, realmId);
}
```
##########
impl/maven-classworlds/src/main/java/org/codehaus/plexus/classworlds/launcher/Configurator.java:
##########
@@ -198,106 +185,158 @@ public void associateRealms() {
}
}
- public void addImportFrom(String relamName, String importSpec) throws
NoSuchRealmException {
- curRealm.importFrom(relamName, importSpec);
+ @Override
+ public void addLayer(String layerName) {
+ if (configuredRealms.containsKey(layerName)) {
+ throw new IllegalArgumentException("Name '" + layerName + "' is
already used by a realm");
+ }
+ curLayerName = layerName;
+ curRealm = null;
+ namedLayerPaths.computeIfAbsent(layerName, k -> new ArrayList<>());
}
Review Comment:
**Low: `addLayer()` guards against realm-name collision but not duplicate
layer names**
`namedLayerPaths.computeIfAbsent` silently merges entries when the same
layer name appears twice. A duplicate `[layer infra]` section in `m2.conf` is
accepted rather than rejected, unlike duplicate realm declarations which throw
`DuplicateRealmException`. Add an explicit guard for consistency:
```suggestion
if (namedLayerPaths.containsKey(layerName)) {
throw new IllegalArgumentException("Layer '" + layerName + "' is
already defined");
}
namedLayerPaths.put(layerName, new ArrayList<>());
```
--
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]