View Javadoc

1   package org.apache.maven.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * 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, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.PrintStream;
20  import java.util.List;
21  
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.model.Plugin;
24  import org.apache.maven.plugin.descriptor.MojoDescriptor;
25  import org.apache.maven.plugin.descriptor.PluginDescriptor;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.classworlds.realm.ClassRealm;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.component.annotations.Requirement;
30  import org.sonatype.aether.RepositorySystemSession;
31  import org.sonatype.aether.repository.RemoteRepository;
32  
33  // TODO: the antrun plugin has its own configurator, the only plugin that does. might need to think about how that works
34  // TODO: remove the coreArtifactFilterManager
35  
36  @Component(role = BuildPluginManager.class)
37  public class DefaultBuildPluginManager
38      implements BuildPluginManager
39  {
40  
41      @Requirement
42      private MavenPluginManager mavenPluginManager;
43  
44      @Requirement
45      private LegacySupport legacySupport;
46  
47      /**
48       * 
49       * @param plugin
50       * @param repositoryRequest
51       * @return PluginDescriptor The component descriptor for the Maven plugin.
52       * @throws PluginNotFoundException The plugin could not be found in any repositories.
53       * @throws PluginResolutionException The plugin could be found but could not be resolved.
54       * @throws InvalidPluginDescriptorException 
55       */
56      public PluginDescriptor loadPlugin( Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session )
57          throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException
58      {
59          return mavenPluginManager.getPluginDescriptor( plugin, repositories, session );
60      }
61  
62      // ----------------------------------------------------------------------
63      // Mojo execution
64      // ----------------------------------------------------------------------
65  
66      public void executeMojo( MavenSession session, MojoExecution mojoExecution )
67          throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException
68      {
69          MavenProject project = session.getCurrentProject();
70  
71          MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
72  
73          Mojo mojo = null;
74  
75          ClassRealm pluginRealm;
76          try
77          {
78              pluginRealm = getPluginRealm( session, mojoDescriptor.getPluginDescriptor() );
79          }
80          catch ( PluginResolutionException e )
81          {
82              throw new PluginExecutionException( mojoExecution, project, e );
83          }
84  
85          ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
86          Thread.currentThread().setContextClassLoader( pluginRealm );
87  
88          MavenSession oldSession = legacySupport.getSession();
89  
90          try
91          {
92              mojo = mavenPluginManager.getConfiguredMojo( Mojo.class, session, mojoExecution );
93  
94              legacySupport.setSession( session );
95  
96              // NOTE: DuplicateArtifactAttachmentException is currently unchecked, so be careful removing this try/catch!
97              // This is necessary to avoid creating compatibility problems for existing plugins that use
98              // MavenProjectHelper.attachArtifact(..).
99              try
100             {
101                 mojo.execute();
102             }
103             catch ( ClassCastException e )
104             {
105                 // to be processed in the outer catch block
106                 throw e;
107             }
108             catch ( RuntimeException e )
109             {
110                 throw new PluginExecutionException( mojoExecution, project, e );
111             }
112         }
113         catch ( PluginContainerException e )
114         {
115             throw new PluginExecutionException( mojoExecution, project, e );
116         }
117         catch ( NoClassDefFoundError e )
118         {
119             ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
120             PrintStream ps = new PrintStream( os );
121             ps.println( "A required class was missing while executing " + mojoDescriptor.getId() + ": "
122                 + e.getMessage() );
123             pluginRealm.display( ps );
124 
125             Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
126 
127             throw new PluginExecutionException( mojoExecution, project, wrapper );
128         }
129         catch ( LinkageError e )
130         {
131             ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
132             PrintStream ps = new PrintStream( os );
133             ps.println( "An API incompatibility was encountered while executing " + mojoDescriptor.getId() + ": "
134                 + e.getClass().getName() + ": " + e.getMessage() );
135             pluginRealm.display( ps );
136 
137             Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
138 
139             throw new PluginExecutionException( mojoExecution, project, wrapper );
140         }
141         catch ( ClassCastException e )
142         {
143             ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
144             PrintStream ps = new PrintStream( os );
145             ps.println( "A type incompatibility occured while executing " + mojoDescriptor.getId() + ": "
146                 + e.getMessage() );
147             pluginRealm.display( ps );
148 
149             throw new PluginExecutionException( mojoExecution, project, os.toString(), e );
150         }
151         finally
152         {
153             mavenPluginManager.releaseMojo( mojo, mojoExecution );
154 
155             Thread.currentThread().setContextClassLoader( oldClassLoader );
156 
157             legacySupport.setSession( oldSession );
158         }
159     }
160 
161     /**
162      * TODO pluginDescriptor classRealm and artifacts are set as a side effect of this
163      *      call, which is not nice.
164      * @throws PluginResolutionException 
165      */
166     public ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginDescriptor ) 
167         throws PluginResolutionException, PluginManagerException
168     {
169         ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
170         if ( pluginRealm != null )
171         {
172             return pluginRealm;
173         }
174 
175         mavenPluginManager.setupPluginRealm( pluginDescriptor, session, null, null, null );
176 
177         return pluginDescriptor.getClassRealm();
178     }
179 
180     public MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, List<RemoteRepository> repositories,
181                                              RepositorySystemSession session )
182         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
183         MojoNotFoundException, InvalidPluginDescriptorException
184     {
185         return mavenPluginManager.getMojoDescriptor( plugin, goal, repositories, session );
186     }
187 
188 }