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      @Override
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      @Override
60      public Settings buildSettings(boolean useCachedSettings) throws IOException, XmlPullParserException {
61          return buildSettings();
62      }
63  
64      @Override
65      public Settings buildSettings(File userSettingsFile) throws IOException, XmlPullParserException {
66          File globalSettingsFile = getFile(
67                  "${maven.conf}/settings.xml", "maven.conf", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
68  
69          SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
70          request.setUserSettingsFile(userSettingsFile);
71          request.setGlobalSettingsFile(globalSettingsFile);
72          request.setSystemProperties(SystemProperties.getSystemProperties());
73          return build(request);
74      }
75  
76      @Override
77      public Settings buildSettings(File userSettingsFile, boolean useCachedSettings)
78              throws IOException, XmlPullParserException {
79          return buildSettings(userSettingsFile);
80      }
81  
82      private Settings build(SettingsBuildingRequest request) throws IOException, XmlPullParserException {
83          try {
84              return settingsBuilder.build(request).getEffectiveSettings();
85          } catch (SettingsBuildingException e) {
86              throw new IOException(e.getMessage(), e);
87          }
88      }
89  
90      /** @since 2.1 */
91      @Override
92      public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
93          SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
94          settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
95          settingsRequest.setProjectSettingsFile(request.getProjectSettingsFile());
96          settingsRequest.setGlobalSettingsFile(request.getGlobalSettingsFile());
97          settingsRequest.setUserProperties(request.getUserProperties());
98          settingsRequest.setSystemProperties(request.getSystemProperties());
99  
100         return build(settingsRequest);
101     }
102 
103     private File getFile(String pathPattern, String basedirSysProp, String altLocationSysProp) {
104         // -------------------------------------------------------------------------------------
105         // Alright, here's the justification for all the regexp wizardry below...
106         //
107         // Continuum and other server-like apps may need to locate the user-level and
108         // global-level settings somewhere other than ${user.home} and ${maven.home},
109         // respectively. Using a simple replacement of these patterns will allow them
110         // to specify the absolute path to these files in a customized components.xml
111         // file. Ideally, we'd do full pattern-evaluation against the sysprops, but this
112         // is a first step. There are several replacements below, in order to normalize
113         // the path character before we operate on the string as a regex input, and
114         // in order to avoid surprises with the File construction...
115         // -------------------------------------------------------------------------------------
116 
117         String path = System.getProperty(altLocationSysProp);
118 
119         if (path == null || path.isEmpty()) {
120             // TODO This replacing shouldn't be necessary as user.home should be in the
121             // context of the container and thus the value would be interpolated by Plexus
122             String basedir = System.getProperty(basedirSysProp);
123             if (basedir == null) {
124                 basedir = System.getProperty("user.dir");
125             }
126 
127             basedir = basedir.replace("\\", "/");
128             basedir = basedir.replace("$", "\\$");
129 
130             // basedirSysProp is non regexp and basedir too
131             path = pathPattern.replace("${" + basedirSysProp + "}", basedir);
132             path = path.replace("\\", "/");
133         }
134         return new File(path).getAbsoluteFile();
135     }
136 }