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