Coverage Report - org.apache.maven.plugin.coreit.DumpAuthMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
DumpAuthMojo
0 %
0/35
0 %
0/16
12
 
 1  
 package org.apache.maven.plugin.coreit;
 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 org.apache.maven.artifact.manager.WagonManager;
 23  
 import org.apache.maven.plugin.AbstractMojo;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.MojoFailureException;
 26  
 import org.apache.maven.wagon.authentication.AuthenticationInfo;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileOutputStream;
 30  
 import java.io.IOException;
 31  
 import java.io.OutputStream;
 32  
 import java.util.Properties;
 33  
 
 34  
 /**
 35  
  * Dumps the authentication info registered with the wagon manager for a server to a properties file.
 36  
  * 
 37  
  * @goal dump-auth
 38  
  * @phase validate
 39  
  * 
 40  
  * @author Benjamin Bentmann
 41  
  * @version $Id: DumpAuthMojo.java 730994 2009-01-03 14:46:12Z bentmann $
 42  
  */
 43  0
 public class DumpAuthMojo
 44  
     extends AbstractMojo
 45  
 {
 46  
 
 47  
     /**
 48  
      * Project base directory used for manual path alignment.
 49  
      * 
 50  
      * @parameter default-value="${basedir}"
 51  
      * @readonly
 52  
      */
 53  
     private File basedir;
 54  
 
 55  
     /**
 56  
      * The Wagon manager used to retrieve authentication infos.
 57  
      * 
 58  
      * @component
 59  
      */
 60  
     private WagonManager wagonManager;
 61  
 
 62  
     /**
 63  
      * The path to the properties file used to dump the auth infos.
 64  
      * 
 65  
      * @parameter expression="${wagon.propertiesFile}"
 66  
      */
 67  
     private File propertiesFile;
 68  
 
 69  
     /**
 70  
      * The set of server identifiers whose auth infos should be dumped.
 71  
      * 
 72  
      * @parameter
 73  
      */
 74  
     private String[] serverIds;
 75  
 
 76  
     /**
 77  
      * Runs this mojo.
 78  
      * 
 79  
      * @throws MojoFailureException If the output file could not be created.
 80  
      */
 81  
     public void execute()
 82  
         throws MojoExecutionException, MojoFailureException
 83  
     {
 84  0
         Properties authProperties = new Properties();
 85  
 
 86  0
         for ( int i = 0; i < serverIds.length; i++ )
 87  
         {
 88  0
             String serverId = serverIds[i];
 89  0
             getLog().info( "[MAVEN-CORE-IT-LOG] Getting authentication info for server " + serverId );
 90  
 
 91  0
             AuthenticationInfo authInfo = wagonManager.getAuthenticationInfo( serverId );
 92  0
             if ( authInfo != null )
 93  
             {
 94  0
                 if ( authInfo.getUserName() != null )
 95  
                 {
 96  0
                     authProperties.setProperty( serverId + ".username", authInfo.getUserName() );
 97  
                 }
 98  0
                 if ( authInfo.getPassword() != null )
 99  
                 {
 100  0
                     authProperties.setProperty( serverId + ".password", authInfo.getPassword() );
 101  
                 }
 102  0
                 if ( authInfo.getPrivateKey() != null )
 103  
                 {
 104  0
                     authProperties.setProperty( serverId + ".privateKey", authInfo.getPrivateKey() );
 105  
                 }
 106  0
                 if ( authInfo.getPassphrase() != null )
 107  
                 {
 108  0
                     authProperties.setProperty( serverId + ".passphrase", authInfo.getPassphrase() );
 109  
                 }
 110  
 
 111  0
                 getLog().info( "[MAVEN-CORE-IT-LOG]   username = " + authInfo.getUserName() );
 112  0
                 getLog().info( "[MAVEN-CORE-IT-LOG]   password = " + authInfo.getPassword() );
 113  0
                 getLog().info( "[MAVEN-CORE-IT-LOG]   private key = " + authInfo.getPrivateKey() );
 114  0
                 getLog().info( "[MAVEN-CORE-IT-LOG]   passphrase = " + authInfo.getPassphrase() );
 115  
             }
 116  
             else
 117  
             {
 118  0
                 getLog().info( "[MAVEN-CORE-IT-LOG]   (no authentication info available)" );
 119  
             }
 120  
         }
 121  
 
 122  0
         if ( !propertiesFile.isAbsolute() )
 123  
         {
 124  0
             propertiesFile = new File( basedir, propertiesFile.getPath() );
 125  
         }
 126  
 
 127  0
         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + propertiesFile );
 128  
 
 129  0
         OutputStream out = null;
 130  
         try
 131  
         {
 132  0
             propertiesFile.getParentFile().mkdirs();
 133  0
             out = new FileOutputStream( propertiesFile );
 134  0
             authProperties.store( out, "MAVEN-CORE-IT-LOG" );
 135  
         }
 136  0
         catch ( IOException e )
 137  
         {
 138  0
             throw new MojoExecutionException( "Output file could not be created: " + propertiesFile, e );
 139  
         }
 140  
         finally
 141  
         {
 142  0
             if ( out != null )
 143  
             {
 144  
                 try
 145  
                 {
 146  0
                     out.close();
 147  
                 }
 148  0
                 catch ( IOException e )
 149  
                 {
 150  
                     // just ignore
 151  0
                 }
 152  
             }
 153  
         }
 154  
 
 155  0
         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + propertiesFile );
 156  0
     }
 157  
 
 158  
 }