1 package org.apache.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.PrintStream;
24 import java.util.List;
25
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.execution.MojoExecutionEvent;
28 import org.apache.maven.execution.MojoExecutionListener;
29 import org.apache.maven.execution.scope.internal.MojoExecutionScope;
30 import org.apache.maven.model.Plugin;
31 import org.apache.maven.plugin.descriptor.MojoDescriptor;
32 import org.apache.maven.plugin.descriptor.PluginDescriptor;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.classworlds.realm.ClassRealm;
35 import org.codehaus.plexus.component.annotations.Component;
36 import org.codehaus.plexus.component.annotations.Requirement;
37 import org.eclipse.aether.RepositorySystemSession;
38 import org.eclipse.aether.repository.RemoteRepository;
39
40
41
42
43 @Component( role = BuildPluginManager.class )
44 public class DefaultBuildPluginManager
45 implements BuildPluginManager
46 {
47
48 @Requirement
49 private MavenPluginManager mavenPluginManager;
50
51 @Requirement
52 private LegacySupport legacySupport;
53
54 @Requirement
55 private MojoExecutionScope scope;
56
57 private MojoExecutionListener mojoExecutionListener;
58
59
60 @Requirement( role = MojoExecutionListener.class )
61 private List<MojoExecutionListener> mojoExecutionListeners;
62
63 public void setMojoExecutionListeners( final List<MojoExecutionListener> listeners )
64 {
65 this.mojoExecutionListeners = listeners;
66 this.mojoExecutionListener = new CompoundMojoExecutionListener( listeners );
67 }
68
69
70
71
72
73
74
75
76
77
78 public PluginDescriptor loadPlugin( Plugin plugin, List<RemoteRepository> repositories,
79 RepositorySystemSession session )
80 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
81 InvalidPluginDescriptorException
82 {
83 return mavenPluginManager.getPluginDescriptor( plugin, repositories, session );
84 }
85
86
87
88
89
90 public void executeMojo( MavenSession session, MojoExecution mojoExecution )
91 throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException
92 {
93 MavenProject project = session.getCurrentProject();
94
95 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
96
97 Mojo mojo = null;
98
99 ClassRealm pluginRealm;
100 try
101 {
102 pluginRealm = getPluginRealm( session, mojoDescriptor.getPluginDescriptor() );
103 }
104 catch ( PluginResolutionException e )
105 {
106 throw new PluginExecutionException( mojoExecution, project, e );
107 }
108
109 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
110 Thread.currentThread().setContextClassLoader( pluginRealm );
111
112 MavenSession oldSession = legacySupport.getSession();
113
114 scope.enter();
115
116 try
117 {
118 scope.seed( MavenProject.class, project );
119 scope.seed( MojoExecution.class, mojoExecution );
120
121 mojo = mavenPluginManager.getConfiguredMojo( Mojo.class, session, mojoExecution );
122
123 legacySupport.setSession( session );
124
125
126
127
128 try
129 {
130 MojoExecutionEvent mojoExecutionEvent = new MojoExecutionEvent( session, project, mojoExecution, mojo );
131
132 mojoExecutionListener.beforeMojoExecution( mojoExecutionEvent );
133
134 mojo.execute();
135
136 mojoExecutionListener.afterMojoExecutionSuccess( mojoExecutionEvent );
137 }
138 catch ( ClassCastException e )
139 {
140
141 throw e;
142 }
143 catch ( RuntimeException e )
144 {
145 throw new PluginExecutionException( mojoExecution, project, e );
146 }
147 }
148 catch ( PluginContainerException e )
149 {
150 mojoExecutionListener.afterExecutionFailure( new MojoExecutionEvent( session, project, mojoExecution, mojo,
151 e ) );
152
153 throw new PluginExecutionException( mojoExecution, project, e );
154 }
155 catch ( NoClassDefFoundError e )
156 {
157 mojoExecutionListener.afterExecutionFailure( new MojoExecutionEvent( session, project, mojoExecution, mojo,
158 e ) );
159
160 ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
161 PrintStream ps = new PrintStream( os );
162 ps.println( "A required class was missing while executing " + mojoDescriptor.getId() + ": "
163 + e.getMessage() );
164 pluginRealm.display( ps );
165
166 Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
167
168 throw new PluginExecutionException( mojoExecution, project, wrapper );
169 }
170 catch ( LinkageError e )
171 {
172 mojoExecutionListener.afterExecutionFailure( new MojoExecutionEvent( session, project, mojoExecution, mojo,
173 e ) );
174
175 ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
176 PrintStream ps = new PrintStream( os );
177 ps.println( "An API incompatibility was encountered while executing " + mojoDescriptor.getId() + ": "
178 + e.getClass().getName() + ": " + e.getMessage() );
179 pluginRealm.display( ps );
180
181 Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
182
183 throw new PluginExecutionException( mojoExecution, project, wrapper );
184 }
185 catch ( ClassCastException e )
186 {
187 mojoExecutionListener.afterExecutionFailure( new MojoExecutionEvent( session, project, mojoExecution, mojo,
188 e ) );
189
190 ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
191 PrintStream ps = new PrintStream( os );
192 ps.println( "A type incompatibility occurred while executing " + mojoDescriptor.getId() + ": "
193 + e.getMessage() );
194 pluginRealm.display( ps );
195
196 throw new PluginExecutionException( mojoExecution, project, os.toString(), e );
197 }
198 catch ( RuntimeException e )
199 {
200 mojoExecutionListener.afterExecutionFailure( new MojoExecutionEvent( session, project, mojoExecution, mojo,
201 e ) );
202
203 throw e;
204 }
205 finally
206 {
207 mavenPluginManager.releaseMojo( mojo, mojoExecution );
208
209 scope.exit();
210
211 Thread.currentThread().setContextClassLoader( oldClassLoader );
212
213 legacySupport.setSession( oldSession );
214 }
215 }
216
217
218
219
220
221
222 public ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginDescriptor )
223 throws PluginResolutionException, PluginManagerException
224 {
225 ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
226 if ( pluginRealm != null )
227 {
228 return pluginRealm;
229 }
230
231 mavenPluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
232
233 return pluginDescriptor.getClassRealm();
234 }
235
236 public MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, List<RemoteRepository> repositories,
237 RepositorySystemSession session )
238 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
239 MojoNotFoundException, InvalidPluginDescriptorException
240 {
241 return mavenPluginManager.getMojoDescriptor( plugin, goal, repositories, session );
242 }
243
244 }