1 | |
package org.apache.maven.plugins.help; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.io.StringWriter; |
24 | |
import java.net.InetAddress; |
25 | |
import java.net.UnknownHostException; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.Properties; |
28 | |
|
29 | |
import org.apache.maven.plugin.MojoExecutionException; |
30 | |
import org.apache.maven.settings.Profile; |
31 | |
import org.apache.maven.settings.Proxy; |
32 | |
import org.apache.maven.settings.Server; |
33 | |
import org.apache.maven.settings.Settings; |
34 | |
import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer; |
35 | |
import org.codehaus.plexus.util.StringUtils; |
36 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
37 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
38 | |
import org.codehaus.plexus.util.xml.XmlWriterUtil; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class EffectiveSettingsMojo |
50 | |
extends AbstractEffectiveMojo |
51 | |
{ |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private Settings settings; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private boolean showPasswords; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public void execute() |
80 | |
throws MojoExecutionException |
81 | |
{ |
82 | |
Settings copySettings; |
83 | 0 | if ( showPasswords ) |
84 | |
{ |
85 | 0 | copySettings = settings; |
86 | |
} |
87 | |
else |
88 | |
{ |
89 | 0 | copySettings = copySettings( settings ); |
90 | 0 | hidePasswords( copySettings ); |
91 | |
} |
92 | |
|
93 | 0 | StringWriter w = new StringWriter(); |
94 | 0 | XMLWriter writer = |
95 | |
new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ), |
96 | |
copySettings.getModelEncoding(), null ); |
97 | |
|
98 | 0 | writeHeader( writer ); |
99 | |
|
100 | 0 | writeEffectiveSettings( copySettings, writer ); |
101 | |
|
102 | 0 | String effectiveSettings = w.toString(); |
103 | |
|
104 | 0 | if ( output != null ) |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | 0 | writeXmlFile( output, effectiveSettings, copySettings.getModelEncoding() ); |
109 | |
} |
110 | 0 | catch ( IOException e ) |
111 | |
{ |
112 | 0 | throw new MojoExecutionException( "Cannot write effective-settings to output: " + output, e ); |
113 | 0 | } |
114 | |
|
115 | 0 | if ( getLog().isInfoEnabled() ) |
116 | |
{ |
117 | 0 | getLog().info( "Effective-settings written to: " + output ); |
118 | |
} |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 0 | StringBuffer message = new StringBuffer(); |
123 | |
|
124 | 0 | message.append( "\nEffective user-specific configuration settings:\n\n" ); |
125 | 0 | message.append( effectiveSettings ); |
126 | 0 | message.append( "\n" ); |
127 | |
|
128 | 0 | if ( getLog().isInfoEnabled() ) |
129 | |
{ |
130 | 0 | getLog().info( message.toString() ); |
131 | |
} |
132 | |
} |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
private static void hidePasswords( Settings aSettings ) |
145 | |
{ |
146 | 0 | for ( Iterator it = aSettings.getProxies().iterator(); it.hasNext(); ) |
147 | |
{ |
148 | 0 | Proxy proxy = (Proxy) it.next(); |
149 | |
|
150 | 0 | if ( StringUtils.isNotEmpty( proxy.getPassword() ) ) |
151 | |
{ |
152 | 0 | proxy.setPassword( "***" ); |
153 | |
} |
154 | 0 | } |
155 | |
|
156 | 0 | for ( Iterator it = aSettings.getServers().iterator(); it.hasNext(); ) |
157 | |
{ |
158 | 0 | Server server = (Server) it.next(); |
159 | |
|
160 | 0 | if ( StringUtils.isNotEmpty( server.getPassword() ) ) |
161 | |
{ |
162 | 0 | server.setPassword( "***" ); |
163 | |
} |
164 | |
|
165 | 0 | if ( StringUtils.isNotEmpty( server.getPassphrase() ) ) |
166 | |
{ |
167 | 0 | server.setPassphrase( "***" ); |
168 | |
} |
169 | 0 | } |
170 | 0 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
private static Settings copySettings( Settings settings ) |
179 | |
{ |
180 | 0 | if ( settings == null ) |
181 | |
{ |
182 | 0 | return null; |
183 | |
} |
184 | |
|
185 | 0 | Settings clone = new Settings(); |
186 | 0 | clone.setActiveProfiles( settings.getActiveProfiles() ); |
187 | 0 | clone.setInteractiveMode( settings.isInteractiveMode() ); |
188 | 0 | clone.setLocalRepository( settings.getLocalRepository() ); |
189 | 0 | clone.setMirrors( settings.getMirrors() ); |
190 | 0 | clone.setOffline( settings.isOffline() ); |
191 | 0 | clone.setPluginGroups( settings.getPluginGroups() ); |
192 | 0 | clone.setProfiles( settings.getProfiles() ); |
193 | 0 | clone.setProxies( settings.getProxies() ); |
194 | 0 | clone.setRuntimeInfo( settings.getRuntimeInfo() ); |
195 | 0 | clone.setServers( settings.getServers() ); |
196 | 0 | clone.setSourceLevel( settings.getSourceLevel() ); |
197 | 0 | clone.setUsePluginRegistry( settings.isUsePluginRegistry() ); |
198 | |
|
199 | 0 | return clone; |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
private static void writeEffectiveSettings( Settings settings, XMLWriter writer ) |
210 | |
throws MojoExecutionException |
211 | |
{ |
212 | 0 | cleanSettings( settings ); |
213 | |
|
214 | |
String effectiveSettings; |
215 | |
|
216 | 0 | StringWriter sWriter = new StringWriter(); |
217 | 0 | SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer(); |
218 | |
try |
219 | |
{ |
220 | 0 | settingsWriter.write( sWriter, settings ); |
221 | |
} |
222 | 0 | catch ( IOException e ) |
223 | |
{ |
224 | 0 | throw new MojoExecutionException( "Cannot serialize Settings to XML.", e ); |
225 | 0 | } |
226 | |
|
227 | 0 | effectiveSettings = addMavenNamespace( sWriter.toString(), false ); |
228 | |
|
229 | 0 | writeComment( writer, "Effective Settings for '" + getUserName() + "' on '" + getHostName() + "'" ); |
230 | |
|
231 | 0 | writer.writeMarkup( effectiveSettings ); |
232 | 0 | } |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
private static void cleanSettings( Settings settings ) |
240 | |
{ |
241 | 0 | for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); ) |
242 | |
{ |
243 | 0 | Profile profile = (Profile) it.next(); |
244 | |
|
245 | 0 | Properties properties = new SortedProperties(); |
246 | 0 | properties.putAll( profile.getProperties() ); |
247 | 0 | profile.setProperties( properties ); |
248 | 0 | } |
249 | 0 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
private static String getHostName() |
256 | |
{ |
257 | |
try |
258 | |
{ |
259 | 0 | return InetAddress.getLocalHost().getHostName(); |
260 | |
} |
261 | 0 | catch ( UnknownHostException e ) |
262 | |
{ |
263 | 0 | return "unknown"; |
264 | |
} |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
private static String getUserName() |
271 | |
{ |
272 | 0 | String userName = System.getProperty( "user.name" ); |
273 | 0 | if ( StringUtils.isEmpty( userName ) ) |
274 | |
{ |
275 | 0 | return "unknown"; |
276 | |
} |
277 | |
|
278 | 0 | return userName; |
279 | |
} |
280 | |
} |