Copilot commented on code in PR #14211: URL: https://github.com/apache/maven/pull/14211#discussion_r4756204287
########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ExtensionActivationEvaluator.java: ########## @@ -0,0 +1,209 @@ +/* + * 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()) { + return false; + } + try { + if (!JdkVersionProfileActivator.isJavaVersionCompatible(activation.jdk(), javaVersion)) { + return false; + } + } catch (NumberFormatException e) { + return false; + } + } + + if (activation.os() != null) { + hasConditions = true; + if (!isOsActive(activation.os(), systemProperties)) { + return false; + } + } + + if (activation.property() != null) { + hasConditions = true; + if (!isPropertyActive(activation.property(), userProperties, systemProperties)) { + return false; + } + } + + if (activation.file() != null) { + hasConditions = true; + if (!isFileActive(activation.file())) { + return false; + } + } + + if (!hasConditions) { + return activation.activeByDefault(); + } + + return true; + } + + private static boolean isPropertyActive( + ActivationProperty property, Map<String, String> userProperties, Map<String, String> systemProperties) { + String name = property.name(); + if (name == null || name.isEmpty()) { + return false; + } + + boolean negate = false; + if (name.startsWith("!")) { + negate = true; + name = name.substring(1); + } + + String actual = userProperties.get(name); + if (actual == null) { + actual = systemProperties.get(name); + } + + String expected = property.value(); + if (expected != null && !expected.isEmpty()) { + boolean negateValue = false; + if (expected.startsWith("!")) { + negateValue = true; + expected = expected.substring(1); + } + return negateValue != expected.equals(actual); Review Comment: This string comparison drops the existing `maven.version` range semantics implemented by `PropertyProfileActivator`. For example, a property condition with name `maven.version` and value `[4.0.0,5.0.0)` never activates on Maven 4.1.0; negating that range instead activates even on versions inside it. Please share or reproduce the profile activator's Maven-version range handling so extensions can be gated on compatible Maven versions. ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ExtensionActivationEvaluator.java: ########## @@ -0,0 +1,209 @@ +/* + * 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()) { + return false; + } + try { + if (!JdkVersionProfileActivator.isJavaVersionCompatible(activation.jdk(), javaVersion)) { + return false; + } + } catch (NumberFormatException e) { + return false; + } + } + + if (activation.os() != null) { + hasConditions = true; + if (!isOsActive(activation.os(), systemProperties)) { + return false; + } + } + + if (activation.property() != null) { + hasConditions = true; + if (!isPropertyActive(activation.property(), userProperties, systemProperties)) { + return false; + } + } + + if (activation.file() != null) { + hasConditions = true; + if (!isFileActive(activation.file())) { + return false; + } + } + + if (!hasConditions) { + return activation.activeByDefault(); + } + + return true; + } + + private static boolean isPropertyActive( + ActivationProperty property, Map<String, String> userProperties, Map<String, String> systemProperties) { + String name = property.name(); + if (name == null || name.isEmpty()) { + return false; + } + + boolean negate = false; + if (name.startsWith("!")) { + negate = true; + name = name.substring(1); + } + + String actual = userProperties.get(name); + if (actual == null) { + actual = systemProperties.get(name); + } + + String expected = property.value(); + if (expected != null && !expected.isEmpty()) { + boolean negateValue = false; + if (expected.startsWith("!")) { + negateValue = true; + expected = expected.substring(1); + } + return negateValue != expected.equals(actual); + } + return negate != (actual != null && !actual.isEmpty()); + } + + private static boolean isOsActive(ActivationOS os, Map<String, String> systemProperties) { + if (os.family() == null && os.name() == null && os.arch() == null && os.version() == null) { + return false; + } + + String actualName = systemProperties.getOrDefault("os.name", Os.OS_NAME).toLowerCase(Locale.ENGLISH); + String actualArch = systemProperties.getOrDefault("os.arch", Os.OS_ARCH).toLowerCase(Locale.ENGLISH); + String actualVersion = + systemProperties.getOrDefault("os.version", Os.OS_VERSION).toLowerCase(Locale.ENGLISH); + + if (os.family() != null) { + String family = os.family().toLowerCase(Locale.ENGLISH); + boolean negate = family.startsWith("!"); + if (negate) { + family = family.substring(1); + } + if (negate == Os.isFamily(family, actualName)) { + return false; + } + } + if (os.name() != null) { + String name = os.name().toLowerCase(Locale.ENGLISH); + boolean negate = name.startsWith("!"); + if (negate) { + name = name.substring(1); + } + if (negate == actualName.equals(name)) { + return false; + } + } + if (os.arch() != null) { + String arch = os.arch().toLowerCase(Locale.ENGLISH); + boolean negate = arch.startsWith("!"); + if (negate) { + arch = arch.substring(1); + } + if (negate == actualArch.equals(arch)) { + return false; + } + } + if (os.version() != null) { + String version = os.version().toLowerCase(Locale.ENGLISH); + boolean negate = version.startsWith("!"); + if (negate) { + version = version.substring(1); + } + boolean result; + if (version.startsWith("regex:")) { + result = Pattern.compile(os.version().substring(negate ? 7 : 6), Pattern.CASE_INSENSITIVE) + .matcher(actualVersion) + .matches(); + } else { + result = actualVersion.equals(version); + } + if (negate == result) { + return false; + } + } + return true; + } + + private static boolean isFileActive(ActivationFile file) { + String exists = file.exists(); + String missing = file.missing(); + boolean hasExists = exists != null && !exists.isEmpty(); + boolean hasMissing = missing != null && !missing.isEmpty(); + if (hasExists) { + return Files.exists(Path.of(exists)); + } else if (hasMissing) { + return !Files.exists(Path.of(missing)); Review Comment: File activation paths are checked without interpolation, and `BaseParser.readSettingsEarly()` leaves `${...}` references untouched. Consequently, `<exists>${user.home}/.m2/marker</exists>` stays inactive even when the file exists, while the corresponding `<missing>` condition activates incorrectly. Pass the available user/system properties into the file evaluator and interpolate both paths before checking existence, with regression tests for both conditions. -- 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]
