1 package org.apache.maven.settings;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
123
124
125
126
127
128
129
130
131
132
133
134 String path = System.getProperty( altLocationSysProp );
135
136 if ( StringUtils.isEmpty( path ) )
137 {
138
139
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
153
154
155
156
157
158 }
159 return new File( path ).getAbsoluteFile();
160 }
161
162 }