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 | 0 | 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 | 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 | |
} |