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;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.PrintStream;
23  import java.util.List;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.execution.MojoExecutionEvent;
27  import org.apache.maven.execution.MojoExecutionListener;
28  import org.apache.maven.execution.scope.internal.MojoExecutionScope;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.project.MavenProject;
33  import org.codehaus.plexus.classworlds.realm.ClassRealm;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  import org.eclipse.aether.RepositorySystemSession;
37  import org.eclipse.aether.repository.RemoteRepository;
38  
39  // TODO the antrun plugin has its own configurator, the only plugin that does. might need to think about how that works
40  // TODO remove the coreArtifactFilterManager
41  
42  /**
43   * DefaultBuildPluginManager
44   */
45  @Component(role = BuildPluginManager.class)
46  public class DefaultBuildPluginManager implements BuildPluginManager {
47  
48      @Requirement
49      private MavenPluginManager mavenPluginManager;
50  
51      @Requirement
52      private LegacySupport legacySupport;
53  
54      @Requirement
55      private MojoExecutionScope scope;
56  
57      private MojoExecutionListener mojoExecutionListener;
58  
59      // this tricks plexus-component-metadata generate required metadata
60      @Requirement(role = MojoExecutionListener.class)
61      private List<MojoExecutionListener> mojoExecutionListeners;
62  
63      public void setMojoExecutionListeners(final List<MojoExecutionListener> listeners) {
64          this.mojoExecutionListeners = listeners;
65          this.mojoExecutionListener = new CompoundMojoExecutionListener(listeners);
66      }
67  
68      /**
69       * @param plugin
70       * @param repositories
71       * @param session
72       * @return PluginDescriptor The component descriptor for the Maven plugin.
73       * @throws PluginNotFoundException The plugin could not be found in any repositories.
74       * @throws PluginResolutionException The plugin could be found but could not be resolved.
75       * @throws InvalidPluginDescriptorException
76       */
77      public PluginDescriptor loadPlugin(
78              Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session)
79              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
80                      InvalidPluginDescriptorException {
81          return mavenPluginManager.getPluginDescriptor(plugin, repositories, session);
82      }
83  
84      // ----------------------------------------------------------------------
85      // Mojo execution
86      // ----------------------------------------------------------------------
87  
88      public void executeMojo(MavenSession session, MojoExecution mojoExecution)
89              throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException {
90          MavenProject project = session.getCurrentProject();
91  
92          MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
93  
94          Mojo mojo = null;
95  
96          ClassRealm pluginRealm;
97          try {
98              pluginRealm = getPluginRealm(session, mojoDescriptor.getPluginDescriptor());
99          } catch (PluginResolutionException e) {
100             throw new PluginExecutionException(mojoExecution, project, e);
101         }
102 
103         ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
104         Thread.currentThread().setContextClassLoader(pluginRealm);
105 
106         MavenSession oldSession = legacySupport.getSession();
107 
108         scope.enter();
109 
110         try {
111             scope.seed(MavenProject.class, project);
112             scope.seed(MojoExecution.class, mojoExecution);
113 
114             mojo = mavenPluginManager.getConfiguredMojo(Mojo.class, session, mojoExecution);
115 
116             legacySupport.setSession(session);
117 
118             // NOTE: DuplicateArtifactAttachmentException is currently unchecked, so be careful removing this try/catch!
119             // This is necessary to avoid creating compatibility problems for existing plugins that use
120             // MavenProjectHelper.attachArtifact(..).
121             try {
122                 MojoExecutionEvent mojoExecutionEvent = new MojoExecutionEvent(session, project, mojoExecution, mojo);
123 
124                 mojoExecutionListener.beforeMojoExecution(mojoExecutionEvent);
125 
126                 mojo.execute();
127 
128                 mojoExecutionListener.afterMojoExecutionSuccess(mojoExecutionEvent);
129             } catch (ClassCastException e) {
130                 // to be processed in the outer catch block
131                 throw e;
132             } catch (RuntimeException e) {
133                 throw new PluginExecutionException(mojoExecution, project, e);
134             }
135         } catch (PluginContainerException e) {
136             mojoExecutionListener.afterExecutionFailure(
137                     new MojoExecutionEvent(session, project, mojoExecution, mojo, e));
138 
139             throw new PluginExecutionException(mojoExecution, project, e);
140         } catch (NoClassDefFoundError e) {
141             mojoExecutionListener.afterExecutionFailure(
142                     new MojoExecutionEvent(session, project, mojoExecution, mojo, e));
143 
144             ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
145             PrintStream ps = new PrintStream(os);
146             ps.println(
147                     "A required class was missing while executing " + mojoDescriptor.getId() + ": " + e.getMessage());
148             pluginRealm.display(ps);
149 
150             Exception wrapper = new PluginContainerException(mojoDescriptor, pluginRealm, os.toString(), e);
151 
152             throw new PluginExecutionException(mojoExecution, project, wrapper);
153         } catch (LinkageError e) {
154             mojoExecutionListener.afterExecutionFailure(
155                     new MojoExecutionEvent(session, project, mojoExecution, mojo, e));
156 
157             ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
158             PrintStream ps = new PrintStream(os);
159             ps.println("An API incompatibility was encountered while executing " + mojoDescriptor.getId() + ": "
160                     + e.getClass().getName() + ": " + e.getMessage());
161             pluginRealm.display(ps);
162 
163             Exception wrapper = new PluginContainerException(mojoDescriptor, pluginRealm, os.toString(), e);
164 
165             throw new PluginExecutionException(mojoExecution, project, wrapper);
166         } catch (ClassCastException e) {
167             mojoExecutionListener.afterExecutionFailure(
168                     new MojoExecutionEvent(session, project, mojoExecution, mojo, e));
169 
170             ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
171             PrintStream ps = new PrintStream(os);
172             ps.println("A type incompatibility occurred while executing " + mojoDescriptor.getId() + ": "
173                     + e.getMessage());
174             pluginRealm.display(ps);
175 
176             throw new PluginExecutionException(mojoExecution, project, os.toString(), e);
177         } catch (RuntimeException e) {
178             mojoExecutionListener.afterExecutionFailure(
179                     new MojoExecutionEvent(session, project, mojoExecution, mojo, e));
180 
181             throw e;
182         } finally {
183             mavenPluginManager.releaseMojo(mojo, mojoExecution);
184 
185             scope.exit();
186 
187             Thread.currentThread().setContextClassLoader(oldClassLoader);
188 
189             legacySupport.setSession(oldSession);
190         }
191     }
192 
193     /**
194      * TODO pluginDescriptor classRealm and artifacts are set as a side effect of this
195      *      call, which is not nice.
196      * @throws PluginResolutionException
197      */
198     public ClassRealm getPluginRealm(MavenSession session, PluginDescriptor pluginDescriptor)
199             throws PluginResolutionException, PluginManagerException {
200         ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
201         if (pluginRealm != null) {
202             return pluginRealm;
203         }
204 
205         mavenPluginManager.setupPluginRealm(pluginDescriptor, session, null, null, null);
206 
207         return pluginDescriptor.getClassRealm();
208     }
209 
210     public MojoDescriptor getMojoDescriptor(
211             Plugin plugin, String goal, List<RemoteRepository> repositories, RepositorySystemSession session)
212             throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
213                     MojoNotFoundException, InvalidPluginDescriptorException {
214         return mavenPluginManager.getMojoDescriptor(plugin, goal, repositories, session);
215     }
216 }