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