1 package org.apache.maven.reporting.exec;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38 @Component( role = MavenPluginManagerHelper.class )
39 public class DefaultMavenPluginManagerHelper
40 implements MavenPluginManagerHelper
41 {
42 @Requirement
43 private Logger logger;
44
45 @Requirement
46 protected MavenPluginManager mavenPluginManager;
47
48 private Boolean isEclipseAether;
49
50 private Method setupPluginRealm;
51
52 private Method getPluginDescriptor;
53
54 private Method getRepositorySession;
55
56 public DefaultMavenPluginManagerHelper()
57 {
58 try
59 {
60 for ( Method m : MavenPluginManager.class.getMethods() )
61 {
62 if ( "setupPluginRealm".equals( m.getName() ) )
63 {
64 setupPluginRealm = m;
65 }
66 else if ( "getPluginDescriptor".equals( m.getName() ) )
67 {
68 getPluginDescriptor = m;
69 }
70 }
71 }
72 catch ( SecurityException e )
73 {
74 logger.warn( "unable to find MavenPluginManager.setupPluginRealm() method", e );
75 }
76
77 try
78 {
79 for ( Method m : MavenSession.class.getMethods() )
80 {
81 if ( "getRepositorySession".equals( m.getName() ) )
82 {
83 getRepositorySession = m;
84 break;
85 }
86 }
87 }
88 catch ( SecurityException e )
89 {
90 logger.warn( "unable to find MavenSession.getRepositorySession() method", e );
91 }
92 }
93
94 private boolean isEclipseAether()
95 {
96 if ( isEclipseAether == null )
97 {
98 try
99 {
100 Thread.currentThread().getContextClassLoader().loadClass( "org.sonatype.aether.graph.DependencyFilter" );
101 isEclipseAether = false;
102 }
103 catch ( ClassNotFoundException e )
104 {
105 isEclipseAether = true;
106 }
107 }
108
109 return isEclipseAether.booleanValue();
110 }
111
112 private Object createExclusionsDependencyFilter( List<String> artifactIdsList )
113 {
114 if ( isEclipseAether() )
115 {
116 return new org.eclipse.aether.util.filter.ExclusionsDependencyFilter( artifactIdsList );
117 }
118 else
119 {
120 return new org.sonatype.aether.util.filter.ExclusionsDependencyFilter( artifactIdsList );
121 }
122 }
123
124 public PluginDescriptor getPluginDescriptor( Plugin plugin, MavenSession session )
125 throws PluginResolutionException, PluginDescriptorParsingException, InvalidPluginDescriptorException
126 {
127 try
128 {
129 Object repositorySession = getRepositorySession.invoke( session );
130 List<?> remoteRepositories = session.getCurrentProject().getRemotePluginRepositories();
131
132 return (PluginDescriptor) getPluginDescriptor.invoke( mavenPluginManager, plugin, remoteRepositories,
133 repositorySession );
134 }
135 catch ( IllegalArgumentException e )
136 {
137 logger.warn( "IllegalArgumentException during MavenPluginManager.getPluginDescriptor() call", e );
138 }
139 catch ( IllegalAccessException e )
140 {
141 logger.warn( "IllegalAccessException during MavenPluginManager.getPluginDescriptor() call", e );
142 }
143 catch ( InvocationTargetException e )
144 {
145 Throwable target = e.getTargetException();
146 if ( target instanceof PluginResolutionException )
147 {
148 throw (PluginResolutionException) target;
149 }
150 if ( target instanceof PluginDescriptorParsingException )
151 {
152 throw (PluginDescriptorParsingException) target;
153 }
154 if ( target instanceof InvalidPluginDescriptorException )
155 {
156 throw (InvalidPluginDescriptorException) target;
157 }
158 if ( target instanceof RuntimeException )
159 {
160 throw (RuntimeException) target;
161 }
162 if ( target instanceof Error )
163 {
164 throw (Error) target;
165 }
166 logger.warn( "Exception during MavenPluginManager.getPluginDescriptor() call", e );
167 }
168
169 return null;
170 }
171
172 public void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent,
173 List<String> imports, List<String> excludeArtifactIds )
174 throws PluginResolutionException, PluginContainerException
175 {
176 try
177 {
178 setupPluginRealm.invoke( mavenPluginManager, pluginDescriptor, session, parent, imports,
179 createExclusionsDependencyFilter( excludeArtifactIds ) );
180 }
181 catch ( IllegalArgumentException e )
182 {
183 logger.warn( "IllegalArgumentException during MavenPluginManager.setupPluginRealm() call", e );
184 }
185 catch ( IllegalAccessException e )
186 {
187 logger.warn( "IllegalAccessException during MavenPluginManager.setupPluginRealm() call", e );
188 }
189 catch ( InvocationTargetException e )
190 {
191 Throwable target = e.getTargetException();
192 if ( target instanceof PluginResolutionException )
193 {
194 throw (PluginResolutionException) target;
195 }
196 if ( target instanceof PluginContainerException )
197 {
198 throw (PluginContainerException) target;
199 }
200 if ( target instanceof RuntimeException )
201 {
202 throw (RuntimeException) target;
203 }
204 if ( target instanceof Error )
205 {
206 throw (Error) target;
207 }
208 logger.warn( "Exception during MavenPluginManager.setupPluginRealm() call", e );
209 }
210 }
211 }