/maven-help-plugin/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java

https://github.com/tobrien/maven-plugins · Java · 280 lines · 163 code · 34 blank · 83 comment · 14 complexity · 1820b091e74b45a094a3ba3cacbab25e MD5 · raw file

  1. package org.apache.maven.plugins.help;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.IOException;
  21. import java.io.StringWriter;
  22. import java.net.InetAddress;
  23. import java.net.UnknownHostException;
  24. import java.util.Iterator;
  25. import java.util.Properties;
  26. import org.apache.maven.plugin.MojoExecutionException;
  27. import org.apache.maven.settings.Profile;
  28. import org.apache.maven.settings.Proxy;
  29. import org.apache.maven.settings.Server;
  30. import org.apache.maven.settings.Settings;
  31. import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
  32. import org.codehaus.plexus.util.StringUtils;
  33. import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
  34. import org.codehaus.plexus.util.xml.XMLWriter;
  35. import org.codehaus.plexus.util.xml.XmlWriterUtil;
  36. /**
  37. * Displays the calculated settings as XML for this project, given any profile enhancement and the inheritance
  38. * of the global settings into the user-level settings.
  39. *
  40. * @version $Id$
  41. * @since 2.0
  42. * @goal effective-settings
  43. * @requiresProject false
  44. */
  45. public class EffectiveSettingsMojo
  46. extends AbstractEffectiveMojo
  47. {
  48. // ----------------------------------------------------------------------
  49. // Mojo parameters
  50. // ----------------------------------------------------------------------
  51. /**
  52. * The system settings for Maven. This is the instance resulting from
  53. * merging global and user-level settings files.
  54. *
  55. * @parameter expression="${settings}"
  56. * @readonly
  57. * @required
  58. */
  59. private Settings settings;
  60. /**
  61. * For security reasons, all passwords are hidden by default. Set this to <code>true</code> to show all passwords.
  62. *
  63. * @since 2.1
  64. * @parameter expression="${showPasswords}" default-value="false"
  65. */
  66. private boolean showPasswords;
  67. // ----------------------------------------------------------------------
  68. // Public methods
  69. // ----------------------------------------------------------------------
  70. /** {@inheritDoc} */
  71. public void execute()
  72. throws MojoExecutionException
  73. {
  74. Settings copySettings;
  75. if ( showPasswords )
  76. {
  77. copySettings = settings;
  78. }
  79. else
  80. {
  81. copySettings = copySettings( settings );
  82. hidePasswords( copySettings );
  83. }
  84. StringWriter w = new StringWriter();
  85. XMLWriter writer =
  86. new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
  87. copySettings.getModelEncoding(), null );
  88. writeHeader( writer );
  89. writeEffectiveSettings( copySettings, writer );
  90. String effectiveSettings = w.toString();
  91. if ( output != null )
  92. {
  93. try
  94. {
  95. writeXmlFile( output, effectiveSettings, copySettings.getModelEncoding() );
  96. }
  97. catch ( IOException e )
  98. {
  99. throw new MojoExecutionException( "Cannot write effective-settings to output: " + output, e );
  100. }
  101. if ( getLog().isInfoEnabled() )
  102. {
  103. getLog().info( "Effective-settings written to: " + output );
  104. }
  105. }
  106. else
  107. {
  108. StringBuffer message = new StringBuffer();
  109. message.append( "\nEffective user-specific configuration settings:\n\n" );
  110. message.append( effectiveSettings );
  111. message.append( "\n" );
  112. if ( getLog().isInfoEnabled() )
  113. {
  114. getLog().info( message.toString() );
  115. }
  116. }
  117. }
  118. // ----------------------------------------------------------------------
  119. // Private methods
  120. // ----------------------------------------------------------------------
  121. /**
  122. * Hide proxy and server passwords.
  123. *
  124. * @param aSettings not null
  125. */
  126. private static void hidePasswords( Settings aSettings )
  127. {
  128. for ( Iterator it = aSettings.getProxies().iterator(); it.hasNext(); )
  129. {
  130. Proxy proxy = (Proxy) it.next();
  131. if ( StringUtils.isNotEmpty( proxy.getPassword() ) )
  132. {
  133. proxy.setPassword( "***" );
  134. }
  135. }
  136. for ( Iterator it = aSettings.getServers().iterator(); it.hasNext(); )
  137. {
  138. Server server = (Server) it.next();
  139. // Password
  140. if ( StringUtils.isNotEmpty( server.getPassword() ) )
  141. {
  142. server.setPassword( "***" );
  143. }
  144. // Passphrase
  145. if ( StringUtils.isNotEmpty( server.getPassphrase() ) )
  146. {
  147. server.setPassphrase( "***" );
  148. }
  149. }
  150. }
  151. /**
  152. * TODO: should be replaced by SettingsUtils#copySettings() in 2.0.10+.
  153. *
  154. * @param settings could be null
  155. * @return a new instance of settings or null if settings was null.
  156. */
  157. private static Settings copySettings( Settings settings )
  158. {
  159. if ( settings == null )
  160. {
  161. return null;
  162. }
  163. Settings clone = new Settings();
  164. clone.setActiveProfiles( settings.getActiveProfiles() );
  165. clone.setInteractiveMode( settings.isInteractiveMode() );
  166. clone.setLocalRepository( settings.getLocalRepository() );
  167. clone.setMirrors( settings.getMirrors() );
  168. clone.setOffline( settings.isOffline() );
  169. clone.setPluginGroups( settings.getPluginGroups() );
  170. clone.setProfiles( settings.getProfiles() );
  171. clone.setProxies( settings.getProxies() );
  172. clone.setRuntimeInfo( settings.getRuntimeInfo() );
  173. clone.setServers( settings.getServers() );
  174. clone.setSourceLevel( settings.getSourceLevel() );
  175. clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
  176. return clone;
  177. }
  178. /**
  179. * Method for writing the effective settings informations.
  180. *
  181. * @param settings the settings, not null.
  182. * @param writer the XML writer used, not null.
  183. * @throws MojoExecutionException if any
  184. */
  185. private static void writeEffectiveSettings( Settings settings, XMLWriter writer )
  186. throws MojoExecutionException
  187. {
  188. cleanSettings( settings );
  189. String effectiveSettings;
  190. StringWriter sWriter = new StringWriter();
  191. SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
  192. try
  193. {
  194. settingsWriter.write( sWriter, settings );
  195. }
  196. catch ( IOException e )
  197. {
  198. throw new MojoExecutionException( "Cannot serialize Settings to XML.", e );
  199. }
  200. effectiveSettings = addMavenNamespace( sWriter.toString(), false );
  201. writeComment( writer, "Effective Settings for '" + getUserName() + "' on '" + getHostName() + "'" );
  202. writer.writeMarkup( effectiveSettings );
  203. }
  204. /**
  205. * Apply some logic to clean the model before writing it.
  206. *
  207. * @param settings not null
  208. */
  209. private static void cleanSettings( Settings settings )
  210. {
  211. for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
  212. {
  213. Profile profile = (Profile) it.next();
  214. Properties properties = new SortedProperties();
  215. properties.putAll( profile.getProperties() );
  216. profile.setProperties( properties );
  217. }
  218. }
  219. /**
  220. * @return the current host name or <code>unknown</code> if error
  221. * @see InetAddress#getLocalHost()
  222. */
  223. private static String getHostName()
  224. {
  225. try
  226. {
  227. return InetAddress.getLocalHost().getHostName();
  228. }
  229. catch ( UnknownHostException e )
  230. {
  231. return "unknown";
  232. }
  233. }
  234. /**
  235. * @return the user name or <code>unknown</code> if <code>user.name</code> is not a system property.
  236. */
  237. private static String getUserName()
  238. {
  239. String userName = System.getProperty( "user.name" );
  240. if ( StringUtils.isEmpty( userName ) )
  241. {
  242. return "unknown";
  243. }
  244. return userName;
  245. }
  246. }