Coverage Report - org.apache.maven.plugins.help.HelpUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
HelpUtil
0%
0/31
0%
0/16
22
 
 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.PrintWriter;
 23  
 import java.io.StringWriter;
 24  
 import java.lang.reflect.InvocationTargetException;
 25  
 import java.lang.reflect.Method;
 26  
 
 27  
 import org.apache.maven.BuildFailureException;
 28  
 import org.apache.maven.execution.MavenSession;
 29  
 import org.apache.maven.lifecycle.DefaultLifecycleExecutor;
 30  
 import org.apache.maven.lifecycle.LifecycleExecutionException;
 31  
 import org.apache.maven.lifecycle.LifecycleExecutor;
 32  
 import org.apache.maven.plugin.MojoExecutionException;
 33  
 import org.apache.maven.plugin.MojoFailureException;
 34  
 import org.apache.maven.plugin.PluginNotFoundException;
 35  
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 36  
 import org.apache.maven.project.MavenProject;
 37  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 38  
 
 39  
 /**
 40  
  * Utility methods to play with Help Mojos.
 41  
  *
 42  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 43  
  * @version $Id: HelpUtil.java 689770 2008-08-28 09:47:18Z vsiveton $
 44  
  */
 45  0
 public class HelpUtil
 46  
 {
 47  
     /**
 48  
      * Invoke the following private method <code>
 49  
      * DefaultLifecycleExecutor#getMojoDescriptor(String, MavenSession, MavenProject, String, boolean, boolean)</code>
 50  
      *
 51  
      * @param task not null
 52  
      * @param session not null
 53  
      * @param project not null
 54  
      * @param invokedVia not null
 55  
      * @param canUsePrefix not null
 56  
      * @param isOptionalMojo not null
 57  
      * @return MojoDescriptor for the task
 58  
      * @throws MojoFailureException if can not invoke the method.
 59  
      * @throws MojoExecutionException if no descriptor was found for <code>task</code>.
 60  
      * @see DefaultLifecycleExecutor#getMojoDescriptor(String, MavenSession, MavenProject, String, boolean, boolean)
 61  
      */
 62  
     protected static MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project,
 63  
                                               String invokedVia, boolean canUsePrefix, boolean isOptionalMojo )
 64  
         throws MojoFailureException, MojoExecutionException
 65  
     {
 66  
         try
 67  
         {
 68  0
             DefaultLifecycleExecutor lifecycleExecutor =
 69  
                 (DefaultLifecycleExecutor) session.lookup( LifecycleExecutor.ROLE );
 70  
 
 71  0
             Method m =
 72  
                 lifecycleExecutor.getClass().getDeclaredMethod(
 73  
                                                                 "getMojoDescriptor",
 74  0
                                                                 new Class[] { String.class, MavenSession.class,
 75  
                                                                     MavenProject.class, String.class,
 76  
                                                                     Boolean.TYPE, Boolean.TYPE } );
 77  0
             m.setAccessible( true );
 78  0
             MojoDescriptor mojoDescriptor =
 79  
                 (MojoDescriptor) m.invoke( lifecycleExecutor, new Object[] { task, session, project, invokedVia,
 80  
                     Boolean.valueOf( canUsePrefix ), Boolean.valueOf( isOptionalMojo ) } );
 81  
 
 82  0
             if ( mojoDescriptor == null )
 83  
             {
 84  0
                 throw new MojoExecutionException( "No MOJO exists for '" + task + "'." );
 85  
             }
 86  
 
 87  0
             return mojoDescriptor;
 88  
         }
 89  0
         catch ( SecurityException e )
 90  
         {
 91  0
             throw new MojoFailureException( "SecurityException: " + e.getMessage() );
 92  
         }
 93  0
         catch ( IllegalArgumentException e )
 94  
         {
 95  0
             throw new MojoFailureException( "IllegalArgumentException: " + e.getMessage() );
 96  
         }
 97  0
         catch ( ComponentLookupException e )
 98  
         {
 99  0
             throw new MojoFailureException( "ComponentLookupException: " + e.getMessage() );
 100  
         }
 101  0
         catch ( NoSuchMethodException e )
 102  
         {
 103  0
             throw new MojoFailureException( "NoSuchMethodException: " + e.getMessage() );
 104  
         }
 105  0
         catch ( IllegalAccessException e )
 106  
         {
 107  0
             throw new MojoFailureException( "IllegalAccessException: " + e.getMessage() );
 108  
         }
 109  0
         catch ( InvocationTargetException e )
 110  
         {
 111  0
             Throwable cause = e.getCause();
 112  
 
 113  0
             if ( cause instanceof BuildFailureException )
 114  
             {
 115  0
                 throw new MojoFailureException( "BuildFailureException: " + cause.getMessage() );
 116  
             }
 117  0
             else if ( cause instanceof LifecycleExecutionException )
 118  
             {
 119  0
                 throw new MojoFailureException( "LifecycleExecutionException: " + cause.getMessage() );
 120  
             }
 121  0
             else if ( cause instanceof PluginNotFoundException )
 122  
             {
 123  0
                 throw new MojoFailureException( "PluginNotFoundException: " + cause.getMessage() );
 124  
             }
 125  
 
 126  0
             StringWriter s = new StringWriter();
 127  0
             PrintWriter writer = new PrintWriter( s );
 128  0
             e.printStackTrace( writer );
 129  
 
 130  0
             throw new MojoFailureException( "InvocationTargetException: " + e.getMessage() + "\n" + s.toString() );
 131  
         }
 132  
     }
 133  
 }