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