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 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
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.home}/conf/settings.xml", "maven.home",
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
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
115
116
117
118
119
120
121
122
123
124
125
126 String path = System.getProperty( altLocationSysProp );
127
128 if ( StringUtils.isEmpty( path ) )
129 {
130
131
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
145
146
147
148
149
150 return new File( path ).getAbsoluteFile();
151 }
152 else
153 {
154 return new File( path ).getAbsoluteFile();
155 }
156 }
157
158 }