[MNG-3372] Improve error handling where a mojo is referenced but the plugin doesn't contain it (in direct-invocation cases), and fix plugin-version discovery where a plugin pom gives a maven prereq.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@614708 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2008-01-23 22:18:23 +00:00
parent 22e2b4ab35
commit 268d08b8a3
9 changed files with 113 additions and 41 deletions

View File

@ -13,6 +13,7 @@ import org.apache.maven.plugin.loader.PluginLoaderException;
import org.apache.maven.plugin.loader.PluginLoader;
import org.apache.maven.plugin.PluginExecutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.model.MojoBinding;
@ -64,6 +65,13 @@ public privileged aspect LifecycleErrorReporterAspect
getReporter().reportErrorLoadingPlugin( binding, project, cause );
}
after( String task, MavenSession session, MavenProject project ) throwing ( InvalidPluginException cause ):
call( private * DefaultLifecycleExecutor.getMojoDescriptorForDirectInvocation( String, MavenSession, MavenProject ) )
&& args( task, session, project )
{
getReporter().reportInvalidPluginForDirectInvocation( task, session, project, cause );
}
after( MojoBinding binding, MavenProject project ) throwing ( MojoExecutionException cause ):
cflow( le_executeGoalAndHandleFailures( binding ) )
&& cflow( pm_executeMojo( project ) )

View File

@ -1,9 +1,6 @@
package org.apache.maven;
import org.apache.maven.lifecycle.LifecycleLoaderException;
import org.apache.maven.lifecycle.LifecycleSpecificationException;
import org.apache.maven.lifecycle.TaskValidationResult;
import org.apache.maven.plugin.loader.PluginLoaderException;
/**
* Exception which occurs when a task or goal is specified on the command line
@ -21,21 +18,7 @@ public class InvalidTaskException
private final String task;
public InvalidTaskException( TaskValidationResult result,
LifecycleLoaderException cause )
{
super( result.getMessage(), cause );
task = result.getInvalidTask();
}
public InvalidTaskException( TaskValidationResult result,
LifecycleSpecificationException cause )
{
super( result.getMessage(), cause );
task = result.getInvalidTask();
}
public InvalidTaskException( TaskValidationResult result,
PluginLoaderException cause )
Throwable cause )
{
super( result.getMessage(), cause );
task = result.getInvalidTask();

View File

@ -20,6 +20,7 @@
import org.apache.maven.lifecycle.model.MojoBinding;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@ -125,4 +126,6 @@ public interface CoreErrorReporter
void reportMissingModulePom( MissingModuleException err );
void reportInvalidPluginForDirectInvocation( String task, MavenSession session, MavenProject project, InvalidPluginException err );
}

View File

@ -6,10 +6,12 @@
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleException;
import org.apache.maven.lifecycle.model.MojoBinding;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.PluginConfigurationException;
import org.apache.maven.plugin.PluginExecutionException;
@ -307,4 +309,13 @@ public static List getMissingModuleTips( File pomFile,
return null;
}
public static List getInvalidPluginForDirectInvocationTips( String task,
MavenSession session,
MavenProject project,
InvalidPluginException err )
{
// TODO Auto-generated method stub
return null;
}
}

View File

@ -24,6 +24,7 @@
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@ -1666,4 +1667,33 @@ public void reportMissingModulePom( MissingModuleException err )
registerBuildError( err, writer.toString() );
}
public void reportInvalidPluginForDirectInvocation( String task,
MavenSession session,
MavenProject project,
InvalidPluginException err )
{
StringWriter writer = new StringWriter();
writer.write( NEWLINE );
writer.write( "Maven encountered an error while loading a plugin for use in your build." );
writer.write( NEWLINE );
writer.write( NEWLINE );
writer.write( "Original task invocation:" );
writer.write( task );
writer.write( NEWLINE );
writer.write( NEWLINE );
writer.write( "While building project:" );
writeProjectCoordinate( project, writer );
writer.write( NEWLINE );
writer.write( NEWLINE );
writer.write( "Error message:" );
writer.write( err.getMessage() );
addTips( CoreErrorTips.getInvalidPluginForDirectInvocationTips( task, session, project, err ), writer );
registerBuildError( err, writer.toString() );
}
}

View File

@ -34,6 +34,7 @@
import org.apache.maven.lifecycle.plan.BuildPlanner;
import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.monitor.event.MavenEvents;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.PluginConfigurationException;
@ -625,6 +626,12 @@ public TaskValidationResult isTaskValid( String task,
task,
message, e );
}
catch ( InvalidPluginException e )
{
return new TaskValidationResult(
task,
e.getMessage(), e );
}
}
}
@ -733,7 +740,8 @@ private List segmentTaskListByAggregationNeeds( final List tasks,
private MojoDescriptor getMojoDescriptorForDirectInvocation( String task,
MavenSession session,
MavenProject project )
throws LifecycleSpecificationException, PluginLoaderException, LifecycleLoaderException
throws LifecycleSpecificationException, PluginLoaderException, LifecycleLoaderException,
InvalidPluginException
{
// we don't need to include report configuration here, since we're just looking for
// an @aggregator flag...
@ -750,6 +758,11 @@ private MojoDescriptor getMojoDescriptorForDirectInvocation( String task,
MojoDescriptor mojoDescriptor = descriptor.getMojo( binding.getGoal() );
if ( mojoDescriptor == null )
{
throw new InvalidPluginException( "Plugin: " + descriptor.getId() + " does not contain referenced mojo: " + binding.getGoal() );
}
return mojoDescriptor;
}

View File

@ -1,6 +1,7 @@
package org.apache.maven.lifecycle;
import org.apache.maven.InvalidTaskException;
import org.apache.maven.plugin.InvalidPluginException;
import org.apache.maven.plugin.loader.PluginLoaderException;
/** @author Jason van Zyl */
@ -43,6 +44,15 @@ public TaskValidationResult( String invalidTask,
this.cause = cause;
}
public TaskValidationResult( String task,
String message,
InvalidPluginException e )
{
invalidTask = task;
this.message = message;
cause = e;
}
public String getInvalidTask()
{
return invalidTask;
@ -65,23 +75,7 @@ public boolean isTaskValid()
public InvalidTaskException generateInvalidTaskException()
{
InvalidTaskException e = null;
if ( cause instanceof LifecycleLoaderException )
{
e = new InvalidTaskException( this, (LifecycleLoaderException)cause );
}
else if ( cause instanceof LifecycleSpecificationException )
{
e = new InvalidTaskException( this, (LifecycleSpecificationException)cause );
}
else if ( cause instanceof PluginLoaderException )
{
e = new InvalidTaskException( this, (PluginLoaderException)cause );
}
else
{
throw new IllegalStateException( "No matching constructor in InvalidTaskException for TaskValidationResult cause: " + cause + " ( invalid task: " + invalidTask + ")" );
}
InvalidTaskException e = new InvalidTaskException( this, cause );
return e;
}

View File

@ -41,4 +41,9 @@ public InvalidPluginException( String message, InvalidDependencyVersionException
super( message, e );
}
public InvalidPluginException( String message )
{
super( message );
}
}

