[MNG-4186] Provide an exact pointer to documentation specific to each known exception that can occur

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@827792 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-10-20 21:08:08 +00:00
parent 6e6f881bc8
commit 504f5bca65
17 changed files with 507 additions and 267 deletions

View File

@ -196,6 +196,9 @@ private static String constructMessageBase( String message,
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append( message ); sb.append( message );
if ( message == null || !message.contains( "from the specified remote repositories:" ) )
{
sb.append( LS ); sb.append( LS );
sb.append( " " + groupId + ":" + artifactId + ":" + type + ":" + version ); sb.append( " " + groupId + ":" + artifactId + ":" + type + ":" + version );
sb.append( LS ); sb.append( LS );
@ -240,6 +243,8 @@ private static String constructMessageBase( String message,
sb.append( constructArtifactPath( path, "" ) ); sb.append( constructArtifactPath( path, "" ) );
sb.append( LS ); sb.append( LS );
}
return sb.toString(); return sb.toString();
} }

View File

@ -0,0 +1,44 @@
package org.apache.maven.repository;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
/**
* Signals a failure to store files within the local repository.
*
* @author Benjamin Bentmann
*/
public class LocalRepositoryNotAccessibleException
extends IOException
{
public LocalRepositoryNotAccessibleException( String message, Throwable cause )
{
super( message );
initCause( cause );
}
public LocalRepositoryNotAccessibleException( String message )
{
super( message );
}
}

View File

@ -122,6 +122,8 @@ else if ( !artifact.getFile().exists() )
public void getArtifact( Artifact artifact, List<ArtifactRepository> remoteRepositories, TransferListener downloadMonitor, boolean force ) public void getArtifact( Artifact artifact, List<ArtifactRepository> remoteRepositories, TransferListener downloadMonitor, boolean force )
throws TransferFailedException, ResourceDoesNotExistException throws TransferFailedException, ResourceDoesNotExistException
{ {
TransferFailedException tfe = null;
for ( ArtifactRepository repository : remoteRepositories ) for ( ArtifactRepository repository : remoteRepositories )
{ {
try try
@ -139,20 +141,31 @@ public void getArtifact( Artifact artifact, List<ArtifactRepository> remoteRepos
// This one we will eat when looking through remote repositories // This one we will eat when looking through remote repositories
// because we want to cycle through them all before squawking. // because we want to cycle through them all before squawking.
logger.debug( "Unable to find resource '" + artifact.getId() + "' in repository " + repository.getId() + " (" + repository.getUrl() + ")", e ); logger.debug( "Unable to find resource '" + artifact.getId() + "' in repository " + repository.getId()
+ " (" + repository.getUrl() + ")", e );
} }
catch ( TransferFailedException e ) catch ( TransferFailedException e )
{ {
logger.debug( "Unable to get resource '" + artifact.getId() + "' from repository " + repository.getId() + " (" + repository.getUrl() + ")", e ); tfe = e;
logger.debug( "Unable to get resource '" + artifact.getId() + "' from repository " + repository.getId()
+ " (" + repository.getUrl() + ")", e );
} }
} }
// if it already exists locally we were just trying to force it - ignore the update // if it already exists locally we were just trying to force it - ignore the update
if ( !artifact.getFile().exists() ) if ( !artifact.getFile().exists() )
{
if ( tfe != null )
{
throw tfe;
}
else
{ {
throw new ResourceDoesNotExistException( "Unable to download the artifact from any repository" ); throw new ResourceDoesNotExistException( "Unable to download the artifact from any repository" );
} }
} }
}
public void getArtifactMetadata( ArtifactMetadata metadata, ArtifactRepository repository, File destination, String checksumPolicy ) public void getArtifactMetadata( ArtifactMetadata metadata, ArtifactRepository repository, File destination, String checksumPolicy )
throws TransferFailedException, ResourceDoesNotExistException throws TransferFailedException, ResourceDoesNotExistException

View File

