1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.properties.internal;
20
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import org.apache.maven.utils.Os;
26
27 /**
28 * Assists the project builder. <strong>Warning:</strong> This is an internal utility class that is only public for
29 * technical reasons, it is not part of the public API. In particular, this class can be changed or deleted without
30 * prior notice.
31 *
32 * @since 3.0
33 */
34 public class EnvironmentUtils {
35
36 private static Properties envVars;
37
38 /**
39 * Adds the environment variables in the form of properties whose keys are prefixed with {@code env.}, e.g. {@code
40 * env.PATH}. Unlike native environment variables, properties are always case-sensitive. For the sake of
41 * determinism, the environment variable names will be normalized to upper case on platforms with case-insensitive
42 * variable lookup.
43 *
44 * @param props The properties to add the environment variables to, may be {@code null}.
45 */
46 public static void addEnvVars(Properties props) {
47 if (props != null) {
48 if (envVars == null) {
49 Properties tmp = new Properties();
50 boolean caseSensitive = !Os.IS_WINDOWS;
51 for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
52 String key = "env."
53 + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH));
54 tmp.setProperty(key, entry.getValue());
55 }
56 envVars = tmp;
57 }
58
59 props.putAll(envVars);
60 }
61 }
62 }