1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43
44
45 public class HelpUtil
46 {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 }