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