View Javadoc
1   package org.apache.maven.settings;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.execution.MavenExecutionRequest;
26  import org.apache.maven.properties.internal.SystemProperties;
27  import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
28  import org.apache.maven.settings.building.SettingsBuilder;
29  import org.apache.maven.settings.building.SettingsBuildingException;
30  import org.apache.maven.settings.building.SettingsBuildingRequest;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.annotations.Requirement;
33  import org.codehaus.plexus.logging.AbstractLogEnabled;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36  
37  /**
38   * @author jdcasey
39   */
40  @Component( role = MavenSettingsBuilder.class )
41  public class DefaultMavenSettingsBuilder
42      extends AbstractLogEnabled
43      implements MavenSettingsBuilder
44  {
45  
46      @Requirement
47      private SettingsBuilder settingsBuilder;
48  
49      public Settings buildSettings()
50          throws IOException, XmlPullParserException
51      {
52          File userSettingsFile =
53              getFile( "${user.home}/.m2/settings.xml", "user.home",
54                       MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION );
55  
56          return buildSettings( userSettingsFile );
57      }
58  
59      public Settings buildSettings( boolean useCachedSettings )
60          throws IOException, XmlPullParserException
61      {
62          return buildSettings();
63      }
64  
65      public Settings buildSettings( File userSettingsFile )
66          throws IOException, XmlPullParserException
67      {
68          File globalSettingsFile =
69              getFile( "${maven.conf}/settings.xml", "maven.conf",
70                       MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION );
71  
72          SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
73          request.setUserSettingsFile( userSettingsFile );
74          request.setGlobalSettingsFile( globalSettingsFile );
75          request.setSystemProperties( SystemProperties.getSystemProperties() );
76          return build( request );
77      }
78  
79      public Settings buildSettings( File userSettingsFile, boolean useCachedSettings )
80          throws IOException, XmlPullParserException
81      {
82          return buildSettings( userSettingsFile );
83      }
84  
85      private Settings build( SettingsBuildingRequest request )
86          throws IOException, XmlPullParserException
87      {
88          try
89          {
90              return settingsBuilder.build( request ).getEffectiveSettings();
91          }
92          catch ( SettingsBuildingException e )
93          {
94              throw (IOException) new IOException( e.getMessage() ).initCause( e );
95          }
96      }
97  
98      /** @since 2.1 */
99      public Settings buildSettings( MavenExecutionRequest request )
100         throws IOException, XmlPullParserException
101     {
102         SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
103         settingsRequest.setUserSettingsFile( request.getUserSettingsFile() );
104         settingsRequest.setGlobalSettingsFile( request.getGlobalSettingsFile() );
105         settingsRequest.setUserProperties( request.getUserProperties() );
106         settingsRequest.setSystemProperties( request.getSystemProperties() );
107 
108         return build( settingsRequest );
109     }
110 
111     private File getFile( String pathPattern, String basedirSysProp, String altLocationSysProp )
112     {
113         // -------------------------------------------------------------------------------------
114         // Alright, here's the justification for all the regexp wizardry below...
115         //
116         // Continuum and other server-like apps may need to locate the user-level and
117         // global-level settings somewhere other than ${user.home} and ${maven.home},
118         // respectively. Using a simple replacement of these patterns will allow them
119         // to specify the absolute path to these files in a customized components.xml
120         // file. Ideally, we'd do full pattern-evaluation against the sysprops, but this
121         // is a first step. There are several replacements below, in order to normalize
122         // the path character before we operate on the string as a regex input, and
123         // in order to avoid surprises with the File construction...
124         // -------------------------------------------------------------------------------------
125 
126         String path = System.getProperty( altLocationSysProp );
127 
128         if ( StringUtils.isEmpty( path ) )
129         {
130             // TODO This replacing shouldn't be necessary as user.home should be in the
131             // context of the container and thus the value would be interpolated by Plexus
132             String basedir = System.getProperty( basedirSysProp );
133             if ( basedir == null )
134             {
135                 basedir = System.getProperty( "user.dir" );
136             }
137 
138             basedir = basedir.replaceAll( "\\\\", "/" );
139             basedir = basedir.replaceAll( "\\$", "\\\\\\$" );
140 
141             path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir );
142             path = path.replaceAll( "\\\\", "/" );
143             // ---------------------------------------------------------------------------------
144             // I'm not sure if this last regexp was really intended to disallow the usage of
145             // network paths as user.home directory. Unfortunately it did. I removed it and
146             // have not detected any problems yet.
147             // ---------------------------------------------------------------------------------
148             // path = path.replaceAll( "//", "/" );
149 
150             return new File( path ).getAbsoluteFile();
151         }
152         else
153         {
154             return new File( path ).getAbsoluteFile();
155         }
156     }
157 
158 }