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