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.xml.pull.XmlPullParserException;
36  
37  /**
38   */
39  @Deprecated
40  @Named
41  @Singleton
42  public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder {
43  
44      private final SettingsBuilder settingsBuilder;
45  
46      @Inject
47      public DefaultMavenSettingsBuilder(SettingsBuilder settingsBuilder) {
48          this.settingsBuilder = settingsBuilder;
49      }
50  
51      public Settings buildSettings() throws IOException, XmlPullParserException {
52          File userSettingsFile = getFile(
53                  "${user.home}/.m2/settings.xml", "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION);
54  
55          return buildSettings(userSettingsFile);
56      }
57  
58      public Settings buildSettings(boolean useCachedSettings) throws IOException, XmlPullParserException {
59          return buildSettings();
60      }
61  
62      public Settings buildSettings(File userSettingsFile) throws IOException, XmlPullParserException {
63          File globalSettingsFile = getFile(
64                  "${maven.conf}/settings.xml", "maven.conf", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
65  
66          SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
67          request.setUserSettingsFile(userSettingsFile);
68          request.setGlobalSettingsFile(globalSettingsFile);
69          request.setSystemProperties(SystemProperties.getSystemProperties());
70          return build(request);
71      }
72  
73      public Settings buildSettings(File userSettingsFile, boolean useCachedSettings)
74              throws IOException, XmlPullParserException {
75          return buildSettings(userSettingsFile);
76      }
77  
78      private Settings build(SettingsBuildingRequest request) throws IOException, XmlPullParserException {
79          try {
80              return settingsBuilder.build(request).getEffectiveSettings();
81          } catch (SettingsBuildingException e) {
82              throw new IOException(e.getMessage(), e);
83          }
84      }
85  
86      /** @since 2.1 */
87      public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
88          SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
89          settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
90          settingsRequest.setProjectSettingsFile(request.getProjectSettingsFile());
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 (path == null || path.isEmpty()) {
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.replace("\\", "/");
123             basedir = basedir.replace("$", "\\$");
124 
125             // basedirSysProp is non regexp and basedir too
126             path = pathPattern.replace("${" + basedirSysProp + "}", basedir);
127             path = path.replace("\\", "/");
128             // ---------------------------------------------------------------------------------
129             // I'm not sure if this last regexp was really intended to disallow the usage of
130             // network paths as user.home directory. Unfortunately it did. I removed it and
131             // have not detected any problems yet.
132             // ---------------------------------------------------------------------------------
133             // path = path.replaceAll( "//", "/" );
134 
135         }
136         return new File(path).getAbsoluteFile();
137     }
138 }