1 package org.apache.maven.plugin.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
26 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
27 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.plugin.InvalidPluginDescriptorException;
31 import org.apache.maven.plugin.InvalidPluginException;
32 import org.apache.maven.plugin.LegacySupport;
33 import org.apache.maven.plugin.MavenPluginManager;
34 import org.apache.maven.plugin.MojoExecution;
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugin.MojoFailureException;
37 import org.apache.maven.plugin.PluginConfigurationException;
38 import org.apache.maven.plugin.PluginDescriptorParsingException;
39 import org.apache.maven.plugin.PluginManager;
40 import org.apache.maven.plugin.PluginManagerException;
41 import org.apache.maven.plugin.PluginNotFoundException;
42 import org.apache.maven.plugin.PluginResolutionException;
43 import org.apache.maven.plugin.descriptor.PluginDescriptor;
44 import org.apache.maven.plugin.prefix.DefaultPluginPrefixRequest;
45 import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
46 import org.apache.maven.plugin.prefix.PluginPrefixRequest;
47 import org.apache.maven.plugin.prefix.PluginPrefixResolver;
48 import org.apache.maven.plugin.prefix.PluginPrefixResult;
49 import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
50 import org.apache.maven.plugin.version.PluginVersionNotFoundException;
51 import org.apache.maven.plugin.version.PluginVersionRequest;
52 import org.apache.maven.plugin.version.PluginVersionResolutionException;
53 import org.apache.maven.plugin.version.PluginVersionResolver;
54 import org.apache.maven.project.MavenProject;
55 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
56 import org.apache.maven.settings.Settings;
57 import org.codehaus.plexus.PlexusContainer;
58 import org.codehaus.plexus.component.annotations.Component;
59 import org.codehaus.plexus.component.annotations.Requirement;
60 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
61
62
63
64
65 @Component( role = PluginManager.class )
66 public class DefaultPluginManager
67 implements PluginManager
68 {
69
70 @Requirement
71 private PlexusContainer container;
72
73 @Requirement
74 private MavenPluginManager pluginManager;
75
76 @Requirement
77 private PluginVersionResolver pluginVersionResolver;
78
79 @Requirement
80 private PluginPrefixResolver pluginPrefixResolver;
81
82 @Requirement
83 private LegacySupport legacySupport;
84
85 public void executeMojo( MavenProject project, MojoExecution execution, MavenSession session )
86 throws MojoExecutionException, ArtifactResolutionException, MojoFailureException, ArtifactNotFoundException,
87 InvalidDependencyVersionException, PluginManagerException, PluginConfigurationException
88 {
89 throw new UnsupportedOperationException();
90 }
91
92 public Object getPluginComponent( Plugin plugin, String role, String roleHint )
93 throws PluginManagerException, ComponentLookupException
94 {
95 MavenSession session = legacySupport.getSession();
96
97 PluginDescriptor pluginDescriptor;
98 try
99 {
100 pluginDescriptor =
101 pluginManager.getPluginDescriptor( plugin, session.getCurrentProject().getRemotePluginRepositories(),
102 session.getRepositorySession() );
103
104 pluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
105 }
106 catch ( Exception e )
107 {
108 throw new PluginManagerException( plugin, e.getMessage(), e );
109 }
110
111 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
112 try
113 {
114 Thread.currentThread().setContextClassLoader( pluginDescriptor.getClassRealm() );
115
116 return container.lookup( role, roleHint );
117 }
118 finally
119 {
120 Thread.currentThread().setContextClassLoader( oldClassLoader );
121 }
122 }
123
124 public Map getPluginComponents( Plugin plugin, String role )
125 throws ComponentLookupException, PluginManagerException
126 {
127 MavenSession session = legacySupport.getSession();
128
129 PluginDescriptor pluginDescriptor;
130 try
131 {
132 pluginDescriptor =
133 pluginManager.getPluginDescriptor( plugin, session.getCurrentProject().getRemotePluginRepositories(),
134 session.getRepositorySession() );
135
136 pluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
137 }
138 catch ( Exception e )
139 {
140 throw new PluginManagerException( plugin, e.getMessage(), e );
141 }
142
143 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
144 try
145 {
146 Thread.currentThread().setContextClassLoader( pluginDescriptor.getClassRealm() );
147
148 return container.lookupMap( role );
149 }
150 finally
151 {
152 Thread.currentThread().setContextClassLoader( oldClassLoader );
153 }
154 }
155
156 public Plugin getPluginDefinitionForPrefix( String prefix, MavenSession session, MavenProject project )
157 {
158 PluginPrefixRequest request = new DefaultPluginPrefixRequest( prefix, session );
159 request.setPom( project.getModel() );
160
161 try
162 {
163 PluginPrefixResult result = pluginPrefixResolver.resolve( request );
164
165 Plugin plugin = new Plugin();
166 plugin.setGroupId( result.getGroupId() );
167 plugin.setArtifactId( result.getArtifactId() );
168
169 return plugin;
170 }
171 catch ( NoPluginFoundForPrefixException e )
172 {
173 return null;
174 }
175 }
176
177 public PluginDescriptor getPluginDescriptorForPrefix( String prefix )
178 {
179 MavenSession session = legacySupport.getSession();
180
181 PluginPrefixRequest request = new DefaultPluginPrefixRequest( prefix, session );
182
183 try
184 {
185 PluginPrefixResult result = pluginPrefixResolver.resolve( request );
186
187 Plugin plugin = new Plugin();
188 plugin.setGroupId( result.getGroupId() );
189 plugin.setArtifactId( result.getArtifactId() );
190
191 return loadPluginDescriptor( plugin, session.getCurrentProject(), session );
192 }
193 catch ( Exception e )
194 {
195 return null;
196 }
197 }
198
199 public PluginDescriptor loadPluginDescriptor( Plugin plugin, MavenProject project, MavenSession session )
200 throws ArtifactResolutionException, PluginVersionResolutionException, ArtifactNotFoundException,
201 InvalidVersionSpecificationException, InvalidPluginException, PluginManagerException, PluginNotFoundException,
202 PluginVersionNotFoundException
203 {
204 return verifyPlugin( plugin, project, session.getSettings(), session.getLocalRepository() );
205 }
206
207 public PluginDescriptor loadPluginFully( Plugin plugin, MavenProject project, MavenSession session )
208 throws ArtifactResolutionException, PluginVersionResolutionException, ArtifactNotFoundException,
209 InvalidVersionSpecificationException, InvalidPluginException, PluginManagerException, PluginNotFoundException,
210 PluginVersionNotFoundException
211 {
212 PluginDescriptor pluginDescriptor = loadPluginDescriptor( plugin, project, session );
213
214 try
215 {
216 pluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
217 }
218 catch ( PluginResolutionException e )
219 {
220 throw new PluginManagerException( plugin, e.getMessage(), e );
221 }
222
223 return pluginDescriptor;
224 }
225
226 public PluginDescriptor verifyPlugin( Plugin plugin, MavenProject project, Settings settings,
227 ArtifactRepository localRepository )
228 throws ArtifactResolutionException, PluginVersionResolutionException, ArtifactNotFoundException,
229 InvalidVersionSpecificationException, InvalidPluginException, PluginManagerException, PluginNotFoundException,
230 PluginVersionNotFoundException
231 {
232 MavenSession session = legacySupport.getSession();
233
234 if ( plugin.getVersion() == null )
235 {
236 PluginVersionRequest versionRequest =
237 new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
238 project.getRemotePluginRepositories() );
239 plugin.setVersion( pluginVersionResolver.resolve( versionRequest ).getVersion() );
240 }
241
242 try
243 {
244 return pluginManager.getPluginDescriptor( plugin, project.getRemotePluginRepositories(),
245 session.getRepositorySession() );
246 }
247 catch ( PluginResolutionException e )
248 {
249 throw new PluginNotFoundException( plugin, project.getPluginArtifactRepositories() );
250 }
251 catch ( PluginDescriptorParsingException e )
252 {
253 throw new PluginManagerException( plugin, e.getMessage(), e );
254 }
255 catch ( InvalidPluginDescriptorException e )
256 {
257 throw new PluginManagerException( plugin, e.getMessage(), e );
258 }
259 }
260
261 }