From a3db035907bd09eff658faa8142b524151346f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Tue, 9 Jul 2024 19:04:43 +0200 Subject: [PATCH 1/2] [MNG-8178] Fall back to System.getProperty for missing context props A call to context.getSystemProperties() may yield empty an empty map, or one missing the desired key, which makes a subsequent call of toLowerCase fail with a NullPointerException. Fall-back to using System.getProperty (with an empty default, in case that one fails too). --- .../OperatingSystemProfileActivator.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java index 2553735cb452..1eeb4c3b8739 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java @@ -22,6 +22,7 @@ import javax.inject.Singleton; import java.util.Locale; +import java.util.Map; import org.apache.maven.model.Activation; import org.apache.maven.model.ActivationOS; @@ -41,6 +42,14 @@ public class OperatingSystemProfileActivator implements ProfileActivator { private static final String REGEX_PREFIX = "regex:"; + private static String systemProperty(Map map, String key) { + String v = map.get(key); + if (v == null) { + v = System.getProperty(key, ""); + } + return v; + } + @Override public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) { Activation activation = profile.getActivation(); @@ -57,9 +66,12 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model boolean active = ensureAtLeastOneNonNull(os); - String actualOsName = context.getSystemProperties().get("os.name").toLowerCase(Locale.ENGLISH); - String actualOsArch = context.getSystemProperties().get("os.arch").toLowerCase(Locale.ENGLISH); - String actualOsVersion = context.getSystemProperties().get("os.version").toLowerCase(Locale.ENGLISH); + String actualOsName = + systemProperty(context.getSystemProperties(), "os.name").toLowerCase(Locale.ENGLISH); + String actualOsArch = + systemProperty(context.getSystemProperties(), "os.arch").toLowerCase(Locale.ENGLISH); + String actualOsVersion = + systemProperty(context.getSystemProperties(), "os.version").toLowerCase(Locale.ENGLISH); if (active && os.getFamily() != null) { active = determineFamilyMatch(os.getFamily(), actualOsName); From 8ce5fad42c4ed868fe1912aed847edfedd7c1957 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 10 Jul 2024 16:34:28 +0200 Subject: [PATCH 2/2] Use Os class to grab values instead --- .../OperatingSystemProfileActivator.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java index 1eeb4c3b8739..e9d08aaf9261 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/profile/activation/OperatingSystemProfileActivator.java @@ -22,7 +22,6 @@ import javax.inject.Singleton; import java.util.Locale; -import java.util.Map; import org.apache.maven.model.Activation; import org.apache.maven.model.ActivationOS; @@ -42,14 +41,6 @@ public class OperatingSystemProfileActivator implements ProfileActivator { private static final String REGEX_PREFIX = "regex:"; - private static String systemProperty(Map map, String key) { - String v = map.get(key); - if (v == null) { - v = System.getProperty(key, ""); - } - return v; - } - @Override public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) { Activation activation = profile.getActivation(); @@ -66,12 +57,15 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model boolean active = ensureAtLeastOneNonNull(os); - String actualOsName = - systemProperty(context.getSystemProperties(), "os.name").toLowerCase(Locale.ENGLISH); - String actualOsArch = - systemProperty(context.getSystemProperties(), "os.arch").toLowerCase(Locale.ENGLISH); - String actualOsVersion = - systemProperty(context.getSystemProperties(), "os.version").toLowerCase(Locale.ENGLISH); + String actualOsName = context.getSystemProperties() + .getOrDefault("os.name", Os.OS_NAME) + .toLowerCase(Locale.ENGLISH); + String actualOsArch = context.getSystemProperties() + .getOrDefault("os.arch", Os.OS_ARCH) + .toLowerCase(Locale.ENGLISH); + String actualOsVersion = context.getSystemProperties() + .getOrDefault("os.version", Os.OS_VERSION) + .toLowerCase(Locale.ENGLISH); if (active && os.getFamily() != null) { active = determineFamilyMatch(os.getFamily(), actualOsName);