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 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 if ( showPasswords )
84 {
85 copySettings = settings;
86 }
87 else
88 {
89 copySettings = copySettings( settings );
90 hidePasswords( copySettings );
91 }
92
93 StringWriter w = new StringWriter();
94 XMLWriter writer =
95 new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
96 copySettings.getModelEncoding(), null );
97
98 writeHeader( writer );
99
100 writeEffectiveSettings( copySettings, writer );
101
102 String effectiveSettings = w.toString();
103
104 if ( output != null )
105 {
106 try
107 {
108 writeXmlFile( output, effectiveSettings, copySettings.getModelEncoding() );
109 }
110 catch ( IOException e )
111 {
112 throw new MojoExecutionException( "Cannot write effective-settings to output: " + output, e );
113 }
114
115 if ( getLog().isInfoEnabled() )
116 {
117 getLog().info( "Effective-settings written to: " + output );
118 }
119 }
120 else
121 {
122 StringBuffer message = new StringBuffer();
123
124 message.append( "\nEffective user-specific configuration settings:\n\n" );
125 message.append( effectiveSettings );
126 message.append( "\n" );
127
128 if ( getLog().isInfoEnabled() )
129 {
130 getLog().info( message.toString() );
131 }
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144 private static void hidePasswords( Settings aSettings )
145 {
146 for ( Iterator it = aSettings.getProxies().iterator(); it.hasNext(); )
147 {
148 Proxy proxy = (Proxy) it.next();
149
150 if ( StringUtils.isNotEmpty( proxy.getPassword() ) )
151 {
152 proxy.setPassword( "***" );
153 }
154 }
155
156 for ( Iterator it = aSettings.getServers().iterator(); it.hasNext(); )
157 {
158 Server server = (Server) it.next();
159
160 if ( StringUtils.isNotEmpty( server.getPassword() ) )
161 {
162 server.setPassword( "***" );
163 }
164
165 if ( StringUtils.isNotEmpty( server.getPassphrase() ) )
166 {
167 server.setPassphrase( "***" );
168 }
169 }
170 }
171
172
173
174
175
176
177
178 private static Settings copySettings( Settings settings )
179 {
180 if ( settings == null )
181 {
182 return null;
183 }
184
185 Settings clone = new Settings();
186 clone.setActiveProfiles( settings.getActiveProfiles() );
187 clone.setInteractiveMode( settings.isInteractiveMode() );
188 clone.setLocalRepository( settings.getLocalRepository() );
189 clone.setMirrors( settings.getMirrors() );
190 clone.setOffline( settings.isOffline() );
191 clone.setPluginGroups( settings.getPluginGroups() );
192 clone.setProfiles( settings.getProfiles() );
193 clone.setProxies( settings.getProxies() );
194 clone.setRuntimeInfo( settings.getRuntimeInfo() );
195 clone.setServers( settings.getServers() );
196 clone.setSourceLevel( settings.getSourceLevel() );
197 clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
198
199 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 cleanSettings( settings );
213
214 String effectiveSettings;
215
216 StringWriter sWriter = new StringWriter();
217 SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
218 try
219 {
220 settingsWriter.write( sWriter, settings );
221 }
222 catch ( IOException e )
223 {
224 throw new MojoExecutionException( "Cannot serialize Settings to XML.", e );
225 }
226
227 effectiveSettings = addMavenNamespace( sWriter.toString(), false );
228
229 writeComment( writer, "Effective Settings for '" + getUserName() + "' on '" + getHostName() + "'" );
230
231 writer.writeMarkup( effectiveSettings );
232 }
233
234
235
236
237
238
239 private static void cleanSettings( Settings settings )
240 {
241 for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
242 {
243 Profile profile = (Profile) it.next();
244
245 Properties properties = new SortedProperties();
246 properties.putAll( profile.getProperties() );
247 profile.setProperties( properties );
248 }
249 }
250
251
252
253
254
255 private static String getHostName()
256 {
257 try
258 {
259 return InetAddress.getLocalHost().getHostName();
260 }
261 catch ( UnknownHostException e )
262 {
263 return "unknown";
264 }
265 }
266
267
268
269
270 private static String getUserName()
271 {
272 String userName = System.getProperty( "user.name" );
273 if ( StringUtils.isEmpty( userName ) )
274 {
275 return "unknown";
276 }
277
278 return userName;
279 }
280 }