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