diff --git a/maven-compat/src/main/java/org/apache/maven/profiles/activation/OperatingSystemProfileActivator.java b/maven-compat/src/main/java/org/apache/maven/profiles/activation/OperatingSystemProfileActivator.java index 3ec25d87b4..0a33cdc2f8 100644 --- a/maven-compat/src/main/java/org/apache/maven/profiles/activation/OperatingSystemProfileActivator.java +++ b/maven-compat/src/main/java/org/apache/maven/profiles/activation/OperatingSystemProfileActivator.java @@ -21,7 +21,7 @@ package org.apache.maven.profiles.activation; import org.apache.maven.model.Activation; import org.apache.maven.model.ActivationOS; import org.apache.maven.model.Profile; -import org.codehaus.plexus.util.Os; +import org.apache.maven.utils.Os; /** * OperatingSystemProfileActivator @@ -68,7 +68,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator { test = test.substring(1); } - boolean result = Os.isVersion(test); + boolean result = Os.OS_VERSION.equals(test); if (reverse) { return !result; @@ -86,7 +86,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator { test = test.substring(1); } - boolean result = Os.isArch(test); + boolean result = Os.OS_ARCH.equals(test); if (reverse) { return !result; @@ -104,7 +104,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator { test = test.substring(1); } - boolean result = Os.isName(test); + boolean result = Os.OS_NAME.equals(test); if (reverse) { return !result; diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 320a54a636..8615ac4b8d 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -62,7 +62,7 @@ import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.root.RootLocator; import org.apache.maven.repository.internal.ArtifactDescriptorUtils; import org.apache.maven.repository.internal.ModelCacheFactory; -import org.codehaus.plexus.util.Os; +import org.apache.maven.utils.Os; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.RequestTrace; @@ -516,7 +516,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { continue; } - if (Os.isFamily(Os.FAMILY_WINDOWS)) { + if (Os.IS_WINDOWS) { // we don't canonicalize on unix to avoid interfering with symlinks try { moduleFile = moduleFile.getCanonicalFile(); diff --git a/maven-core/src/main/java/org/apache/maven/properties/internal/EnvironmentUtils.java b/maven-core/src/main/java/org/apache/maven/properties/internal/EnvironmentUtils.java index dd77ebce85..027b0a016c 100644 --- a/maven-core/src/main/java/org/apache/maven/properties/internal/EnvironmentUtils.java +++ b/maven-core/src/main/java/org/apache/maven/properties/internal/EnvironmentUtils.java @@ -22,7 +22,7 @@ import java.util.Locale; import java.util.Map; import java.util.Properties; -import org.codehaus.plexus.util.Os; +import org.apache.maven.utils.Os; /** * Assists the project builder. Warning: This is an internal utility class that is only public for @@ -47,7 +47,7 @@ public class EnvironmentUtils { if (props != null) { if (envVars == null) { Properties tmp = new Properties(); - boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS); + boolean caseSensitive = !Os.IS_WINDOWS; for (Map.Entry entry : System.getenv().entrySet()) { String key = "env." + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH)); diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/java/JavaToolchainImpl.java b/maven-core/src/main/java/org/apache/maven/toolchain/java/JavaToolchainImpl.java index ba7de5a5aa..5a7b331757 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/java/JavaToolchainImpl.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/java/JavaToolchainImpl.java @@ -24,7 +24,7 @@ import java.nio.file.Paths; import org.apache.maven.toolchain.DefaultToolchain; import org.apache.maven.toolchain.model.ToolchainModel; -import org.codehaus.plexus.util.Os; +import org.apache.maven.utils.Os; import org.slf4j.Logger; /** @@ -64,7 +64,7 @@ public class JavaToolchainImpl extends DefaultToolchain implements JavaToolchain private static Path findTool(String toolName, Path installDir) { Path bin = installDir.resolve("bin"); // NOI18N if (Files.isDirectory(bin)) { - if (Os.isFamily("windows")) { // NOI18N + if (Os.IS_WINDOWS) { Path tool = bin.resolve(toolName + ".exe"); if (Files.exists(tool)) { return tool; diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java index 904278d49a..5fd319871d 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java @@ -26,7 +26,7 @@ import java.util.Locale; import java.util.Properties; import org.apache.maven.cli.jansi.MessageUtils; -import org.codehaus.plexus.util.Os; +import org.apache.maven.utils.Os; import org.slf4j.Logger; /** 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 fa9c6c2639..8f0af6aa12 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 @@ -26,7 +26,7 @@ import org.apache.maven.model.ActivationOS; import org.apache.maven.model.Profile; import org.apache.maven.model.building.ModelProblemCollector; import org.apache.maven.model.profile.ProfileActivationContext; -import org.codehaus.plexus.util.Os; +import org.apache.maven.utils.Os; /** * Determines profile activation based on the operating system of the current runtime platform. @@ -95,7 +95,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator { test = test.substring(1); } - boolean result = Os.isVersion(test); + boolean result = Os.OS_VERSION.equals(test); return reverse != result; } @@ -109,7 +109,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator { test = test.substring(1); } - boolean result = Os.isArch(test); + boolean result = Os.OS_ARCH.equals(test); return reverse != result; } @@ -123,7 +123,7 @@ public class OperatingSystemProfileActivator implements ProfileActivator { test = test.substring(1); } - boolean result = Os.isName(test); + boolean result = Os.OS_NAME.equals(test); return reverse != result; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java b/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java new file mode 100644 index 0000000000..7f647374e6 --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/utils/Os.java @@ -0,0 +1,215 @@ +/* + * 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.utils; + +import java.util.Locale; +import java.util.stream.Stream; + +/** + * OS support + */ +public class Os { + + /** + * The OS Name. + */ + public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); + + /** + * The OA architecture. + */ + public static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH); + + /** + * The OS version. + */ + public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ENGLISH); + + /** + * OS Family + */ + public static final String OS_FAMILY; + + /** + * Boolean indicating if the running OS is a Windows system. + */ + public static final boolean IS_WINDOWS; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_WINDOWS = "windows"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_WIN9X = "win9x"; + + /** + * OS family that can be tested for. {@value} + */ + public static final String FAMILY_NT = "winnt"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_OS2 = "os/2"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_NETWARE = "netware"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_DOS = "dos"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_MAC = "mac"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_TANDEM = "tandem"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_UNIX = "unix"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_OPENVMS = "openvms"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_ZOS = "z/os"; + + /** + * OS family that can be tested for. {@value} + */ + private static final String FAMILY_OS400 = "os/400"; + + /** + * OpenJDK is reported to call MacOS X "Darwin" + * + * @see bugzilla issue + * @see HADOOP-3318 + */ + private static final String DARWIN = "darwin"; + + /** + * The path separator. + */ + private static final String PATH_SEP = System.getProperty("path.separator"); + + static { + // Those two public constants are initialized here, as they need all the private constants + // above to be initialized first, but the code style imposes the public constants to be + // defined above the private ones... + OS_FAMILY = getOsFamily(); + IS_WINDOWS = isFamily(FAMILY_WINDOWS); + } + + private Os() {} + + /** + * Determines if the OS on which Maven is executing matches the + * given OS family. + * + * @param family the family to check for + * @return true if the OS matches + * + */ + public static boolean isFamily(String family) { + // windows probing logic relies on the word 'windows' in the OS + boolean isWindows = OS_NAME.contains(FAMILY_WINDOWS); + boolean is9x = false; + boolean isNT = false; + if (isWindows) { + // there are only four 9x platforms that we look for + is9x = (OS_NAME.contains("95") + || OS_NAME.contains("98") + || OS_NAME.contains("me") + // wince isn't really 9x, but crippled enough to + // be a muchness. Maven doesnt run on CE, anyway. + || OS_NAME.contains("ce")); + isNT = !is9x; + } + switch (family) { + case FAMILY_WINDOWS: + return isWindows; + case FAMILY_WIN9X: + return isWindows && is9x; + case FAMILY_NT: + return isWindows && isNT; + case FAMILY_OS2: + return OS_NAME.contains(FAMILY_OS2); + case FAMILY_NETWARE: + return OS_NAME.contains(FAMILY_NETWARE); + case FAMILY_DOS: + return PATH_SEP.equals(";") && !isFamily(FAMILY_NETWARE); + case FAMILY_MAC: + return OS_NAME.contains(FAMILY_MAC) || OS_NAME.contains(DARWIN); + case FAMILY_TANDEM: + return OS_NAME.contains("nonstop_kernel"); + case FAMILY_UNIX: + return PATH_SEP.equals(":") + && !isFamily(FAMILY_OPENVMS) + && (!isFamily(FAMILY_MAC) || OS_NAME.endsWith("x") || OS_NAME.contains(DARWIN)); + case FAMILY_ZOS: + return OS_NAME.contains(FAMILY_ZOS) || OS_NAME.contains("os/390"); + case FAMILY_OS400: + return OS_NAME.contains(FAMILY_OS400); + case FAMILY_OPENVMS: + return OS_NAME.contains(FAMILY_OPENVMS); + default: + return OS_NAME.contains(family.toLowerCase(Locale.US)); + } + } + + /** + * Helper method to determine the current OS family. + * + * @return name of current OS family. + */ + private static String getOsFamily() { + return Stream.of( + FAMILY_DOS, + FAMILY_MAC, + FAMILY_NETWARE, + FAMILY_NT, + FAMILY_OPENVMS, + FAMILY_OS2, + FAMILY_OS400, + FAMILY_TANDEM, + FAMILY_UNIX, + FAMILY_WIN9X, + FAMILY_WINDOWS, + FAMILY_ZOS) + .filter(Os::isFamily) + .findFirst() + .orElse(null); + } +}