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