o adding a validation method to the lifecycle executor so that I can tell users what is wrong before actually trying

to execute the lifecycle. Trying to do this validation shows in detail how tangled some of our code is as I need
  to create the dispatcher in order to create the session which is required to make the reactorManager which 
  is required to get the project required to validate the goal name ... yah.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@572214 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2007-09-03 02:35:05 +00:00
parent 5c5c39e8ec
commit 2425f945fb
5 changed files with 122 additions and 50 deletions

View File

@ -37,7 +37,7 @@
import org.apache.maven.extension.BuildExtensionScanner;
import org.apache.maven.extension.ExtensionScanningException;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.lifecycle.LifecycleUtils;
import org.apache.maven.lifecycle.TaskValidationResult;
import org.apache.maven.monitor.event.DefaultEventDispatcher;
import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.monitor.event.MavenEvents;
@ -171,7 +171,16 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
{
request.setStartTime( new Date() );
initializeBuildContext( request );
MavenExecutionResult result = new DefaultMavenExecutionResult();
ReactorManager reactorManager = createReactorManager(
request,
result );
if ( result.hasExceptions() )
{
return result;
}
EventDispatcher dispatcher = new DefaultEventDispatcher( request.getEventMonitors() );
@ -181,21 +190,27 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
event,
request.getBaseDirectory() );
MavenExecutionResult result = new DefaultMavenExecutionResult();
MavenSession session = createSession(
request,
reactorManager,
dispatcher );
for ( Iterator i = request.getGoals().iterator(); i.hasNext(); )
{
String goal = (String) i.next();
TaskValidationResult tvr = lifecycleExecutor.isTaskValid( goal, session, reactorManager.getTopLevelProject() );
if ( !tvr.isTaskValid() )
{
result.addException( new BuildFailureException( tvr.getMessage() ) );
return result;
}
}
getLogger().info( "Scanning for projects..." );
ReactorManager reactorManager = createReactorManager(
request,
result );
// Check and make sure the creation of the reactor manager didn't cause a problem.
if ( result.hasExceptions() )
{
return result;
}
if ( reactorManager.hasMultipleProjects() )
{
getLogger().info( "Reactor build order: " );
@ -208,10 +223,7 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
}
}
MavenSession session = createSession(
request,
reactorManager,
dispatcher );
initializeBuildContext( request );
try
{

View File

@ -532,6 +532,53 @@ else if ( ReactorManager.FAIL_AT_END.equals( rm.getFailureBehavior() ) )
// if NEVER, don't blacklist
return false;
}
public TaskValidationResult isTaskValid( String task, MavenSession session, MavenProject rootProject )
{
if ( LifecycleUtils.isValidPhaseName( task ) )
{
return new TaskValidationResult();
}
else
{
MojoDescriptor mojo = null;
// definitely a CLI goal, can use prefix
try
{
mojo = getMojoDescriptorForDirectInvocation(
task,
session,
rootProject );
return new TaskValidationResult();
}
catch ( PluginLoaderException e )
{
// TODO: shouldn't hit this, investigate using the same resolution logic as
// others for plugins in the reactor
return new TaskValidationResult( task, "Cannot find mojo descriptor for: \'" + task
+ "\' - Treating as non-aggregator." );
}
catch ( LifecycleSpecificationException e )
{
String message =
"Invalid task '"
+ task
+ "': you must specify a valid lifecycle phase, or"
+ " a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal";
return new TaskValidationResult( task, message );
}
catch ( LifecycleLoaderException e )
{
String message = "Cannot find plugin to match task '" + task + "'.";
return new TaskValidationResult( task, message );
}
}
}
private List segmentTaskListByAggregationNeeds( final List tasks,
final MavenSession session,
@ -542,8 +589,8 @@ private List segmentTaskListByAggregationNeeds( final List tasks,
if ( rootProject != null )
{
TaskSegment currentSegment = null;
for ( Iterator it = tasks.iterator(); it.hasNext(); )
{
String task = (String) it.next();
@ -568,7 +615,7 @@ private List segmentTaskListByAggregationNeeds( final List tasks,
else
{
MojoDescriptor mojo = null;
// definitely a CLI goal, can use prefix
try
{
mojo = getMojoDescriptorForDirectInvocation(
@ -576,37 +623,10 @@ private List segmentTaskListByAggregationNeeds( final List tasks,
session,
rootProject );
}
catch ( PluginLoaderException e )
catch ( Exception e )
{
// TODO: shouldn't hit this, investigate using the same resolution logic as
// others for plugins in the reactor
getLogger().info(
"Cannot find mojo descriptor for: \'" + task
+ "\' - Treating as non-aggregator." );
getLogger().debug(
"",
e );
}
catch ( LifecycleSpecificationException e )
{
String message =
"Invalid task '"
+ task
+ "': you must specify a valid lifecycle phase, or"
+ " a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal";
throw new BuildFailureException(
message,
e );
}
catch ( LifecycleLoaderException e )
{
String message = "Cannot find plugin to match task '" + task + "'.";
throw new BuildFailureException(
message,
e );
// Won't happen as we've validated. So we need to change the code so that
// we don't have to do this.
}
// if the mojo descriptor was found, determine aggregator status according to:

View File

@ -20,6 +20,7 @@
*/
import org.apache.maven.BuildFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
import org.apache.maven.monitor.event.EventDispatcher;
@ -32,6 +33,8 @@ public interface LifecycleExecutor
{
String ROLE = LifecycleExecutor.class.getName();
TaskValidationResult isTaskValid( String task, MavenSession session, MavenProject rootProject );
void execute( MavenSession session, ReactorManager rm, EventDispatcher dispatcher )
throws LifecycleExecutionException, BuildFailureException;

View File

@ -0,0 +1,35 @@
package org.apache.maven.lifecycle;
/** @author Jason van Zyl */
public class TaskValidationResult
{
private String invalidTask;
private String message;
public TaskValidationResult()
{
}
public TaskValidationResult( String invalidTask, String message )
{
this.invalidTask = invalidTask;
this.message = message;
}
public String getInvalidTask()
{
return invalidTask;
}
public String getMessage()
{
return message;
}
public boolean isTaskValid()
{
return invalidTask == null;
}
}

View File

@ -402,7 +402,9 @@ private static void showError( String message,
Exception e,
boolean show )
{
System.err.println();
System.err.println( message );
System.err.println();
if ( show )
{