public void executeMojo( MavenSession session, MojoExecution mojoExecution )
throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException
{
MavenProject project = session.getCurrentProject();
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
Mojo mojo = null;
ClassRealm pluginRealm;
try
{
pluginRealm = getPluginRealm( session, mojoDescriptor.getPluginDescriptor() );
}
catch ( PluginResolutionException e )
{
throw new PluginExecutionException( mojoExecution, project, e );
}
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( pluginRealm );
MavenSession oldSession = legacySupport.getSession();
try
{
mojo = mavenPluginManager.getConfiguredMojo( Mojo.class, session, mojoExecution );
legacySupport.setSession( session );
// NOTE: DuplicateArtifactAttachmentException is currently unchecked, so be careful removing this try/catch!
// This is necessary to avoid creating compatibility problems for existing plugins that use
// MavenProjectHelper.attachArtifact(..).
try
{
mojo.execute();
}
catch ( ClassCastException e )
{
// to be processed in the outer catch block
throw e;
}
catch ( RuntimeException e )
{
throw new PluginExecutionException( mojoExecution, project, e );
}
}
catch ( PluginContainerException e )
{
throw new PluginExecutionException( mojoExecution, project, e );
}
catch ( NoClassDefFoundError e )
{
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os );
ps.println( "A required class was missing while executing " + mojoDescriptor.getId() + ": "
+ e.getMessage() );
pluginRealm.display( ps );
Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
throw new PluginExecutionException( mojoExecution, project, wrapper );
}
catch ( LinkageError e )
{
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os );
ps.println( "An API incompatibility was encountered while executing " + mojoDescriptor.getId() + ": "
+ e.getClass().getName() + ": " + e.getMessage() );
pluginRealm.display( ps );
Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
throw new PluginExecutionException( mojoExecution, project, wrapper );
}
catch ( ClassCastException e )
{
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os );
ps.println( "A type incompatibility occured while executing " + mojoDescriptor.getId() + ": "
+ e.getMessage() );
pluginRealm.display( ps );
throw new PluginExecutionException( mojoExecution, project, os.toString(), e );
}