}
public <T> T getConfiguredMojo( Class<T> mojoInterface, MavenSession session, MojoExecution mojoExecution )
throws PluginConfigurationException, PluginContainerException
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
if ( logger.isDebugEnabled() )
{
logger.debug( "Configuring mojo " + mojoDescriptor.getId() + " from plugin realm " + pluginRealm );
}
// We are forcing the use of the plugin realm for all lookups that might occur during
// the lifecycle that is part of the lookup. Here we are specifically trying to keep
// lookups that occur in contextualize calls in line with the right realm.
ClassRealm oldLookupRealm = container.setLookupRealm( pluginRealm );
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( pluginRealm );
try
{
T mojo;
try
{
mojo = container.lookup( mojoInterface, mojoDescriptor.getRoleHint() );
}
catch ( ComponentLookupException e )
{
Throwable cause = e.getCause();
while ( cause != null && !( cause instanceof LinkageError )
&& !( cause instanceof ClassNotFoundException ) )
{
cause = cause.getCause();
}
if ( ( cause instanceof NoClassDefFoundError ) || ( cause instanceof ClassNotFoundException ) )
{
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os );
ps.println( "Unable to load the mojo '" + mojoDescriptor.getGoal() + "' in the plugin '"
+ pluginDescriptor.getId() + "'. A required class is missing: " + cause.getMessage() );
pluginRealm.display( ps );
throw new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), cause );
}
else if ( cause instanceof LinkageError )
{
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os );
ps.println( "Unable to load the mojo '" + mojoDescriptor.getGoal() + "' in the plugin '"
+ pluginDescriptor.getId() + "' due to an API incompatibility: " + e.getClass().getName()
+ ": " + cause.getMessage() );
pluginRealm.display( ps );
throw new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), cause );
}
throw new PluginContainerException( mojoDescriptor, pluginRealm, "Unable to load the mojo '"
+ mojoDescriptor.getGoal() + "' (or one of its required components) from the plugin '"
+ pluginDescriptor.getId() + "'", e );
}
if ( mojo instanceof ContextEnabled )
{