Generalized error diagnosis for use outside of maven-core. This allows us to print diagnostic messages in the Ant tasks, for instance (these modifications are also included), and share diagnosers between multiple uses (maven-core, maven-artifact-ant, for example). This should help with MNG-784, but I'll verify that before I close it.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@314804 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-10-12 06:46:29 +00:00
parent 0497cc28e7
commit c933da0504
24 changed files with 545 additions and 311 deletions

View File

@ -46,6 +46,15 @@
<version>1.0-alpha-5-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-error-diagnostics</artifactId>
<version>2.0-beta-4-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>

View File

@ -31,6 +31,7 @@ import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.apache.maven.usability.diagnostics.ErrorDiagnostics;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
@ -367,6 +368,29 @@ public abstract class AbstractArtifactTask
return pom;
}
public void diagnoseError( Throwable error )
{
try
{
ErrorDiagnostics diagnostics = (ErrorDiagnostics) embedder.lookup( ErrorDiagnostics.ROLE );
StringBuffer message = new StringBuffer();
message.append( "An error has occurred while processing the Maven artifact tasks.\n" );
message.append( " Diagnosis:\n\n" );
message.append( diagnostics.diagnose( error ) );
message.append( "\n\n" );
log( message.toString(), Project.MSG_INFO );
}
catch ( ComponentLookupException e )
{
log( "Failed to retrieve error diagnoser.", Project.MSG_DEBUG );
}
}
public void addPom( Pom pom )
{
this.pom = pom;

View File

@ -74,6 +74,8 @@ public class DependenciesTask
* @noinspection RefusedBequest
*/
public void execute()
{
try
{
ArtifactRepository localRepo = createLocalArtifactRepository();
@ -131,8 +133,8 @@ public class DependenciesTask
ArtifactResolutionResult result;
try
{
Artifact pomArtifact = artifactFactory.createBuildArtifact( pom.getGroupId(), pom.getArtifactId(),
pom.getVersion(), pom.getPackaging() );
Artifact pomArtifact = artifactFactory.createBuildArtifact( pom.getGroupId(), pom.getArtifactId(), pom
.getVersion(), pom.getPackaging() );
List listeners = Collections.EMPTY_LIST;
if ( verbose )
@ -232,6 +234,13 @@ public class DependenciesTask
getProject().addReference( filesetId, fileSet );
}
}
catch ( BuildException e )
{
diagnoseError( e );
throw e;
}
}
private List createRemoteArtifactRepositories( List remoteRepositories )
{

View File

@ -44,6 +44,8 @@ public class DeployTask
private File file;
public void execute()
{
try
{
ArtifactRepository localRepo = createLocalArtifactRepository();
MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
@ -65,8 +67,8 @@ public class DeployTask
{
if ( distributionManagement.getSnapshotRepository() != null )
{
remoteSnapshotRepository = createAntRemoteRepositoryBase(
distributionManagement.getSnapshotRepository() );
remoteSnapshotRepository = createAntRemoteRepositoryBase( distributionManagement
.getSnapshotRepository() );
}
if ( distributionManagement.getRepository() != null )
{
@ -122,6 +124,13 @@ public class DeployTask
throw new BuildException( "Error deploying artifact", e );
}
}
catch ( BuildException e )
{
diagnoseError( e );
throw e;
}
}
public RemoteRepository getRemoteRepository()
{

View File

@ -40,6 +40,8 @@ public class InstallTask
private File file;
public void execute()
{
try
{
ArtifactRepository localRepo = createLocalArtifactRepository();
@ -73,6 +75,13 @@ public class InstallTask
throw new BuildException( "Error installing artifact", e );
}
}
catch ( BuildException e )
{
diagnoseError( e );
throw e;
}
}
public File getFile()
{

View File

@ -69,6 +69,8 @@ public class InstallWagonProviderTask
public void execute()
throws BuildException
{
try
{
MavenMetadataSource metadataSource = (MavenMetadataSource) lookup( ArtifactMetadataSource.ROLE );
@ -81,10 +83,11 @@ public class InstallWagonProviderTask
VersionRange versionRange = VersionRange.createFromVersionSpec( version );
Artifact providerArtifact = factory.createExtensionArtifact( "org.apache.maven.wagon", artifactId,
versionRange );
ArtifactResolutionResult result = resolver.resolveTransitively( Collections.singleton( providerArtifact ),
createArtifact( createDummyPom() ),
ArtifactResolutionResult result = resolver.resolveTransitively( Collections
.singleton( providerArtifact ), createArtifact( createDummyPom() ),
createLocalArtifactRepository(),
remoteRepositories, metadataSource, null );
remoteRepositories, metadataSource,
null );
log( "Installing provider: " + providerArtifact );
@ -110,6 +113,13 @@ public class InstallWagonProviderTask
{
throw new BuildException( "Unable to locate wagon provider in remote repository", e );
}
}
catch ( BuildException e )
{
diagnoseError( e );
throw e;
}
}
}

View File

@ -67,6 +67,11 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-error-diagnostics</artifactId>
<version>2.0-beta-4-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-provider-api</artifactId>

View File

@ -44,8 +44,8 @@ import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.SettingsUtils;
import org.apache.maven.usability.DiagnosisUtils;
import org.apache.maven.usability.ErrorDiagnoser;
import org.apache.maven.usability.SystemWarnings;
import org.apache.maven.usability.diagnostics.ErrorDiagnostics;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
@ -66,7 +66,6 @@ import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
/**
@ -90,7 +89,7 @@ public class DefaultMaven
protected PlexusContainer container;
protected Map errorDiagnosers;
protected ErrorDiagnostics errorDiagnostics;
protected RuntimeInformation runtimeInformation;
@ -109,7 +108,7 @@ public class DefaultMaven
{
if ( request.getSettings().isOffline() )
{
getLogger().info( DiagnosisUtils.getOfflineWarning() );
getLogger().info( SystemWarnings.getOfflineWarning() );
WagonManager wagonManager = null;
@ -650,20 +649,9 @@ public class DefaultMaven
private void diagnoseError( Throwable error )
{
String message = null;
if ( errorDiagnosers != null )
if ( errorDiagnostics != null )
{
for ( Iterator it = errorDiagnosers.values().iterator(); it.hasNext(); )
{
ErrorDiagnoser diagnoser = (ErrorDiagnoser) it.next();
if ( diagnoser.canDiagnose( error ) )
{
message = diagnoser.diagnose( error );
// first one wins.
break;
}
}
message = errorDiagnostics.diagnose( error );
}
if ( message == null )
@ -698,20 +686,9 @@ public class DefaultMaven
line();
String message = null;
if ( errorDiagnosers != null )
if ( errorDiagnostics != null )
{
for ( Iterator it = errorDiagnosers.values().iterator(); it.hasNext(); )
{
ErrorDiagnoser diagnoser = (ErrorDiagnoser) it.next();
if ( diagnoser.canDiagnose( error ) )
{
message = diagnoser.diagnose( error );
// first one wins.
break;
}
}
message = errorDiagnostics.diagnose( error );
}
if ( message == null )

View File

@ -18,6 +18,8 @@ package org.apache.maven.usability;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
public class ArtifactNotFoundDiagnoser
implements ErrorDiagnoser
@ -46,7 +48,7 @@ public class ArtifactNotFoundDiagnoser
if ( !wagonManager.isOnline() )
{
message.append( "\n" ).append( DiagnosisUtils.getOfflineWarning() );
message.append( "\n" ).append( SystemWarnings.getOfflineWarning() );
}
Throwable root = DiagnosisUtils.getRootCause( exception );

View File

@ -18,6 +18,8 @@ package org.apache.maven.usability;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
public class ArtifactResolverDiagnoser
implements ErrorDiagnoser
@ -43,7 +45,7 @@ public class ArtifactResolverDiagnoser
if ( !wagonManager.isOnline() )
{
message.append( "\n" ).append( DiagnosisUtils.getOfflineWarning() );
message.append( "\n" ).append( SystemWarnings.getOfflineWarning() );
}
Throwable root = DiagnosisUtils.getRootCause( exception );

View File

@ -1,6 +1,7 @@
package org.apache.maven.usability;
import org.apache.maven.artifact.InvalidArtifactRTException;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
/*
* Copyright 2001-2005 The Apache Software Foundation.

View File

@ -1,6 +1,8 @@
package org.apache.maven.usability;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
public class MojoExecutionExceptionDiagnoser
implements ErrorDiagnoser

View File

@ -21,6 +21,8 @@ import org.apache.maven.plugin.PluginParameterException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
import org.apache.maven.usability.plugin.Expression;
import org.apache.maven.usability.plugin.ExpressionDocumentationException;
import org.apache.maven.usability.plugin.ExpressionDocumenter;

View File

@ -2,6 +2,8 @@ package org.apache.maven.usability;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.PluginContainerException;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
public class PluginContainerDiagnoser
implements ErrorDiagnoser
@ -31,7 +33,7 @@ public class PluginContainerDiagnoser
if ( originalMessage.startsWith( "Cannot resolve artifact" ) )
{
message.append( DiagnosisUtils.getOfflineWarning() );
message.append( SystemWarnings.getOfflineWarning() );
}
else
{

View File

@ -1,6 +1,8 @@
package org.apache.maven.usability;
import org.apache.maven.profiles.activation.ProfileActivationException;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
public class ProfileActivationDiagnoser

View File

@ -3,6 +3,8 @@ package org.apache.maven.usability;
import org.apache.maven.project.InvalidProjectModelException;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.validation.ModelValidationResult;
import org.apache.maven.usability.diagnostics.DiagnosisUtils;
import org.apache.maven.usability.diagnostics.ErrorDiagnoser;
public class ProjectBuildDiagnoser
implements ErrorDiagnoser

View File

@ -0,0 +1,12 @@
package org.apache.maven.usability;
public class SystemWarnings
{
public static String getOfflineWarning()
{
return "\nNOTE: Maven is executing in offline mode. Any artifacts not already in your local\n" +
"repository will be inaccessible.\n";
}
}

View File

@ -78,8 +78,7 @@
<role>org.apache.maven.lifecycle.LifecycleExecutor</role>
</requirement>
<requirement>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<field-name>errorDiagnosers</field-name>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnostics</role>
</requirement>
<requirement>
<role>org.apache.maven.execution.RuntimeInformation</role>
@ -97,7 +96,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>MojoExecutionExceptionDiagnoser</role-hint>
<implementation>org.apache.maven.usability.MojoExecutionExceptionDiagnoser</implementation>
</component>
@ -107,7 +106,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>ProjectBuildDiagnoser</role-hint>
<implementation>org.apache.maven.usability.ProjectBuildDiagnoser</implementation>
</component>
@ -117,7 +116,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>ProfileActivationDiagnoser</role-hint>
<implementation>org.apache.maven.usability.ProfileActivationDiagnoser</implementation>
</component>
@ -127,7 +126,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>PluginConfigurationDiagnoser</role-hint>
<implementation>org.apache.maven.usability.PluginConfigurationDiagnoser</implementation>
</component>
@ -137,7 +136,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>PluginContainerDiagnoser</role-hint>
<implementation>org.apache.maven.usability.PluginContainerDiagnoser</implementation>
</component>
@ -147,7 +146,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>ArtifactNotFoundDiagnoser</role-hint>
<implementation>org.apache.maven.usability.ArtifactNotFoundDiagnoser</implementation>
<requirements>
@ -162,7 +161,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>ArtifactResolverDiagnoser</role-hint>
<implementation>org.apache.maven.usability.ArtifactResolverDiagnoser</implementation>
<requirements>
@ -177,7 +176,7 @@
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnoser</role>
<role-hint>InvalidArtifactDiagnoser</role-hint>
<implementation>org.apache.maven.usability.InvalidArtifactDiagnoser</implementation>
</component>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 ../../../m2-subclipse/maven-site/target/site/maven-v4_0_0.xsd ">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven</artifactId>
<version>2.0-beta-4-SNAPSHOT</version>
</parent>
<artifactId>maven-error-diagnostics</artifactId>
<name>Maven Error Diagnostics</name>
<description>
Provides a manager component which will process a given Throwable instance through a set of diagnostic
sub-components, and return a String message with user-friendly information about the error and possibly
how to fix it.
</description>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +1,4 @@
package org.apache.maven.usability;
package org.apache.maven.usability.diagnostics;
/*
@ -23,12 +23,6 @@ public final class DiagnosisUtils
{
}
public static String getOfflineWarning()
{
return "\nNOTE: Maven is executing in offline mode. Any artifacts not already in your local\n" +
"repository will be inaccessible.\n";
}
public static boolean containsInCausality( Throwable error, Class test )
{
Throwable cause = error;

View File

@ -1,4 +1,4 @@
package org.apache.maven.usability;
package org.apache.maven.usability.diagnostics;
/*
* Copyright 2001-2005 The Apache Software Foundation.

View File

@ -0,0 +1,129 @@
package org.apache.maven.usability.diagnostics;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import java.util.Iterator;
import java.util.List;
public class ErrorDiagnostics
extends AbstractLogEnabled
implements Contextualizable
{
public static final String ROLE = ErrorDiagnostics.class.getName();
private PlexusContainer container;
private List errorDiagnosers;
public void setErrorDiagnosers( List errorDiagnosers )
{
this.errorDiagnosers = errorDiagnosers;
}
public String diagnose( Throwable error )
{
List diags = errorDiagnosers;
boolean releaseDiags = false;
boolean errorProcessed = false;
String message = null;
try
{
if ( diags == null )
{
releaseDiags = true;
try
{
diags = container.lookupList( ErrorDiagnoser.ROLE );
}
catch ( ComponentLookupException e )
{
getLogger().error( "Failed to lookup the list of error diagnosers.", e );
}
}
if ( diags != null )
{
for ( Iterator it = diags.iterator(); it.hasNext(); )
{
ErrorDiagnoser diagnoser = (ErrorDiagnoser) it.next();
if ( diagnoser.canDiagnose( error ) )
{
errorProcessed = true;
message = diagnose( error );
break;
}
}
}
}
finally
{
if ( releaseDiags && diags != null )
{
try
{
container.releaseAll( diags );
}
catch ( ComponentLifecycleException e )
{
getLogger().debug( "Failed to release error diagnoser list.", e );
}
}
if ( !errorProcessed )
{
message = new PuntErrorDiagnoser().diagnose( error );
}
}
return message;
}
public void contextualize( Context context )
throws ContextException
{
this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}
private static class PuntErrorDiagnoser implements ErrorDiagnoser
{
public boolean canDiagnose( Throwable error )
{
return true;
}
public String diagnose( Throwable error )
{
StringBuffer message = new StringBuffer();
message.append( "Error: " ).append( error.getClass().getName() );
message.append( "\nMessage: " ).append( error.getMessage() );
Throwable root = DiagnosisUtils.getRootCause( error );
if ( root != null && root != error )
{
message.append( "\n\nRoot Cause\n\n" );
message.append( "Error: " ).append( root.getClass().getName() );
message.append( "\nMessage: " ).append( root.getMessage() );
}
return message.toString();
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<component-set>
<components>
<component>
<role>org.apache.maven.usability.diagnostics.ErrorDiagnostics</role>
<implementation>org.apache.maven.usability.diagnostics.ErrorDiagnostics</implementation>
</component>
</components>
</component-set>

View File

@ -42,7 +42,7 @@ public class MBoot
"maven-artifact", "maven-plugin-descriptor", "maven-repository-metadata", "maven-artifact-manager",
"maven-artifact-test", "maven-script/maven-script-beanshell", "maven-profile", "maven-project",
"maven-plugin-registry", "maven-reporting/maven-reporting-api", "maven-reporting/maven-reporting-impl",
"maven-plugin-parameter-documenter", "maven-core", "maven-archiver",
"maven-plugin-parameter-documenter", "maven-error-diagnostics", "maven-core", "maven-archiver",
"maven-plugin-tools/maven-plugin-tools-api", "maven-plugin-tools/maven-plugin-tools-java",
"maven-plugin-tools/maven-plugin-tools-beanshell", "maven-plugin-tools/maven-plugin-tools-pluggy",
"maven-core-it-verifier"};