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