[MNG-4858] correct NullPointerException if goal name is malformed

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1005860 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2010-10-08 15:31:05 +00:00
parent 5fee94bc69
commit 1036f38593
3 changed files with 45 additions and 4 deletions

View File

@ -153,7 +153,7 @@ public MojoDescriptor getMojoDescriptor( String task, MavenSession session, Mave
int numTokens = tok.countTokens();
if ( numTokens == 4 )
if ( numTokens >= 4 )
{
// We have everything that we need
//
@ -170,6 +170,11 @@ public MojoDescriptor getMojoDescriptor( String task, MavenSession session, Mave
plugin.setVersion( tok.nextToken() );
goal = tok.nextToken();
// This won't be valid, but it constructs something easy to read in the error message
while ( tok.hasMoreTokens() )
{
goal += ":" + tok.nextToken();
}
}
else if ( numTokens == 3 )
{
@ -187,14 +192,23 @@ else if ( numTokens == 3 )
plugin.setArtifactId( tok.nextToken() );
goal = tok.nextToken();
}
else if ( numTokens == 2 )
else if ( numTokens <= 2 )
{
// We have a prefix and goal
//
// idea:idea
//
String prefix = tok.nextToken();
goal = tok.nextToken();
if ( numTokens == 2 )
{
goal = tok.nextToken();
}
else
{
// goal was missing - pass through to MojoNotFoundException
goal = "";
}
// This is the case where someone has executed a single goal from the command line
// of the form:

View File

@ -27,6 +27,7 @@
import org.apache.maven.lifecycle.internal.TaskSegment;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoNotFoundException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.xml.Xpp3Dom;
@ -310,6 +311,32 @@ MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks
mergedSegment.getTasks() );
}
public void testInvalidGoalName()
throws Exception
{
File pom = getProject( "project-basic" );
MavenSession session = createMavenSession( pom );
try
{
getExecutions( calculateExecutionPlan( session, "resources:" ) );
fail( "expected a MojoNotFoundException" );
}
catch ( MojoNotFoundException e )
{
assertEquals( "", e.getGoal() );
}
try
{
getExecutions( calculateExecutionPlan( session, "org.apache.maven.plugins:maven-resources-plugin:0.1:resources:toomany" ) );
fail( "expected a MojoNotFoundException" );
}
catch ( MojoNotFoundException e )
{
assertEquals( "resources:toomany", e.getGoal() );
}
}
public void testPluginPrefixRetrieval()
throws Exception

View File

@ -54,7 +54,7 @@ private static String toMessage( String goal, PluginDescriptor pluginDescriptor
{
StringBuilder buffer = new StringBuilder( 256 );
buffer.append( "Could not find goal " ).append( goal );
buffer.append( "Could not find goal '" ).append( goal ).append( "'" );
if ( pluginDescriptor != null )
{