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