View Javadoc
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.settings;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.execution.MavenExecutionRequest;
25  import org.apache.maven.properties.internal.SystemProperties;
26  import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
27  import org.apache.maven.settings.building.SettingsBuilder;
28  import org.apache.maven.settings.building.SettingsBuildingException;
29  import org.apache.maven.settings.building.SettingsBuildingRequest;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  import org.codehaus.plexus.logging.AbstractLogEnabled;
33  import org.codehaus.plexus.util.StringUtils;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * @author jdcasey
38   */
39  @Component(role = MavenSettingsBuilder.class)
40  public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder {
41  
42      @Requirement
43      private SettingsBuilder settingsBuilder;
44  
45      public Settings buildSettings() throws IOException, XmlPullParserException {
46          File userSettingsFile = getFile(
47                  "${user.home}/.m2/settings.xml", "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION);
48  
49          return buildSettings(userSettingsFile);
50      }
51  
52      public Settings buildSettings(boolean useCachedSettings) throws IOException, XmlPullParserException {
53          return buildSettings();
54      }
55  
56      public Settings buildSettings(File userSettingsFile) throws IOException, XmlPullParserException {
57          File globalSettingsFile = getFile(
58                  "${maven.conf}/settings.xml", "maven.conf", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
59  
60          SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
61          request.setUserSettingsFile(userSettingsFile);
62          request.setGlobalSettingsFile(globalSettingsFile);
63          request.setSystemProperties(SystemProperties.getSystemProperties());
64          return build(request);
65      }
66  
67      public Settings buildSettings(File userSettingsFile, boolean useCachedSettings)
68              throws IOException, XmlPullParserException {
69          return buildSettings(userSettingsFile);
70      }
71  
72      private Settings build(SettingsBuildingRequest request) throws IOException, XmlPullParserException {
73          try {
74              return settingsBuilder.build(request).getEffectiveSettings();
75          } catch (SettingsBuildingException e) {
76              throw (IOException) new IOException(e.getMessage()).initCause(e);
77          }
78      }
79  
80      /** @since 2.1 */
81      public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
82          SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
83          settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
84          settingsRequest.setGlobalSettingsFile(request.getGlobalSettingsFile());
85          settingsRequest.setUserProperties(request.getUserProperties());
86          settingsRequest.setSystemProperties(request.getSystemProperties());
87  
88          return build(settingsRequest);
89      }
90  
91      private File getFile(String pathPattern, String basedirSysProp, String altLocationSysProp) {
92          // -------------------------------------------------------------------------------------
93          // Alright, here's the justification for all the regexp wizardry below...
94          //
95          // Continuum and other server-like apps may need to locate the user-level and
96          // global-level settings somewhere other than ${user.home} and ${maven.home},
97          // respectively. Using a simple replacement of these patterns will allow them
98          // to specify the absolute path to these files in a customized components.xml
99          // file. Ideally, we'd do full pattern-evaluation against the sysprops, but this
100         // is a first step. There are several replacements below, in order to normalize
101         // the path character before we operate on the string as a regex input, and
102         // in order to avoid surprises with the File construction...
103         // -------------------------------------------------------------------------------------
104 
105         String path = System.getProperty(altLocationSysProp);
106 
107         if (StringUtils.isEmpty(path)) {
108             // TODO This replacing shouldn't be necessary as user.home should be in the
109             // context of the container and thus the value would be interpolated by Plexus
110             String basedir = System.getProperty(basedirSysProp);
111             if (basedir == null) {
112                 basedir = System.getProperty("user.dir");
113             }
114 
115             basedir = basedir.replace("\\", "/");
116             basedir = basedir.replace("$", "\\$");
117 
118             // basedirSysProp is non regexp and basedir too
119             path = pathPattern.replace("${" + basedirSysProp + "}", basedir);
120             path = path.replace("\\", "/");
121             // ---------------------------------------------------------------------------------
122             // I'm not sure if this last regexp was really intended to disallow the usage of
123             // network paths as user.home directory. Unfortunately it did. I removed it and
124             // have not detected any problems yet.
125             // ---------------------------------------------------------------------------------
126             // path = path.replaceAll( "//", "/" );
127 
128         }
129         return new File(path).getAbsoluteFile();
130     }
131 }