gnodet-bot commented on code in PR #14211:
URL: https://github.com/apache/maven/pull/14211#discussion_r4752576864


##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ExtensionActivationEvaluator.java:
##########
@@ -0,0 +1,203 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cling.invoker;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import org.apache.maven.api.settings.Activation;
+import org.apache.maven.api.settings.ActivationFile;
+import org.apache.maven.api.settings.ActivationOS;
+import org.apache.maven.api.settings.ActivationProperty;
+import org.apache.maven.api.settings.Extension;
+import org.apache.maven.impl.model.profile.JdkVersionProfileActivator;
+import org.apache.maven.utils.Os;
+
+/**
+ * Lightweight evaluator for extension activation conditions in settings.xml.
+ * Runs pre-DI, so it cannot use the injected profile activator services.
+ * Reuses the same matching logic as profile activation where possible.
+ * <p>
+ * <b>File activation:</b> relative paths in {@code <exists>} and {@code 
<missing>}
+ * resolve against the JVM working directory, not the Maven project directory.
+ * Unlike {@code FileProfileActivator}, which resolves against {@code 
${project.basedir}}
+ * via the model builder context, this evaluator has no basedir available at 
extension
+ * loading time.  Use absolute paths in extension file activation conditions.
+ */
+public final class ExtensionActivationEvaluator {
+
+    private ExtensionActivationEvaluator() {}
+
+    /**
+     * Returns {@code true} if the extension should be loaded given the 
current environment.
+     * An extension with no activation (or only {@code activeByDefault=true}) 
is always loaded.
+     */
+    public static boolean isActive(
+            Extension extension, Map<String, String> userProperties, 
Map<String, String> systemProperties) {
+        Activation activation = extension.activation();
+        if (activation == null) {
+            return true;
+        }
+
+        boolean hasConditions = false;
+
+        if (activation.jdk() != null && !activation.jdk().isEmpty()) {
+            hasConditions = true;
+            String javaVersion = systemProperties.getOrDefault("java.version", 
"");
+            if (javaVersion.isEmpty()
+                    || 
!JdkVersionProfileActivator.isJavaVersionCompatible(activation.jdk(), 
javaVersion)) {
+                return false;
+            }
+        }

Review Comment:
   ⚠️ **`NumberFormatException` can crash Maven startup on malformed `<jdk>` 
values**
   
   `JdkVersionProfileActivator.isJavaVersionCompatible()` calls 
`Integer.parseInt()` internally when parsing JDK version ranges. A malformed 
value like `<jdk>abc</jdk>` or `[17,` will throw `NumberFormatException` 
uncaught here, propagating up and aborting extension loading entirely — no 
user-friendly error, no degraded operation.
   
   The original `JdkVersionProfileActivator.isActive()` wraps the same call in 
try-catch precisely to handle this. The new evaluator needs the same guard.
   
   ```suggestion
           if (activation.jdk() != null && !activation.jdk().isEmpty()) {
               hasConditions = true;
               String javaVersion = 
systemProperties.getOrDefault("java.version", "");
               if (javaVersion.isEmpty()) {
                   return false;
               }
               try {
                   if 
(!JdkVersionProfileActivator.isJavaVersionCompatible(activation.jdk(), 
javaVersion)) {
                       return false;
                   }
               } catch (NumberFormatException e) {
                   // Malformed JDK activation value — treat as not matching
                   return false;
               }
           }
   ```



##########
api/maven-api-settings/src/main/mdo/settings.mdo:
##########
@@ -1211,6 +1211,20 @@
           <required>false</required>
           <type>DOM</type>
         </field>
+        <field>
+          <name>activation</name>
+          <description>Conditions that control whether this extension is 
loaded.
+          When omitted the extension is always loaded. Supports a subset of 
profile
+          activation types: property, jdk, os, and file. The {@code condition} 
type
+          is not supported here because it requires the DI container, which is 
not
+          available when extensions are loaded.</description>
+          <version>2.1.0+</version>

Review Comment:
   🔍 **MDO version `2.1.0+` doesn't align with existing versions in this file**
   
   The existing version markers in `settings.mdo` are `1.0.0`, `1.2.0`, 
`1.3.0`, and `2.0.0`. The `condition` field (also new) uses `2.0.0+`. 
Introducing `2.1.0+` here implies a new model version that doesn't appear to 
exist elsewhere in the Maven codebase — does this need a corresponding 
schema/model version bump, or should this be `2.0.0+` to match the other recent 
additions?



-- 
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