@ -29,9 +29,6 @@
import java.util.Map; import java.util.Map;
import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.exception.DefaultExceptionHandler;
import org.apache.maven.exception.ExceptionHandler;
import org.apache.maven.exception.ExceptionSummary;
import org.apache.maven.execution.DefaultLifecycleEvent; import org.apache.maven.execution.DefaultLifecycleEvent;
import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.DuplicateProjectException; import org.apache.maven.execution.DuplicateProjectException;
@ -50,6 +47,7 @@
import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult; import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.repository.DelegatingLocalArtifactRepository; import org.apache.maven.repository.DelegatingLocalArtifactRepository;
import org.apache.maven.repository.LocalRepositoryNotAccessibleException;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.component.annotations.Requirement;
@ -92,14 +90,18 @@ private void fireEvent( MavenSession session, ExecutionEventCatapult catapult )
public MavenExecutionResult execute( MavenExecutionRequest request ) public MavenExecutionResult execute( MavenExecutionRequest request )
{ {
MavenExecutionResult result;
try try
{ {
return doExecute( request ); result = doExecute( request );
} }
catch ( OutOfMemoryError e ) catch ( OutOfMemoryError e )
{ {
return processResult( new DefaultMavenExecutionResult(), e ); result = processResult( new DefaultMavenExecutionResult(), e );
} }
return result;
} }
private MavenExecutionResult doExecute( MavenExecutionRequest request ) private MavenExecutionResult doExecute( MavenExecutionRequest request )
@ -114,7 +116,17 @@ private MavenExecutionResult doExecute( MavenExecutionRequest request )
MavenExecutionResult result = new DefaultMavenExecutionResult(); MavenExecutionResult result = new DefaultMavenExecutionResult();
DelegatingLocalArtifactRepository delegatingLocalArtifactRepository = new DelegatingLocalArtifactRepository( request.getLocalRepository() ); try
{
validateLocalRepository( request );
}
catch ( LocalRepositoryNotAccessibleException e )
{
return processResult( result, e );
}
DelegatingLocalArtifactRepository delegatingLocalArtifactRepository =
new DelegatingLocalArtifactRepository( request.getLocalRepository() );
request.setLocalRepository( delegatingLocalArtifactRepository ); request.setLocalRepository( delegatingLocalArtifactRepository );
@ -229,6 +241,18 @@ private MavenExecutionResult doExecute( MavenExecutionRequest request )
return result; return result;
} }
private void validateLocalRepository( MavenExecutionRequest request )
throws LocalRepositoryNotAccessibleException
{
File localRepoDir = request.getLocalRepositoryPath();
localRepoDir.mkdirs();
if ( !localRepoDir.isDirectory() )
{
throw new LocalRepositoryNotAccessibleException( "Could not create local repository at " + localRepoDir );
}
}
private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants( Collection<MavenProject> projects ) private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants( Collection<MavenProject> projects )
{ {
Collection<AbstractMavenLifecycleParticipant> lifecycleListeners = Collection<AbstractMavenLifecycleParticipant> lifecycleListeners =
@ -279,17 +303,11 @@ private Collection<AbstractMavenLifecycleParticipant> getLifecycleParticipants(
private MavenExecutionResult processResult( MavenExecutionResult result, Throwable e ) private MavenExecutionResult processResult( MavenExecutionResult result, Throwable e )
{ {
ExceptionHandler handler = new DefaultExceptionHandler();
ExceptionSummary es = handler.handleException( e );
if ( !result.getExceptions().contains( e ) ) if ( !result.getExceptions().contains( e ) )
{ {
result.addException( e ); result.addException( e );
} }
result.setExceptionSummary( es );
return result; return result;
} }

View File

@ -19,16 +19,17 @@
* under the License. * under the License.
*/ */
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import java.net.UnknownHostException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException; import java.util.ArrayList;
import org.apache.maven.plugin.CycleDetectedInPluginGraphException; import java.util.List;
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.plugin.AbstractMojoExecutionException;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.MojoNotFoundException; import org.apache.maven.plugin.PluginExecutionException;
import org.apache.maven.plugin.PluginDescriptorParsingException; import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.plugin.PluginNotFoundException; import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
@ -79,73 +80,162 @@
public class DefaultExceptionHandler public class DefaultExceptionHandler
implements ExceptionHandler implements ExceptionHandler
{ {
public ExceptionSummary handleException( Throwable exception ) public ExceptionSummary handleException( Throwable exception )
{ {
String message; return handle( "", exception );
String reference = "http://";
// Plugin problems
if ( exception instanceof PluginNotFoundException )
{
message = exception.getMessage();
}
else if ( exception instanceof PluginResolutionException )
{
message = exception.getMessage();
}
else if ( exception instanceof PluginDescriptorParsingException )
{
message = exception.getMessage();
}
else if ( exception instanceof CycleDetectedInPluginGraphException )
{
message = exception.getMessage();
}
else if ( exception instanceof NoPluginFoundForPrefixException )
{
message = exception.getMessage();
} }
// Project dependency downloading problems. private ExceptionSummary handle( String message, Throwable exception )
else if ( exception instanceof ArtifactNotFoundException )
{ {
message = exception.getMessage(); String reference = getReference( exception );
List<ExceptionSummary> children = null;
if ( exception instanceof ProjectBuildingException )
{
List<ProjectBuildingResult> results = ( (ProjectBuildingException) exception ).getResults();
children = new ArrayList<ExceptionSummary>();
for ( ProjectBuildingResult result : results )
{
ExceptionSummary child = handle( result );
if ( child != null )
{
children.add( child );
} }
else if ( exception instanceof ArtifactResolutionException )
{
message = exception.getMessage();
} }
// Mojo problems message = "The build could not read " + children.size() + " project" + ( children.size() == 1 ? "" : "s" );
else if ( exception instanceof MojoNotFoundException ) }
else
{ {
message = exception.getMessage(); message = getMessage( message, exception );
}
return new ExceptionSummary( exception, message, reference, children );
}
private ExceptionSummary handle( ProjectBuildingResult result )
{
List<ExceptionSummary> children = new ArrayList<ExceptionSummary>();
for ( ModelProblem problem : result.getProblems() )
{
ExceptionSummary child = handle( problem );
if ( child != null )
{
children.add( child );
}
}
if ( children.isEmpty() )
{
return null;
}
String message =
"The project " + result.getProjectId() + " (" + result.getPomFile() + ") has " + children.size() + " error"
+ ( children.size() == 1 ? "" : "s" );
return new ExceptionSummary( null, message, null, children );
}
private ExceptionSummary handle( ModelProblem problem )
{
if ( ModelProblem.Severity.ERROR.compareTo( problem.getSeverity() ) >= 0 )
{
return handle( problem.getMessage(), problem.getException() );
}
else
{
return null;
}
}
private String getReference( Throwable exception )
{
String reference = "";
if ( exception != null )
{
if ( exception instanceof MojoExecutionException )
{
reference = MojoExecutionException.class.getSimpleName();
} }
else if ( exception instanceof MojoFailureException ) else if ( exception instanceof MojoFailureException )
{ {
message = ((MojoFailureException)exception).getLongMessage(); reference = MojoFailureException.class.getSimpleName();
}
if ( StringUtils.isEmpty( message ) ) else if ( exception instanceof LinkageError )
{ {
message = exception.getMessage(); reference = LinkageError.class.getSimpleName();
}
else if ( exception instanceof PluginExecutionException )
{
reference = getReference( exception.getCause() );
if ( StringUtils.isEmpty( reference ) )
{
reference = exception.getClass().getSimpleName();
} }
} }
else if ( exception instanceof MojoExecutionException ) else if ( !( exception instanceof RuntimeException ) )
{ {
message = ((MojoExecutionException)exception).getLongMessage(); reference = exception.getClass().getSimpleName();
if ( StringUtils.isEmpty( message ) )
{
message = exception.getMessage();
} }
} }
else if ( StringUtils.isNotEmpty( reference ) && !reference.startsWith( "http:" ) )
{ {
message = exception.getMessage(); reference = "http://cwiki.apache.org/confluence/display/MAVEN/" + reference;
} }
return new ExceptionSummary( exception, message, reference ); return reference;
} }
private String getMessage( String message, Throwable exception )
{
String fullMessage = ( message != null ) ? message : "";
for ( Throwable t = exception; t != null; t = t.getCause() )
{
String exceptionMessage = t.getMessage();
if ( t instanceof AbstractMojoExecutionException )
{
String longMessage = ( (AbstractMojoExecutionException) t ).getLongMessage();
if ( StringUtils.isNotEmpty( longMessage ) )
{
exceptionMessage = longMessage;
}
}
if ( t instanceof UnknownHostException && !fullMessage.contains( "host" ) )
{
if ( fullMessage.length() > 0 )
{
fullMessage += ": ";
}
fullMessage += "Unknown host " + exceptionMessage;
}
else if ( !fullMessage.contains( exceptionMessage ) )
{
if ( fullMessage.length() > 0 )
{
fullMessage += ": ";
}
fullMessage += exceptionMessage;
}
}
if ( StringUtils.isEmpty( fullMessage ) && exception != null )
{
fullMessage = exception.toString();
}
return fullMessage.trim();
}
} }

View File

@ -19,6 +19,9 @@
* under the License. * under the License.
*/ */
import java.util.Collections;
import java.util.List;
// provide a // provide a
// - the exception // - the exception
// - useful message // - useful message
@ -28,17 +31,26 @@
public class ExceptionSummary public class ExceptionSummary
{ {
private Throwable exception; private Throwable exception;
private String message; private String message;
private String reference; private String reference;
private List<ExceptionSummary> children;
public ExceptionSummary( Throwable exception, String message, String reference ) public ExceptionSummary( Throwable exception, String message, String reference )
{
this( exception, message, reference, null );
}
public ExceptionSummary( Throwable exception, String message, String reference, List<ExceptionSummary> children )
{ {
this.exception = exception; this.exception = exception;
this.message = message; this.message = ( message != null ) ? message : "";
this.reference = reference; this.reference = ( reference != null ) ? reference : "";
this.children = ( children != null ) ? children : Collections.<ExceptionSummary> emptyList();
} }
public Throwable getException() public Throwable getException()
@ -55,4 +67,10 @@ public String getReference()
{ {
return reference; return reference;
} }
public List<ExceptionSummary> getChildren()
{
return children;
}
} }

View File

@ -426,6 +426,11 @@ public MavenExecutionRequest setLocalRepository( ArtifactRepository localReposit
{ {
this.localRepository = localRepository; this.localRepository = localRepository;
if ( localRepository != null )
{
setLocalRepositoryPath( new File( localRepository.getBasedir() ).getAbsoluteFile() );
}
return this; return this;
} }

View File

@ -26,7 +26,6 @@
import java.util.Map; import java.util.Map;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult; import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.exception.ExceptionSummary;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
/** @author Jason van Zyl */ /** @author Jason van Zyl */
@ -41,8 +40,6 @@ public class DefaultMavenExecutionResult
private List<Throwable> exceptions; private List<Throwable> exceptions;
private ExceptionSummary exceptionSummary;
private Map<MavenProject, BuildSummary> buildSummaries; private Map<MavenProject, BuildSummary> buildSummaries;
public MavenExecutionResult setProject( MavenProject project ) public MavenExecutionResult setProject( MavenProject project )
@ -103,18 +100,6 @@ public boolean hasExceptions()
return !getExceptions().isEmpty(); return !getExceptions().isEmpty();
} }
public MavenExecutionResult setExceptionSummary( ExceptionSummary exceptionSummary )
{
this.exceptionSummary = exceptionSummary;
return this;
}
public ExceptionSummary getExceptionSummary()
{
return exceptionSummary;
}
public BuildSummary getBuildSummary( MavenProject project ) public BuildSummary getBuildSummary( MavenProject project )
{ {
return ( buildSummaries != null ) ? buildSummaries.get( project ) : null; return ( buildSummaries != null ) ? buildSummaries.get( project ) : null;

View File

@ -50,9 +50,6 @@ public interface MavenExecutionResult
boolean hasExceptions(); boolean hasExceptions();
MavenExecutionResult setExceptionSummary( ExceptionSummary exceptionSummary );
ExceptionSummary getExceptionSummary();
/** /**
* Gets the build summary for the specified project. * Gets the build summary for the specified project.
* *

View File

@ -36,7 +36,7 @@ MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, RepositoryRequest
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException; throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException;
ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginDescriptor ) ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginDescriptor )
throws PluginManagerException; throws PluginResolutionException, PluginManagerException;
void executeMojo( MavenSession session, MojoExecution execution ) void executeMojo( MavenSession session, MojoExecution execution )
throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException; throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException;

View File

@ -19,7 +19,6 @@
import java.io.PrintStream; import java.io.PrintStream;
import org.apache.maven.artifact.repository.RepositoryRequest; import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin; import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.MojoDescriptor;
@ -75,7 +74,16 @@ public void executeMojo( MavenSession session, MojoExecution mojoExecution )
Mojo mojo = null; Mojo mojo = null;
ClassRealm pluginRealm = getPluginRealm( session, mojoDescriptor.getPluginDescriptor() ); ClassRealm pluginRealm;
try
{
pluginRealm = getPluginRealm( session, mojoDescriptor.getPluginDescriptor() );
}
catch ( PluginResolutionException e )
{
throw new PluginExecutionException( mojoExecution, project, e );
}
ClassRealm oldLookupRealm = container.getLookupRealm(); ClassRealm oldLookupRealm = container.getLookupRealm();
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
@ -110,22 +118,36 @@ public void executeMojo( MavenSession session, MojoExecution mojoExecution )
{ {
throw new PluginExecutionException( mojoExecution, project, 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 ) catch ( LinkageError e )
{ {
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 ); ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os ); PrintStream ps = new PrintStream( os );
ps.println( "A linkage error occured while executing " + mojoDescriptor.getId() ); ps.println( "An API incompatibility was encountered while executing " + mojoDescriptor.getId() + ": "
ps.println( e ); + e.getClass().getName() + ": " + e.getMessage() );
pluginRealm.display( ps ); pluginRealm.display( ps );
throw new PluginExecutionException( mojoExecution, project, os.toString(), e ); Exception wrapper = new PluginContainerException( mojoDescriptor, pluginRealm, os.toString(), e );
throw new PluginExecutionException( mojoExecution, project, wrapper );
} }
catch ( ClassCastException e ) catch ( ClassCastException e )
{ {
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 ); ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( os ); PrintStream ps = new PrintStream( os );
ps.println( "A type incompatibility occured while executing " + mojoDescriptor.getId() ); ps.println( "A type incompatibility occured while executing " + mojoDescriptor.getId() + ": "
ps.println( e ); + e.getMessage() );
pluginRealm.display( ps ); pluginRealm.display( ps );
throw new PluginExecutionException( mojoExecution, project, os.toString(), e ); throw new PluginExecutionException( mojoExecution, project, os.toString(), e );
@ -148,10 +170,10 @@ public void executeMojo( MavenSession session, MojoExecution mojoExecution )
/** /**
* TODO pluginDescriptor classRealm and artifacts are set as a side effect of this * TODO pluginDescriptor classRealm and artifacts are set as a side effect of this
* call, which is not nice. * call, which is not nice.
* @throws ArtifactResolutionException * @throws PluginResolutionException
*/ */
public ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginDescriptor ) public ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginDescriptor )
throws PluginManagerException throws PluginResolutionException, PluginManagerException
{ {
ClassRealm pluginRealm = pluginDescriptor.getClassRealm(); ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
if ( pluginRealm != null ) if ( pluginRealm != null )
@ -159,17 +181,8 @@ public ClassRealm getPluginRealm( MavenSession session, PluginDescriptor pluginD
return pluginRealm; return pluginRealm;
} }
Plugin plugin = pluginDescriptor.getPlugin(); mavenPluginManager.setupPluginRealm( pluginDescriptor, session, session.getCurrentProject().getClassRealm(),
null );
try
{
mavenPluginManager.setupPluginRealm( pluginDescriptor, session,
session.getCurrentProject().getClassRealm(), null );
}
catch ( PluginResolutionException e )
{
throw new PluginManagerException( plugin, e.getMessage(), e );
}
return pluginDescriptor.getClassRealm(); return pluginDescriptor.getClassRealm();
} }

View File

@ -74,7 +74,7 @@ MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, RepositoryRequest
*/ */
void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent, void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent,
List<String> imports ) List<String> imports )
throws PluginResolutionException, PluginManagerException; throws PluginResolutionException, PluginContainerException;
/** /**
* Looks up the mojo for the specified mojo execution and populates its parameters from the configuration given by * Looks up the mojo for the specified mojo execution and populates its parameters from the configuration given by

View File

@ -42,6 +42,13 @@ public class PluginContainerException
private ClassRealm pluginRealm; private ClassRealm pluginRealm;
public PluginContainerException( MojoDescriptor mojoDescriptor, ClassRealm pluginRealm, String message, Throwable e )
{
super( mojoDescriptor, message, e );
this.pluginRealm = pluginRealm;
}
public PluginContainerException( MojoDescriptor mojoDescriptor, ClassRealm pluginRealm, String message, ComponentLookupException e ) public PluginContainerException( MojoDescriptor mojoDescriptor, ClassRealm pluginRealm, String message, ComponentLookupException e )
{ {
super( mojoDescriptor, message, e ); super( mojoDescriptor, message, e );
@ -49,6 +56,13 @@ public PluginContainerException( MojoDescriptor mojoDescriptor, ClassRealm plugi
this.pluginRealm = pluginRealm; this.pluginRealm = pluginRealm;
} }
public PluginContainerException( Plugin plugin, ClassRealm pluginRealm, String message, Throwable e )
{
super( plugin, message, e );
this.pluginRealm = pluginRealm;
}
public PluginContainerException( Plugin plugin, ClassRealm pluginRealm, String message, PlexusConfigurationException e ) public PluginContainerException( Plugin plugin, ClassRealm pluginRealm, String message, PlexusConfigurationException e )
{ {
super( plugin, message, e ); super( plugin, message, e );

View File

@ -36,8 +36,9 @@ public class PluginResolutionException
public PluginResolutionException( Plugin plugin, ArtifactResolutionException e ) public PluginResolutionException( Plugin plugin, ArtifactResolutionException e )
{ {
super( "Plugin or one of its dependencies could not be resolved: " + e.getMessage(), e.getGroupId(), super( "Plugin " + plugin.getId() + " or one of its dependencies could not be resolved: " + e.getMessage(),
e.getArtifactId(), e.getVersion(), e.getType(), null, e.getRemoteRepositories(), null, e.getCause() ); e.getGroupId(), e.getArtifactId(), e.getVersion(), e.getType(), null, e.getRemoteRepositories(), null,
e );
this.plugin = plugin; this.plugin = plugin;
} }

View File

@ -20,10 +20,12 @@
*/ */
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintStream;
import java.io.Reader; import java.io.Reader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.ArrayList; import java.util.ArrayList;
@ -63,7 +65,6 @@
import org.apache.maven.plugin.PluginContainerException; import org.apache.maven.plugin.PluginContainerException;
import org.apache.maven.plugin.PluginDescriptorCache; import org.apache.maven.plugin.PluginDescriptorCache;
import org.apache.maven.plugin.PluginDescriptorParsingException; import org.apache.maven.plugin.PluginDescriptorParsingException;
import org.apache.maven.plugin.PluginManagerException;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator; import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import org.apache.maven.plugin.PluginRealmCache; import org.apache.maven.plugin.PluginRealmCache;
import org.apache.maven.plugin.PluginResolutionException; import org.apache.maven.plugin.PluginResolutionException;
@ -290,7 +291,7 @@ public MojoDescriptor getMojoDescriptor( Plugin plugin, String goal, RepositoryR
public synchronized void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, public synchronized void setupPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session,
ClassLoader parent, List<String> imports ) ClassLoader parent, List<String> imports )
throws PluginResolutionException, PluginManagerException throws PluginResolutionException, PluginContainerException
{ {
Plugin plugin = pluginDescriptor.getPlugin(); Plugin plugin = pluginDescriptor.getPlugin();
@ -320,7 +321,7 @@ public synchronized void setupPluginRealm( PluginDescriptor pluginDescriptor, Ma
private void createPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent, private void createPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session, ClassLoader parent,
List<String> imports ) List<String> imports )
throws PluginResolutionException, PluginManagerException throws PluginResolutionException, PluginContainerException
{ {
Plugin plugin = pluginDescriptor.getPlugin(); Plugin plugin = pluginDescriptor.getPlugin();
@ -401,13 +402,13 @@ private void createPluginRealm( PluginDescriptor pluginDescriptor, MavenSession
} }
catch ( PlexusConfigurationException e ) catch ( PlexusConfigurationException e )
{ {
throw new PluginManagerException( plugin, "Error in component graph of plugin " + plugin.getId() + ": " throw new PluginContainerException( plugin, pluginRealm, "Error in component graph of plugin "
+ e.getMessage(), e ); + plugin.getId() + ": " + e.getMessage(), e );
} }
catch ( CycleDetectedInComponentGraphException e ) catch ( CycleDetectedInComponentGraphException e )
{ {
throw new PluginManagerException( plugin, "Error in component graph of plugin " + plugin.getId() + ": " throw new PluginContainerException( plugin, pluginRealm, "Error in component graph of plugin "
+ e.getMessage(), e ); + plugin.getId() + ": " + e.getMessage(), e );
} }
} }
@ -497,13 +498,28 @@ public <T> T getConfiguredMojo( Class<T> mojoInterface, MavenSession session, Mo
if ( ( cause instanceof NoClassDefFoundError ) || ( cause instanceof ClassNotFoundException ) ) if ( ( cause instanceof NoClassDefFoundError ) || ( cause instanceof ClassNotFoundException ) )
{ {
throw new PluginContainerException( mojoDescriptor, pluginRealm, "Unable to load the mojo '" ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
+ mojoDescriptor.getGoal() + "' in the plugin '" + pluginDescriptor.getId() PrintStream ps = new PrintStream( os );
+ "'. A required class is missing: " + cause.getMessage(), e ); 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 find the mojo '" throw new PluginContainerException( mojoDescriptor, pluginRealm, "Unable to load the mojo '"
+ mojoDescriptor.getGoal() + "' (or one of its required components) in the plugin '" + mojoDescriptor.getGoal() + "' (or one of its required components) from the plugin '"
+ pluginDescriptor.getId() + "'", e ); + pluginDescriptor.getId() + "'", e );
} }
@ -596,46 +612,23 @@ private void populatePluginFields( Object mojo, MojoDescriptor mojoDescriptor, C
} }
catch ( NoClassDefFoundError e ) catch ( NoClassDefFoundError e )
{ {
throw new PluginConfigurationException( mojoDescriptor.getPluginDescriptor(), ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
"A required class was missing during configuration of mojo " PrintStream ps = new PrintStream( os );
+ mojoDescriptor.getId() + ": " + e.getMessage(), e ); ps.println( "A required class was missing during configuration of mojo " + mojoDescriptor.getId() + ": "
+ e.getMessage() );
pluginRealm.display( ps );
throw new PluginConfigurationException( mojoDescriptor.getPluginDescriptor(), os.toString(), e );
} }
catch ( LinkageError e ) catch ( LinkageError e )
{ {
if ( logger.isFatalErrorEnabled() ) ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
{ PrintStream ps = new PrintStream( os );
logger.fatalError( configurator.getClass().getName() ps.println( "An API incompatibility was encountered during configuration of mojo " + mojoDescriptor.getId()
+ "#configureComponent(...) caused a linkage error (" + e.getClass().getName() + ": " + e.getClass().getName() + ": " + e.getMessage() );
+ ") and may be out-of-date. Check the realms:" ); pluginRealm.display( ps );
StringBuilder sb = new StringBuilder(); throw new PluginConfigurationException( mojoDescriptor.getPluginDescriptor(), os.toString(), e );
sb.append( "Plugin realm = " + pluginRealm.getId() ).append( '\n' );
for ( int i = 0; i < pluginRealm.getURLs().length; i++ )
{
sb.append( "urls[" + i + "] = " + pluginRealm.getURLs()[i] );
if ( i != ( pluginRealm.getURLs().length - 1 ) )
{
sb.append( '\n' );
}
}
logger.fatalError( sb.toString() );
ClassRealm containerRealm = container.getContainerRealm();
sb = new StringBuilder();
sb.append( "Container realm = " + containerRealm.getId() ).append( '\n' );
for ( int i = 0; i < containerRealm.getURLs().length; i++ )
{
sb.append( "urls[" + i + "] = " + containerRealm.getURLs()[i] );
if ( i != ( containerRealm.getURLs().length - 1 ) )
{
sb.append( '\n' );
}
}
logger.fatalError( sb.toString() );
}
throw new PluginConfigurationException( mojoDescriptor.getPluginDescriptor(), e.getClass().getName() + ": "
+ e.getMessage(), new ComponentConfigurationException( e ) );
} }
finally finally
{ {

View File

@ -118,11 +118,11 @@ public void buildExtensionsAssembled( ModelBuildingEvent event )
} }
catch ( PluginResolutionException e ) catch ( PluginResolutionException e )
{ {
event.getProblems().addError( "Unresolveable build extensions: " + e.getMessage(), e ); event.getProblems().addError( "Unresolveable build extension: " + e.getMessage(), e );
} }
catch ( PluginVersionResolutionException e ) catch ( PluginVersionResolutionException e )
{ {
event.getProblems().addError( "Unresolveable build extensions: " + e.getMessage(), e ); event.getProblems().addError( "Unresolveable build extension: " + e.getMessage(), e );
} }
if ( project.getClassRealm() != null ) if ( project.getClassRealm() != null )

