diff --git a/maven-core/src/main/java/org/apache/maven/usability/ArtifactResolverDiagnoser.java b/maven-core/src/main/java/org/apache/maven/usability/ArtifactResolverDiagnoser.java index 7a5757283f..0f4832a5a4 100644 --- a/maven-core/src/main/java/org/apache/maven/usability/ArtifactResolverDiagnoser.java +++ b/maven-core/src/main/java/org/apache/maven/usability/ArtifactResolverDiagnoser.java @@ -31,7 +31,7 @@ public class ArtifactResolverDiagnoser public String diagnose( Throwable error ) { - Throwable root = traverseToRoot( error ); + Throwable root = DiagnosisUtils.getRootCause( error ); String message = null; @@ -39,7 +39,7 @@ public class ArtifactResolverDiagnoser { StringBuffer messageBuffer = new StringBuffer(); - if ( causalityChainContains( error, TransitiveArtifactResolutionException.class ) ) + if ( DiagnosisUtils.containsInCausality( error, TransitiveArtifactResolutionException.class ) ) { messageBuffer.append( "Error while transitively resolving artifacts (transitive path trace currently unavailable):\n\n" ); @@ -67,36 +67,4 @@ public class ArtifactResolverDiagnoser return message; } - private boolean causalityChainContains( Throwable error, Class errorClass ) - { - Throwable cause = error; - - boolean contains = false; - - while ( cause != null ) - { - if ( errorClass.isInstance( cause ) ) - { - contains = true; - break; - } - - cause = cause.getCause(); - } - - return contains; - } - - private Throwable traverseToRoot( Throwable error ) - { - Throwable potentialRoot = error; - - while ( potentialRoot.getCause() != null ) - { - potentialRoot = potentialRoot.getCause(); - } - - return potentialRoot; - } - } diff --git a/maven-core/src/main/java/org/apache/maven/usability/DiagnosisUtils.java b/maven-core/src/main/java/org/apache/maven/usability/DiagnosisUtils.java new file mode 100644 index 0000000000..e927de936a --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/usability/DiagnosisUtils.java @@ -0,0 +1,80 @@ +package org.apache.maven.usability; + + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +public final class DiagnosisUtils +{ + private DiagnosisUtils() + { + } + + public static boolean containsInCausality( Throwable error, Class test ) + { + Throwable cause = error; + + while( cause != null ) + { + if( test.isInstance( cause ) ) + { + return true; + } + + cause = cause.getCause(); + } + + return false; + } + + public static Throwable getRootCause( Throwable error ) + { + Throwable cause = error; + + while( true ) + { + Throwable nextCause = cause.getCause(); + + if( nextCause == null ) + { + break; + } + else + { + cause = nextCause; + } + } + + return cause; + } + + public static Throwable getFromCausality( Throwable error, Class targetClass ) + { + Throwable cause = error; + + while( cause != null ) + { + if( targetClass.isInstance( cause ) ) + { + return cause; + } + + cause = cause.getCause(); + } + + return null; + } +} diff --git a/maven-core/src/main/java/org/apache/maven/usability/PluginConfigurationDiagnoser.java b/maven-core/src/main/java/org/apache/maven/usability/PluginConfigurationDiagnoser.java index f9730af17b..e5f53a3302 100644 --- a/maven-core/src/main/java/org/apache/maven/usability/PluginConfigurationDiagnoser.java +++ b/maven-core/src/main/java/org/apache/maven/usability/PluginConfigurationDiagnoser.java @@ -20,6 +20,7 @@ import org.apache.maven.plugin.PluginConfigurationException; import org.apache.maven.plugin.PluginParameterException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.Parameter; +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; import org.codehaus.plexus.util.StringUtils; import java.util.ArrayList; @@ -58,7 +59,12 @@ public class PluginConfigurationDiagnoser { PluginParameterException exception = (PluginParameterException) error; - return buildDiagnosticMessage( exception ); + return buildParameterDiagnosticMessage( exception ); + } + else if( DiagnosisUtils.containsInCausality(error, ComponentConfigurationException.class ) ) + { + ComponentConfigurationException cce = (ComponentConfigurationException) DiagnosisUtils.getFromCausality( error, ComponentConfigurationException.class ); + return buildInvalidPluginConfigurationDiagnosisMessage( cce ); } else { @@ -66,7 +72,18 @@ public class PluginConfigurationDiagnoser } } - private String buildDiagnosticMessage( PluginParameterException exception ) + private String buildInvalidPluginConfigurationDiagnosisMessage( ComponentConfigurationException cce ) + { + StringBuffer message = new StringBuffer(); + + message.append( "Either your POM or one of its ancestors has declared an invalid plugin configuration.\n" ) + .append( "The error message is:\n\n" ) + .append( cce.getMessage() ).append( "\n" ); + + return message.toString(); + } + + private String buildParameterDiagnosticMessage( PluginParameterException exception ) { StringBuffer messageBuffer = new StringBuffer(); diff --git a/maven-core/src/test/java/org/apache/maven/usability/PluginErrorDiagnoserTest.java b/maven-core/src/test/java/org/apache/maven/usability/PluginErrorDiagnoserTest.java index 98c0ace45e..d71848e0b0 100644 --- a/maven-core/src/test/java/org/apache/maven/usability/PluginErrorDiagnoserTest.java +++ b/maven-core/src/test/java/org/apache/maven/usability/PluginErrorDiagnoserTest.java @@ -17,11 +17,14 @@ package org.apache.maven.usability; */ import junit.framework.TestCase; + +import org.apache.maven.plugin.PluginConfigurationException; import org.apache.maven.plugin.PluginParameterException; import org.apache.maven.plugin.descriptor.DuplicateParameterException; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; import java.util.ArrayList; import java.util.Collections; @@ -30,6 +33,8 @@ import java.util.List; public class PluginErrorDiagnoserTest extends TestCase { + + private PluginConfigurationDiagnoser diagnoser = new PluginConfigurationDiagnoser(); private PluginParameterException buildException( String prefix, String goal, List params ) throws DuplicateParameterException @@ -49,6 +54,22 @@ public class PluginErrorDiagnoserTest return new PluginParameterException( mojoDescriptor, params ); } + + public void testShouldDiagnoseInvalidPluginConfiguration() + { + printMethodHeader(); + + ComponentConfigurationException cce = new ComponentConfigurationException( "Class \'org.apache.maven.plugin.jar.JarMojo\' does not contain a field named \'addClasspath\'" ); + PluginConfigurationException pce = new PluginConfigurationException( "test", cce ); + + assertTrue( diagnoser.canDiagnose( pce ) ); + + String userMessage = diagnoser.diagnose( pce ); + + System.out.println( userMessage ); + + assertNotNull( userMessage ); + } public void testShouldBeAbleToDiagnosePluginParameterExceptions() throws DuplicateParameterException @@ -61,7 +82,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - assertTrue( new PluginConfigurationDiagnoser().canDiagnose( error ) ); + assertTrue( diagnoser.canDiagnose( error ) ); } public void testParamWithOneReportsExpressionAndOneProjectBasedExpression() @@ -93,7 +114,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", params ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage ); @@ -113,7 +134,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage ); @@ -132,7 +153,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage ); @@ -152,7 +173,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage ); @@ -172,7 +193,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage ); @@ -192,7 +213,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage ); @@ -212,7 +233,7 @@ public class PluginErrorDiagnoserTest PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); - String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); + String userMessage = diagnoser.diagnose( error ); System.out.println( userMessage );