View File

@ -27,8 +27,8 @@
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.RuntimeInformation;
@ -226,13 +226,38 @@ private String resolveMetaVersion( String groupId,
// if we don't have the required Maven version, then ignore an update
if ( ( pluginProject.getPrerequisites() != null ) && ( pluginProject.getPrerequisites().getMaven() != null ) )
{
DefaultArtifactVersion requiredVersion =
new DefaultArtifactVersion( pluginProject.getPrerequisites().getMaven() );
String mavenVersion = pluginProject.getPrerequisites().getMaven();
if ( runtimeInformation.getApplicationVersion().compareTo( requiredVersion ) != 0 )
VersionRange mavenRange = null;
try
{
mavenRange = VersionRange.createFromVersionSpec( mavenVersion );
List restrictions = mavenRange.getRestrictions();
if ( ( restrictions.size() == 1 ) && Restriction.EVERYTHING.equals( restrictions.get( 0 ) ) )
{
String range = "[" + mavenVersion + ",]";
getLogger().debug( "Plugin: "
+ pluginProject.getId()
+ " specifies a simple prerequisite Maven version of: "
+ mavenVersion
+ ". This version has been translated into the range: "
+ range + " for plugin-version resolution purposes." );
mavenRange = VersionRange.createFromVersionSpec( range );
}
}
catch ( InvalidVersionSpecificationException e )
{
getLogger().debug( "Invalid prerequisite Maven version: " + mavenVersion + " for plugin: " + pluginProject.getId() +
e.getMessage() );
}
if ( ( mavenRange != null ) && !mavenRange.containsVersion( runtimeInformation.getApplicationVersion() ) )
{
getLogger().info( "Ignoring available plugin version: " + artifactVersion +
" for: " + groupId + ":" + artifactId + " as it requires Maven version " + requiredVersion );
" for: " + groupId + ":" + artifactId + " as it requires Maven version matching: " + mavenVersion );
VersionRange vr;
try