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