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.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  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              DefaultLifecycleExecutor lifecycleExecutor =
69                  (DefaultLifecycleExecutor) session.lookup( LifecycleExecutor.ROLE );
70  
71              Method m =
72                  lifecycleExecutor.getClass().getDeclaredMethod(
73                                                                  "getMojoDescriptor",
74                                                                  new Class[] { String.class, MavenSession.class,
75                                                                      MavenProject.class, String.class,
76                                                                      Boolean.TYPE, Boolean.TYPE } );
77              m.setAccessible( true );
78              MojoDescriptor mojoDescriptor =
79                  (MojoDescriptor) m.invoke( lifecycleExecutor, new Object[] { task, session, project, invokedVia,
80                      Boolean.valueOf( canUsePrefix ), Boolean.valueOf( isOptionalMojo ) } );
81  
82              if ( mojoDescriptor == null )
83              {
84                  throw new MojoExecutionException( "No MOJO exists for '" + task + "'." );
85              }
86  
87              return mojoDescriptor;
88          }
89          catch ( SecurityException e )
90          {
91              throw new MojoFailureException( "SecurityException: " + e.getMessage() );
92          }
93          catch ( IllegalArgumentException e )
94          {
95              throw new MojoFailureException( "IllegalArgumentException: " + e.getMessage() );
96          }
97          catch ( ComponentLookupException e )
98          {
99              throw new MojoFailureException( "ComponentLookupException: " + e.getMessage() );
100         }
101         catch ( NoSuchMethodException e )
102         {
103             throw new MojoFailureException( "NoSuchMethodException: " + e.getMessage() );
104         }
105         catch ( IllegalAccessException e )
106         {
107             throw new MojoFailureException( "IllegalAccessException: " + e.getMessage() );
108         }
109         catch ( InvocationTargetException e )
110         {
111             Throwable cause = e.getCause();
112 
113             if ( cause instanceof BuildFailureException )
114             {
115                 throw new MojoFailureException( "BuildFailureException: " + cause.getMessage() );
116             }
117             else if ( cause instanceof LifecycleExecutionException )
118             {
119                 throw new MojoFailureException( "LifecycleExecutionException: " + cause.getMessage() );
120             }
121             else if ( cause instanceof PluginNotFoundException )
122             {
123                 throw new MojoFailureException( "PluginNotFoundException: " + cause.getMessage() );
124             }
125 
126             StringWriter s = new StringWriter();
127             PrintWriter writer = new PrintWriter( s );
128             e.printStackTrace( writer );
129 
130             throw new MojoFailureException( "InvocationTargetException: " + e.getMessage() + "\n" + s.toString() );
131         }
132     }
133 }