| 1 package hudson.maven.agent; 2 3 import org.apache.maven.artifact.resolver.ArtifactNotFoundException; 4 import org.apache.maven.artifact.resolver.ArtifactResolutionException; 5 import org.apache.maven.execution.MavenSession; 6 import org.apache.maven.plugin.DefaultPluginManager; 7 import org.apache.maven.plugin.MojoExecution; 8 import org.apache.maven.plugin.MojoExecutionException; 9 import org.apache.maven.plugin.MojoFailureException; 10 import org.apache.maven.plugin.PluginConfigurationException; 11 import org.apache.maven.plugin.PluginManagerException; 12 import org.apache.maven.plugin.PluginParameterExpressionEvaluator; 13 import org.apache.maven.plugin.descriptor.MojoDescriptor; 14 import org.apache.maven.plugin.descriptor.PluginDescriptor; 15 import org.apache.maven.project.MavenProject; 16 import org.apache.maven.project.artifact.InvalidDependencyVersionException; 17 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; 18 import org.codehaus.plexus.configuration.PlexusConfiguration; 19 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; 20 import org.codehaus.plexus.util.xml.Xpp3Dom; 21 22 import java.io.IOException ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 26 32 public class PluginManagerInterceptor extends DefaultPluginManager { 33 34 private final Method mergeMojoConfiguration; 35 36 41 private static PluginManagerListener listener; 42 43 public PluginManagerInterceptor() { 44 try { 45 this.mergeMojoConfiguration = DefaultPluginManager.class.getDeclaredMethod( 46 "mergeMojoConfiguration", XmlPlexusConfiguration.class, MojoDescriptor.class); 47 mergeMojoConfiguration.setAccessible(true); 48 } catch (NoSuchMethodException e) { 49 NoSuchMethodError x = new NoSuchMethodError ("Unable to find DefaultPluginManager.mergeMojoConfiguration()"); 50 x.initCause(e); 51 throw x; 52 } 53 } 54 55 public static void setListener(PluginManagerListener _listener) { 56 listener = _listener; 57 } 58 59 public void executeMojo(MavenProject project, MojoExecution mojoExecution, MavenSession session) throws ArtifactResolutionException, MojoExecutionException, MojoFailureException, ArtifactNotFoundException, InvalidDependencyVersionException, PluginManagerException, PluginConfigurationException { 60 Xpp3Dom dom = getConfigDom(mojoExecution, project); 61 62 XmlPlexusConfiguration pomConfiguration; 63 if ( dom == null ) 64 { 65 pomConfiguration = new XmlPlexusConfiguration( "configuration" ); 66 } 67 else 68 { 69 pomConfiguration = new XmlPlexusConfiguration( dom ); 70 } 71 72 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); 73 PlexusConfiguration mergedConfiguration; 74 75 try { 76 mergedConfiguration = (PlexusConfiguration) mergeMojoConfiguration.invoke(this, pomConfiguration, mojoDescriptor ); 77 } catch (IllegalAccessException e) { 78 IllegalAccessError x = new IllegalAccessError (); 79 x.initCause(e); 80 throw x; 81 } catch (InvocationTargetException e) { 82 throw new MojoExecutionException("Failed to check configuration",e); 83 } 84 88 ExpressionEvaluator eval = new PluginParameterExpressionEvaluator( session, mojoExecution, 89 pathTranslator, getLogger(), 90 project, 91 session.getExecutionProperties() ); 92 93 try { 94 if(listener!=null) 95 listener.preExecute(project,mojoExecution,mergedConfiguration,eval); 96 super.executeMojo(project, mojoExecution, session); 97 if(listener!=null) 98 listener.postExecute(project,mojoExecution,mergedConfiguration,eval); 99 } catch (InterruptedException e) { 100 throw new AbortException("Execution aborted",e); 102 } catch (IOException e) { 103 throw new PluginManagerException(e.getMessage(),e); 104 } 105 } 106 107 private Xpp3Dom getConfigDom(MojoExecution mojoExecution, MavenProject project) { 108 MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); 109 PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); 110 String goalId = mojoDescriptor.getGoal(); 111 String groupId = pluginDescriptor.getGroupId(); 112 String artifactId = pluginDescriptor.getArtifactId(); 113 String executionId = mojoExecution.getExecutionId(); 114 Xpp3Dom dom = project.getGoalConfiguration( groupId, artifactId, executionId, goalId ); 115 Xpp3Dom reportDom = project.getReportConfiguration( groupId, artifactId, executionId ); 116 dom = Xpp3Dom.mergeXpp3Dom( dom, reportDom ); 117 if ( mojoExecution.getConfiguration() != null ) 118 { 119 dom = Xpp3Dom.mergeXpp3Dom( dom, mojoExecution.getConfiguration() ); 120 } 121 return dom; 122 } 123 124 } 125 | Popular Tags |