View Javadoc

1   package org.apache.maven.reporting.exec;
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.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  import java.util.List;
25  
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.MavenPluginManager;
30  import org.apache.maven.plugin.PluginContainerException;
31  import org.apache.maven.plugin.PluginDescriptorParsingException;
32  import org.apache.maven.plugin.PluginResolutionException;
33  import org.apache.maven.plugin.descriptor.PluginDescriptor;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  import org.codehaus.plexus.logging.Logger;
37  import org.sonatype.aether.repository.RemoteRepository;
38  
39  /**
40   * {@link MavenPluginManager} helper to deal with API changes between Maven 3.0.x and 3.1.x, ie switch from Sonatype Aether
41   * (in org.sonatype.aether package) to Eclipse Aether (in org.eclipse.aether package).
42   * 
43   * @author Hervé Boutemy
44   * @since 1.1
45   */
46  @Component( role = MavenPluginManagerHelper.class )
47  public class DefaultMavenPluginManagerHelper
48      implements MavenPluginManagerHelper
49  {
50      @Requirement
51      private Logger logger;
52  
53      @Requirement
54      protected MavenPluginManager mavenPluginManager;
55  
56      private Boolean isEclipseAether;
57  
58      private Method setupPluginRealm;
59  
60      private Method getPluginDescriptor;
61  
62      private Method getRepositorySession;
63  
64      public DefaultMavenPluginManagerHelper()
65      {
66          try
67          {
68              for ( Method m : MavenPluginManager.class.getMethods() )
69              {
70                  if ( "setupPluginRealm".equals( m.getName() ) )
71                  {
72                      setupPluginRealm = m;
73                  }
74                  else if ( "getPluginDescriptor".equals( m.getName() ) )
75                  {
76                      getPluginDescriptor = m;
77                  }
78              }
79          }
80          catch ( SecurityException e )
81          {
82              logger.warn( "unable to find MavenPluginManager.setupPluginRealm() method", e );
83          }
84  
85          try
86          {
87              for ( Method m : MavenSession.class.getMethods() )
88              {
89                  if ( "getRepositorySession".equals( m.getName() ) )
90                  {
91                      getRepositorySession = m;
92                      break;
93                  }
94              }
95          }
96          catch ( SecurityException e )
97          {
98              logger.warn( "unable to find MavenSession.getRepositorySession() method", e );
99          }
100     }
101 
102     private boolean isEclipseAether()
103     {
104         if ( isEclipseAether == null )
105         {
106             try
107             {
108                 Thread.currentThread().getContextClassLoader().loadClass( "org.sonatype.aether.graph.DependencyFilter" );
109                 isEclipseAether = false;
110             }
111             catch ( ClassNotFoundException e )
112             {
113                 isEclipseAether = true;
114             }
115         }
116 
117         return isEclipseAether.booleanValue();
118     }
119 
120     private Object createExclusionsDependencyFilter( List<String> artifactIdsList )
121     {
122         if ( isEclipseAether() )
123         {
124             return new org.eclipse.aether.util.filter.ExclusionsDependencyFilter( artifactIdsList );
125         }
126         else
127         {
128             return new org.sonatype.aether.util.filter.ExclusionsDependencyFilter( artifactIdsList );
129         }
130     }
131 
132     public PluginDescriptor getPluginDescriptor( Plugin plugin, List<RemoteRepository> repositories,
133                                                  MavenSession session )
134         throws PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException
135     {
136         try
137         {
138             Object repositorySession = getRepositorySession.invoke( session );
139 
140             return (PluginDescriptor) getPluginDescriptor.invoke( mavenPluginManager, plugin, repositories,
141                                                                   repositorySession );
142         }
143         catch ( IllegalArgumentException e )
144         {
145             logger.warn( "IllegalArgumentException during MavenPluginManager.getPluginDescriptor() call", e );
146         }
147         catch ( IllegalAccessException e )
148         {
149             logger.warn( "IllegalAccessException during MavenPluginManager.getPluginDescriptor() call", e );
150         }
151         catch ( InvocationTargetException e )
152         {
153             Throwable target = e.getTargetException();
154             if ( target instanceof PluginResolutionException )
155             {
156                 throw (PluginResolutionException) target;
157             }
158             if ( target instanceof PluginDescriptorParsingException )
159             {
160                 throw (PluginDescriptorParsingException) target;
161             }
162             if ( target instanceof InvalidPluginDescriptorException )
163             {
164                 throw (InvalidPluginDescriptorException) target;
165             }
166             if ( target instanceof RuntimeException )
167             {
168                 throw (RuntimeException) target;
169             }
170             if ( target instanceof Error )
171             {
172                 throw (Error) target;
173             }
174             logger.warn( "Exception during MavenPluginManager.getPluginDescriptor() call", e );
175         }
176 
177         return null;
178     }
179 
180     public void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent,
181                                   List<String> imports, List<String> excludeArtifactIds )
182         throws PluginResolutionException, PluginContainerException
183     {
184         try
185         {
186             setupPluginRealm.invoke( mavenPluginManager, pluginDescriptor, session, parent, imports,
187                                      createExclusionsDependencyFilter( excludeArtifactIds ) );
188         }
189         catch ( IllegalArgumentException e )
190         {
191             logger.warn( "IllegalArgumentException during MavenPluginManager.setupPluginRealm() call", e );
192         }
193         catch ( IllegalAccessException e )
194         {
195             logger.warn( "IllegalAccessException during MavenPluginManager.setupPluginRealm() call", e );
196         }
197         catch ( InvocationTargetException e )
198         {
199             Throwable target = e.getTargetException();
200             if ( target instanceof PluginResolutionException )
201             {
202                 throw (PluginResolutionException) target;
203             }
204             if ( target instanceof PluginContainerException )
205             {
206                 throw (PluginContainerException) target;
207             }
208             if ( target instanceof RuntimeException )
209             {
210                 throw (RuntimeException) target;
211             }
212             if ( target instanceof Error )
213             {
214                 throw (Error) target;
215             }
216             logger.warn( "Exception during MavenPluginManager.setupPluginRealm() call", e );
217         }
218     }
219 }