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