Coverage Report - org.apache.maven.plugins.help.EffectiveSettingsMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
EffectiveSettingsMojo
0%
0/78
0%
0/24
3.857
 
 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  0
 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  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  
     // 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  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  
             // Password
 160  0
             if ( StringUtils.isNotEmpty( server.getPassword() ) )
 161  
             {
 162  0
                 server.setPassword( "***" );
 163  
             }
 164  
             // Passphrase
 165  0
             if ( StringUtils.isNotEmpty( server.getPassphrase() ) )
 166  
             {
 167  0
                 server.setPassphrase( "***" );
 168  
             }
 169  0
         }
 170  0
     }
 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  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  
      * 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  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  
      * 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  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  
      * @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  0
             return InetAddress.getLocalHost().getHostName();
 260  
         }
 261  0
         catch ( UnknownHostException e )
 262  
         {
 263  0
             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  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  
 }