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  import javax.inject.Inject;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  import org.apache.maven.execution.MavenExecutionRequest;
27  import org.apache.maven.properties.internal.SystemProperties;
28  import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
29  import org.apache.maven.settings.building.SettingsBuilder;
30  import org.apache.maven.settings.building.SettingsBuildingException;
31  import org.apache.maven.settings.building.SettingsBuildingRequest;
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  @Named
40  @Singleton
41  public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder {
42  
43      private final SettingsBuilder settingsBuilder;
44  
45      @Inject
46      public DefaultMavenSettingsBuilder(SettingsBuilder settingsBuilder) {
47          this.settingsBuilder = settingsBuilder;
48      }
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 new IOException(e.getMessage(), 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.replaceAll("\\\\", "/");
121             basedir = basedir.replaceAll("\\$", "\\\\\\$");
122 
123             path = pathPattern.replaceAll("\\$\\{" + basedirSysProp + "\\}", basedir);
124             path = path.replaceAll("\\\\", "/");
125             // ---------------------------------------------------------------------------------
126             // I'm not sure if this last regexp was really intended to disallow the usage of
127             // network paths as user.home directory. Unfortunately it did. I removed it and
128             // have not detected any problems yet.
129             // ---------------------------------------------------------------------------------
130             // path = path.replaceAll( "//", "/" );
131 
132         }
133         return new File(path).getAbsoluteFile();
134     }
135 }