From 04983c01116802d21b73ace88506db48803df625 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Thu, 21 Jan 2010 13:38:42 +0000 Subject: [PATCH] o Refactored code git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@901701 13f79535-47bb-0310-9956-ffa450edef68 --- .../project/artifact/MavenMetadataSource.java | 19 +---- .../properties/internal/EnvironmentUtils.java | 69 +++++++++++++++++++ .../java/org/apache/maven/cli/MavenCli.java | 12 +--- 3 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/properties/internal/EnvironmentUtils.java diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index 757dec220e..ddd438c4a9 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -24,7 +24,6 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -62,6 +61,7 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.properties.internal.EnvironmentUtils; import org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest; import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.codehaus.plexus.PlexusContainer; @@ -69,7 +69,6 @@ import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.util.Os; /** * @author Jason van Zyl @@ -100,8 +99,6 @@ public class MavenMetadataSource @Requirement private LegacySupport legacySupport; - private static Properties envVars; - private void injectSession( MetadataResolutionRequest request ) { MavenSession session = legacySupport.getSession(); @@ -700,19 +697,7 @@ public class MavenMetadataSource { Properties props = new Properties(); - if ( envVars == null ) - { - Properties tmp = new Properties(); - boolean caseSensitive = !Os.isFamily( Os.FAMILY_WINDOWS ); - for ( Map.Entry entry : System.getenv().entrySet() ) - { - String key = "env." + ( caseSensitive ? entry.getKey() : entry.getKey().toUpperCase( Locale.ENGLISH ) ); - tmp.setProperty( key, entry.getValue() ); - } - envVars = tmp; - } - - props.putAll( envVars ); + EnvironmentUtils.addEnvVars( props ); props.putAll( System.getProperties() ); 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 new file mode 100644 index 0000000000..0c525b4689 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/properties/internal/EnvironmentUtils.java @@ -0,0 +1,69 @@ +package org.apache.maven.properties.internal; + +/* + * 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. + */ + +import java.util.Locale; +import java.util.Map; +import java.util.Properties; + +import org.codehaus.plexus.util.Os; + +/** + * Assists the project builder. Warning: This is an internal utility class that is only public for + * technical reasons, it is not part of the public API. In particular, this class can be changed or deleted without + * prior notice. + * + * @author Benjamin Bentmann + */ +public class EnvironmentUtils +{ + + private static Properties envVars; + + /** + * Adds the environment variables in the form of properties whose keys are prefixed with {@code env.}, e.g. {@code + * env.PATH}. Unlike native environment variables, properties are always case-sensitive. For the sake of + * determinism, the environment variable names will be normalized to upper case on platforms with case-insensitive + * variable lookup. + * + * @param props The properties to add the environment variables to, may be {@code null}. + */ + public static void addEnvVars( Properties props ) + { + if ( props != null ) + { + if ( envVars == null ) + { + Properties tmp = new Properties(); + boolean caseSensitive = !Os.isFamily( Os.FAMILY_WINDOWS ); + for ( Map.Entry entry : System.getenv().entrySet() ) + { + String key = + "env." + ( caseSensitive ? entry.getKey() : entry.getKey().toUpperCase( Locale.ENGLISH ) ); + tmp.setProperty( key, entry.getValue() ); + } + envVars = tmp; + } + + props.putAll( envVars ); + } + } + +} diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 0f26efc990..a177d70f9f 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; @@ -40,6 +39,7 @@ import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.lifecycle.LifecycleExecutionException; import org.apache.maven.model.building.ModelProcessor; import org.apache.maven.project.MavenProject; +import org.apache.maven.properties.internal.EnvironmentUtils; import org.apache.maven.repository.ArtifactTransferListener; import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; import org.apache.maven.settings.building.SettingsBuilder; @@ -52,7 +52,6 @@ import org.codehaus.plexus.DefaultPlexusContainer; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.util.Os; import org.codehaus.plexus.util.StringUtils; import org.sonatype.plexus.components.cipher.DefaultPlexusCipher; import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher; @@ -898,14 +897,7 @@ public class MavenCli static void populateProperties( CommandLine commandLine, Properties systemProperties, Properties userProperties ) { - // add the env vars to the property set, with the "env." prefix - // XXX support for env vars should probably be removed from the ModelInterpolator - boolean caseSensitive = !Os.isFamily( Os.FAMILY_WINDOWS ); - for ( Map.Entry entry : System.getenv().entrySet() ) - { - String key = "env." + ( caseSensitive ? entry.getKey() : entry.getKey().toUpperCase( Locale.ENGLISH ) ); - systemProperties.setProperty( key, entry.getValue() ); - } + EnvironmentUtils.addEnvVars( systemProperties ); // ---------------------------------------------------------------------- // Options that are set on the command line become system properties