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 org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
24  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
25  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.model.Plugin;
28  import org.apache.maven.plugin.InvalidPluginDescriptorException;
29  import org.apache.maven.plugin.InvalidPluginException;
30  import org.apache.maven.plugin.LegacySupport;
31  import org.apache.maven.plugin.MavenPluginManager;
32  import org.apache.maven.plugin.MojoExecution;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugin.PluginConfigurationException;
36  import org.apache.maven.plugin.PluginDescriptorParsingException;
37  import org.apache.maven.plugin.PluginManager;
38  import org.apache.maven.plugin.PluginManagerException;
39  import org.apache.maven.plugin.PluginNotFoundException;
40  import org.apache.maven.plugin.PluginResolutionException;
41  import org.apache.maven.plugin.descriptor.PluginDescriptor;
42  import org.apache.maven.plugin.prefix.DefaultPluginPrefixRequest;
43  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
44  import org.apache.maven.plugin.prefix.PluginPrefixRequest;
45  import org.apache.maven.plugin.prefix.PluginPrefixResolver;
46  import org.apache.maven.plugin.prefix.PluginPrefixResult;
47  import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
48  import org.apache.maven.plugin.version.PluginVersionNotFoundException;
49  import org.apache.maven.plugin.version.PluginVersionRequest;
50  import org.apache.maven.plugin.version.PluginVersionResolutionException;
51  import org.apache.maven.plugin.version.PluginVersionResolver;
52  import org.apache.maven.project.MavenProject;
53  import org.apache.maven.project.artifact.InvalidDependencyVersionException;
54  import org.apache.maven.settings.Settings;
55  import org.codehaus.plexus.PlexusContainer;
56  import org.codehaus.plexus.component.annotations.Component;
57  import org.codehaus.plexus.component.annotations.Requirement;
58  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
59  
60  import java.util.Map;
61  
62  /**
63   * @author Benjamin Bentmann
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<String, Object> 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 | InvalidPluginDescriptorException e )
252         {
253             throw new PluginManagerException( plugin, e.getMessage(), e );
254         }
255     }
256 
257 }