gnodet-bot commented on code in PR #14211: URL: https://github.com/apache/maven/pull/14211#discussion_r4751968123
########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ExtensionActivationEvaluator.java: ########## @@ -0,0 +1,191 @@ +/* + * 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 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. + */ +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; + } + } + + 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); + } + if (negate == actualVersion.equals(version)) { + return false; + } + } + return true; + } + + private static boolean isFileActive(ActivationFile file) { + String exists = file.exists(); Review Comment: ⚠️ **`isFileActive` applies AND semantics when both `exists` and `missing` are set — this diverges from `FileProfileActivator`.** When both fields are non-empty, the current code evaluates both independently (the file must exist AND another must be absent). `FileProfileActivator` treats `exists` as taking priority when both are present (ignoring `missing` with a warning). Since the MDO describes this as reusing profile activation semantics, the behavior should match: ```suggestion 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) { // exists takes priority (consistent with FileProfileActivator) return Files.exists(Path.of(exists)); } else if (hasMissing) { return !Files.exists(Path.of(missing)); } return false; } ``` ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ExtensionActivationEvaluator.java: ########## @@ -0,0 +1,191 @@ +/* + * 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 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. + */ +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; + } + } + + 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); Review Comment: 💡 **`isOsActive` does not support the `regex:` prefix for OS version matching.** `OperatingSystemProfileActivator.determineVersionMatch()` accepts `regex:`-prefixed patterns (e.g. `<os><version>regex:5\..*</version></os>`). Without this, a user who relies on regex version matching in profile activation will find the same syntax silently broken for extension activation. Consider supporting `regex:` for version: ```suggestion 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 = java.util.regex.Pattern.compile( os.version().substring(negate ? 7 : 6), java.util.regex.Pattern.CASE_INSENSITIVE) .matcher(actualVersion) .matches(); } else { result = actualVersion.equals(version); } if (negate == result) { return false; } } ``` ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/ExtensionActivationEvaluator.java: ########## @@ -0,0 +1,191 @@ +/* + * 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 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. + */ +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; + } + } + + 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); + } + if (negate == actualVersion.equals(version)) { + return false; + } + } + return true; + } + + private static boolean isFileActive(ActivationFile file) { + String exists = file.exists(); + if (exists != null && !exists.isEmpty()) { + if (!Files.exists(Path.of(exists))) { + return false; Review Comment: ℹ️ **Relative file paths resolve against the JVM working directory, not the Maven project directory.** `Path.of(exists)` / `Path.of(missing)` resolves relative paths against the JVM CWD. `FileProfileActivator` resolves against `${project.basedir}` via `context.exists(path, false)` with interpolation. Since this evaluator runs pre-DI without basedir context, relative paths will silently behave differently than their profile activation counterparts. Consider documenting this limitation in the class-level Javadoc so users know to use absolute paths in extension activation file 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]
