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