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