View Javadoc

1   package org.apache.maven.plugins.help;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Displays the calculated settings as XML for this project, given any profile enhancement and the inheritance
42   * of the global settings into the user-level settings.
43   *
44   * @version $Id: EffectiveSettingsMojo.java 926395 2010-03-22 23:22:23Z brett $
45   * @since 2.0
46   * @goal effective-settings
47   * @requiresProject false
48   */
49  public class EffectiveSettingsMojo
50      extends AbstractEffectiveMojo
51  {
52      // ----------------------------------------------------------------------
53      // Mojo parameters
54      // ----------------------------------------------------------------------
55  
56      /**
57       * The system settings for Maven. This is the instance resulting from
58       * merging global and user-level settings files.
59       *
60       * @parameter expression="${settings}"
61       * @readonly
62       * @required
63       */
64      private Settings settings;
65  
66      /**
67       * For security reasons, all passwords are hidden by default. Set this to <code>true</code> to show all passwords.
68       *
69       * @since 2.1
70       * @parameter expression="${showPasswords}" default-value="false"
71       */
72      private boolean showPasswords;
73  
74      // ----------------------------------------------------------------------
75      // Public methods
76      // ----------------------------------------------------------------------
77  
78      /** {@inheritDoc} */
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     // Private methods
137     // ----------------------------------------------------------------------
138 
139     /**
140      * Hide proxy and server passwords.
141      *
142      * @param aSettings not null
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             // Password
160             if ( StringUtils.isNotEmpty( server.getPassword() ) )
161             {
162                 server.setPassword( "***" );
163             }
164             // Passphrase
165             if ( StringUtils.isNotEmpty( server.getPassphrase() ) )
166             {
167                 server.setPassphrase( "***" );
168             }
169         }
170     }
171 
172     /**
173      * TODO: should be replaced by SettingsUtils#copySettings() in 2.0.10+.
174      *
175      * @param settings could be null
176      * @return a new instance of settings or null if settings was null.
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      * Method for writing the effective settings informations.
204      *
205      * @param settings the settings, not null.
206      * @param writer the XML writer used, not null.
207      * @throws MojoExecutionException if any
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      * Apply some logic to clean the model before writing it.
236      *
237      * @param settings not null
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      * @return the current host name or <code>unknown</code> if error
253      * @see InetAddress#getLocalHost()
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      * @return the user name or <code>unknown</code> if <code>user.name</code> is not a system property.
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 }