View File

@ -21,7 +21,9 @@
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -29,6 +31,8 @@
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.apache.maven.Maven; import org.apache.maven.Maven;
import org.apache.maven.exception.DefaultExceptionHandler;
import org.apache.maven.exception.ExceptionHandler;
import org.apache.maven.exception.ExceptionSummary; import org.apache.maven.exception.ExceptionSummary;
import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.DefaultMavenExecutionResult;
@ -37,7 +41,6 @@
import org.apache.maven.execution.MavenExecutionRequestPopulator; import org.apache.maven.execution.MavenExecutionRequestPopulator;
import org.apache.maven.execution.MavenExecutionResult; import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.model.building.ModelProcessor; import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.model.locator.ModelLocator;
import org.apache.maven.repository.ArtifactTransferListener; import org.apache.maven.repository.ArtifactTransferListener;
import org.apache.maven.settings.Settings; import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
@ -452,35 +455,53 @@ else if ( commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) )
// The exception handling should be handled in Maven itself. // The exception handling should be handled in Maven itself.
try try
{
return processResult( request, result, showErrors );
}
finally
{
if ( fileStream != null )
{
fileStream.close();
}
}
}
private int processResult( MavenExecutionRequest request, MavenExecutionResult result, boolean showErrors )
{ {
if ( result.hasExceptions() ) if ( result.hasExceptions() )
{ {
ExceptionSummary es = result.getExceptionSummary(); ExceptionHandler handler = new DefaultExceptionHandler();
if ( es == null ) Map<String, String> references = new LinkedHashMap<String, String>();
for ( Throwable exception : result.getExceptions() )
{ {
logger.error( "", result.getExceptions().get( 0 ) ); ExceptionSummary summary = handler.handleException( exception );
}
else logSummary( summary, references, "", showErrors );
{
if ( showErrors )
{
logger.error( es.getMessage(), es.getException() );
}
else
{
logger.error( es.getMessage() );
logger.error( "To see the full stack trace of the error, re-run Maven with the -e switch." );
} }
logger.error( "" );
if ( !showErrors )
{
logger.error( "To see the full stack trace of the errors, re-run Maven with the -e switch." );
}
if ( !logger.isDebugEnabled() )
{
logger.error( "Re-run Maven using the -X switch to enable full debug logging." ); logger.error( "Re-run Maven using the -X switch to enable full debug logging." );
}
if ( StringUtils.isNotEmpty( es.getReference() ) ) if ( !references.isEmpty() )
{ {
logger.error( "" ); logger.error( "" );
logger.error( "For more information about the error and possible solutions" logger.error( "For more information about the errors and possible solutions"
+ ", please try the following article:" ); + ", please read the following articles:" );
logger.error( " " + es.getReference() );
for ( Map.Entry<String, String> entry : references.entrySet() )
{
logger.error( entry.getValue() + " " + entry.getKey() );
} }
} }
@ -500,13 +521,36 @@ else if ( commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) )
return 0; return 0;
} }
} }
finally
private void logSummary( ExceptionSummary summary, Map<String, String> references, String indent, boolean showErrors )
{ {
if ( fileStream != null ) String referenceKey = "";
if ( StringUtils.isNotEmpty( summary.getReference() ) )
{ {
fileStream.close(); referenceKey = references.get( summary.getReference() );
if ( referenceKey == null )
{
referenceKey = "[" + references.size() + "]";
references.put( summary.getReference(), referenceKey );
} }
} }
if ( showErrors )
{
logger.error( indent + referenceKey, summary.getException() );
}
else
{
logger.error( indent + summary.getMessage() + " " + referenceKey );
}
indent += " ";
for ( ExceptionSummary child : summary.getChildren() )
{
logSummary( child, references, indent, showErrors );
}
} }
protected ModelProcessor createModelProcessor( PlexusContainer container ) protected ModelProcessor createModelProcessor( PlexusContainer container )