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