MNG-2728 return a MavenExecutionResult from Maven.execute( request )

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@491512 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2007-01-01 00:18:12 +00:00
parent cd1c9e7b29
commit 8f4a420c8d
10 changed files with 167 additions and 155 deletions

View File

@ -23,7 +23,7 @@
import org.apache.maven.embedder.MavenEmbedderException;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.reactor.MavenExecutionException;
import org.apache.maven.execution.MavenExecutionResult;
import org.codehaus.plexus.classworlds.ClassWorld;
import java.io.File;
@ -177,8 +177,6 @@ else if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_UPDATES ) )
//
// ----------------------------------------------------------------------
try
{
List goals = commandLine.getArgList();
boolean recursive = true;
@ -295,48 +293,11 @@ else if ( profileAction.startsWith( "+" ) )
alternatePomFile = commandLine.getOptionValue( CLIManager.ALTERNATE_POM_FILE );
}
// ----------------------------------------------------------------------
// From here we are CLI free
// ----------------------------------------------------------------------
// -> baseDirectory
// -> goals
// -> debug: use to set the threshold on the logger manager
// -> active profiles (settings)
// -> inactive profiles (settings)
// -> offline (settings)
// -> interactive (settings)
// -> Settings
// -> localRepository
// -> interactiveMode
// -> usePluginRegistry
// -> offline
// -> proxies
// -> servers
// -> mirrors
// -> profiles
// -> activeProfiles
// -> pluginGroups
// -> executionProperties
// -> reactorFailureBehaviour: fail fast, fail at end, fail never
// -> globalChecksumPolicy: fail, warn
// -> showErrors (this is really CLI is but used inside Maven internals
// -> recursive
// -> updateSnapshots
// -> useReactor
// -> transferListener: in the CLI this is batch or console
// We have a general problem with plexus components that are singletons in that they use
// the same logger for their lifespan. This is not good in that many requests may be fired
// off and the singleton plexus component will continue to funnel their output to the same
// logger. We need to be able to swap the logger.
// the local repository should just be a path and we should look here:
// in the system property
// user specified settings.xml
// default ~/.m2/settings.xml
// and with that maven internals should contruct the ArtifactRepository object
int loggingLevel;
if ( debug )
@ -382,11 +343,11 @@ else if ( quiet )
.setNoSnapshotUpdates( noSnapshotUpdates ) // default: false
.setGlobalChecksumPolicy( globalChecksumPolicy ); // default: warn
mavenEmbedder.execute( request );
}
catch ( MavenExecutionException e )
MavenExecutionResult result = mavenEmbedder.execute( request );
if ( result.hasExceptions() )
{
showFatalError( "Unable to configure the Maven application", e, showErrors );
showFatalError( "Unable to configure the Maven application", (Exception) result.getExceptions().get( 0 ), showErrors );
return 1;
}

View File

@ -29,7 +29,6 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
import org.apache.maven.execution.RuntimeInformation;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.monitor.event.DefaultEventDispatcher;
import org.apache.maven.monitor.event.DefaultEventMonitor;
@ -44,14 +43,10 @@
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.reactor.MavenExecutionException;
import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
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;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
@ -63,7 +58,6 @@
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.io.File;
import java.io.IOException;
@ -118,7 +112,6 @@ public class DefaultMaven
// ----------------------------------------------------------------------
public MavenExecutionResult execute( MavenExecutionRequest request )
throws MavenExecutionException
{
Logger logger = loggerManager.getLoggerForComponent( Mojo.ROLE );
@ -143,14 +136,16 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
dispatcher.dispatchStart( event, request.getBaseDirectory() );
ReactorManager rm;
MavenExecutionResult result;
try
result = doExecute( request, dispatcher );
if ( result.hasExceptions() )
{
rm = doExecute( request, dispatcher );
}
catch ( LifecycleExecutionException e )
for ( Iterator i = result.getExceptions().iterator(); i.hasNext(); )
{
Exception e = (Exception) i.next();
dispatcher.dispatchError( event, request.getBaseDirectory(), e );
logError( e, request.isShowErrors() );
@ -158,44 +153,19 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
stats( request.getStartTime() );
line();
throw new MavenExecutionException( e.getMessage(), e );
}
catch ( BuildFailureException e )
{
dispatcher.dispatchError( event, request.getBaseDirectory(), e );
logFailure( e, request.isShowErrors() );
stats( request.getStartTime() );
line();
throw new MavenExecutionException( e.getMessage(), e );
}
catch ( Throwable t )
{
dispatcher.dispatchError( event, request.getBaseDirectory(), t );
logFatal( t );
stats( request.getStartTime() );
line();
throw new MavenExecutionException( "Error executing project within the reactor", t );
}
// Either the build was successful, or it was a fail_at_end/fail_never reactor build
// TODO: should all the logging be left to the CLI?
logReactorSummary( rm );
logReactorSummary( result.getReactorManager() );
if ( rm.hasBuildFailures() )
if ( result.getReactorManager().hasBuildFailures() )
{
logErrors( rm, request.isShowErrors() );
logErrors( result.getReactorManager(), request.isShowErrors() );
if ( !ReactorManager.FAIL_NEVER.equals( rm.getFailureBehavior() ) )
if ( !result.getReactorManager().FAIL_NEVER.equals( result.getReactorManager().getFailureBehavior() ) )
{
dispatcher.dispatchError( event, request.getBaseDirectory(), null );
@ -207,7 +177,7 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
line();
throw new MavenExecutionException( "Some builds failed" );
return new DefaultMavenExecutionResult( Collections.singletonList( new MavenExecutionException( "Some builds failed" ) ) );
}
else
{
@ -215,7 +185,7 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
}
}
logSuccess( rm );
logSuccess( result.getReactorManager() );
stats( request.getStartTime() );
@ -223,10 +193,11 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
dispatcher.dispatchEnd( event, request.getBaseDirectory() );
return new DefaultMavenExecutionResult( rm.getTopLevelProject(), null );
return new DefaultMavenExecutionResult( result.getReactorManager() );
}
private void logErrors( ReactorManager rm, boolean showErrors )
private void logErrors( ReactorManager rm,
boolean showErrors )
{
for ( Iterator it = rm.getSortedProjects().iterator(); it.hasNext(); )
{
@ -255,9 +226,11 @@ private void logErrors( ReactorManager rm, boolean showErrors )
}
}
private ReactorManager doExecute( MavenExecutionRequest request, EventDispatcher dispatcher )
throws MavenExecutionException, BuildFailureException, LifecycleExecutionException
private MavenExecutionResult doExecute( MavenExecutionRequest request,
EventDispatcher dispatcher )
{
List executionExceptions = new ArrayList();
ProfileManager globalProfileManager = new DefaultProfileManager( container, request.getProperties() );
globalProfileManager.loadSettingsProfiles( request.getSettings() );
@ -269,14 +242,29 @@ private ReactorManager doExecute( MavenExecutionRequest request, EventDispatcher
getLogger().info( "Scanning for projects..." );
boolean foundProjects = true;
List projects = getProjects( request, globalProfileManager );
List projects;
try
{
projects = getProjects( request, globalProfileManager );
if ( projects.isEmpty() )
{
projects.add( getSuperProject( request ) );
foundProjects = false;
}
}
catch ( Exception e )
{
executionExceptions.add( e );
return new DefaultMavenExecutionResult( executionExceptions );
}
ReactorManager rm;
try
{
rm = new ReactorManager( projects );
@ -290,12 +278,16 @@ private ReactorManager doExecute( MavenExecutionRequest request, EventDispatcher
}
catch ( CycleDetectedException e )
{
throw new BuildFailureException(
"The projects in the reactor contain a cyclic reference: " + e.getMessage(), e );
executionExceptions.add( new BuildFailureException(
"The projects in the reactor contain a cyclic reference: " + e.getMessage(), e ) );
return new DefaultMavenExecutionResult( executionExceptions );
}
catch ( DuplicateProjectException e )
{
throw new BuildFailureException( e.getMessage(), e );
executionExceptions.add( new BuildFailureException( e.getMessage(), e ) );
return new DefaultMavenExecutionResult( executionExceptions );
}
if ( rm.hasMultipleProjects() )
@ -305,6 +297,7 @@ private ReactorManager doExecute( MavenExecutionRequest request, EventDispatcher
for ( Iterator i = rm.getSortedProjects().iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
getLogger().info( " " + project.getName() );
}
}
@ -313,9 +306,16 @@ private ReactorManager doExecute( MavenExecutionRequest request, EventDispatcher
session.setUsingPOMsFromFilesystem( foundProjects );
try
{
lifecycleExecutor.execute( session, rm, dispatcher );
}
catch ( Exception e )
{
executionExceptions.add( new BuildFailureException( e.getMessage(), e ) );
}
return rm;
return new DefaultMavenExecutionResult( executionExceptions, rm );
}
private MavenProject getSuperProject( MavenExecutionRequest request )
@ -325,7 +325,8 @@ private MavenProject getSuperProject( MavenExecutionRequest request )
try
{
superProject = projectBuilder.buildStandaloneSuperProject( request.getLocalRepository(),
new DefaultProfileManager( container, request.getProperties()) );
new DefaultProfileManager( container,
request.getProperties() ) );
}
catch ( ProjectBuildingException e )
@ -335,7 +336,8 @@ private MavenProject getSuperProject( MavenExecutionRequest request )
return superProject;
}
private List getProjects( MavenExecutionRequest request, ProfileManager globalProfileManager )
private List getProjects( MavenExecutionRequest request,
ProfileManager globalProfileManager )
throws MavenExecutionException, BuildFailureException
{
List projects;
@ -343,12 +345,8 @@ private List getProjects( MavenExecutionRequest request, ProfileManager globalPr
{
List files = getProjectFiles( request );
projects = collectProjects( files,
request.getLocalRepository(),
request.isRecursive(),
request.getSettings(),
globalProfileManager,
!request.useReactor() );
projects = collectProjects( files, request.getLocalRepository(), request.isRecursive(),
request.getSettings(), globalProfileManager, !request.useReactor() );
}
catch ( IOException e )
@ -370,12 +368,15 @@ private List getProjects( MavenExecutionRequest request, ProfileManager globalPr
return projects;
}
private void logReactorSummaryLine( String name, String status )
private void logReactorSummaryLine( String name,
String status )
{
logReactorSummaryLine( name, status, -1 );
}
private void logReactorSummaryLine( String name, String status, long time )
private void logReactorSummaryLine( String name,
String status,
long time )
{
StringBuffer messageBuffer = new StringBuffer();
@ -424,8 +425,12 @@ private static String getFormattedTime( long time )
return fmt.format( new Date( time ) );
}
private List collectProjects( List files, ArtifactRepository localRepository, boolean recursive, Settings settings,
ProfileManager globalProfileManager, boolean isRoot )
private List collectProjects( List files,
ArtifactRepository localRepository,
boolean recursive,
Settings settings,
ProfileManager globalProfileManager,
boolean isRoot )
throws ArtifactResolutionException, ProjectBuildingException, ProfileActivationException,
MavenExecutionException, BuildFailureException
{
@ -498,7 +503,9 @@ private List collectProjects( List files, ArtifactRepository localRepository, bo
return projects;
}
public MavenProject getProject( File pom, ArtifactRepository localRepository, Settings settings,
public MavenProject getProject( File pom,
ArtifactRepository localRepository,
Settings settings,
ProfileManager globalProfileManager )
throws ProjectBuildingException, ArtifactResolutionException, ProfileActivationException
{
@ -523,15 +530,12 @@ public MavenProject getProject( File pom, ArtifactRepository localRepository, Se
// the session type would be specific to the request i.e. having a project
// or not.
protected MavenSession createSession( MavenExecutionRequest request, ReactorManager rpm, EventDispatcher dispatcher )
protected MavenSession createSession( MavenExecutionRequest request,
ReactorManager rpm,
EventDispatcher dispatcher )
{
return new MavenSession( container,
request.getSettings(),
request.getLocalRepository(),
dispatcher,
rpm, request.getGoals(),
request.getBaseDirectory(),
request.getProperties(),
return new MavenSession( container, request.getSettings(), request.getLocalRepository(), dispatcher, rpm,
request.getGoals(), request.getBaseDirectory(), request.getProperties(),
request.getStartTime() );
}
@ -575,7 +579,8 @@ protected void logFatal( Throwable error )
logTrace( error, true );
}
protected void logError( Exception e, boolean showErrors )
protected void logError( Exception e,
boolean showErrors )
{
line();
@ -595,7 +600,8 @@ protected void logError( Exception e, boolean showErrors )
}
}
protected void logFailure( BuildFailureException e, boolean showErrors )
protected void logFailure( BuildFailureException e,
boolean showErrors )
{
line();
@ -608,7 +614,8 @@ protected void logFailure( BuildFailureException e, boolean showErrors )
logTrace( e, showErrors );
}
private void logTrace( Throwable t, boolean showErrors )
private void logTrace( Throwable t,
boolean showErrors )
{
if ( getLogger().isDebugEnabled() )
{

View File

@ -48,6 +48,5 @@ public interface Maven
static final int LOGGING_LEVEL_DISABLE = 5;
MavenExecutionResult execute( MavenExecutionRequest request )
throws MavenExecutionException;
MavenExecutionResult execute( MavenExecutionRequest request );
}

View File

@ -3,16 +3,34 @@
import org.apache.maven.project.MavenProject;
import java.util.List;
import java.util.ArrayList;
/**
* @author Jason van Zyl
*/
/** @author Jason van Zyl */
public class DefaultMavenExecutionResult
implements MavenExecutionResult
{
private List exceptions;
private MavenProject mavenProject;
private List exceptions;
private ReactorManager reactorManager;
public DefaultMavenExecutionResult( List exceptions )
{
this.exceptions = exceptions;
}
public DefaultMavenExecutionResult( ReactorManager reactorManager )
{
this.reactorManager = reactorManager;
}
public DefaultMavenExecutionResult( List exceptions,
ReactorManager reactorManager )
{
this.reactorManager = reactorManager;
this.exceptions = exceptions;
}
public DefaultMavenExecutionResult( MavenProject project,
List exceptions )
@ -23,11 +41,36 @@ public DefaultMavenExecutionResult( MavenProject project,
public MavenProject getMavenProject()
{
if ( reactorManager != null )
{
return reactorManager.getTopLevelProject();
}
return mavenProject;
}
public ReactorManager getReactorManager()
{
return reactorManager;
}
public List getExceptions()
{
return exceptions;
}
public void addException( Throwable t )
{
if ( exceptions == null )
{
exceptions = new ArrayList();
}
exceptions.add( t );
}
public boolean hasExceptions()
{
return (exceptions != null && exceptions.size() > 0 );
}
}

View File

@ -11,10 +11,16 @@ public interface MavenExecutionResult
{
MavenProject getMavenProject();
ReactorManager getReactorManager();
// for each exception
// - knowing what artifacts are missing
// - project building exception
// - invalid project model exception: list of markers
// - xmlpull parser exception
List getExceptions();
void addException( Throwable t );
boolean hasExceptions();
}

View File

@ -63,6 +63,7 @@ public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
if ( pluginContextsByKey == null )
{
pluginContextsByKey = new HashMap();
pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
}

View File

@ -27,7 +27,7 @@
<artifactId>maven-embedder</artifactId>
<name>Maven Embedder</name>
<properties>
<bundleVersion>2.1.0.v20061231-1402</bundleVersion>
<bundleVersion>2.1.0.v20061231-1908</bundleVersion>
</properties>
<build>
<resources>

View File

@ -537,7 +537,6 @@ public void stop()
// ----------------------------------------------------------------------
public MavenExecutionResult execute( MavenExecutionRequest request )
throws MavenExecutionException
{
try
{
@ -545,7 +544,7 @@ public MavenExecutionResult execute( MavenExecutionRequest request )
}
catch ( MavenEmbedderException e )
{
throw new MavenExecutionException( "Error populating request with default values.", e );
return new DefaultMavenExecutionResult( Collections.singletonList( e ) );
}
return maven.execute( request );

View File

@ -40,15 +40,11 @@ public MavenExecutionRequest populateDefaults(MavenExecutionRequest request)
throws MavenEmbedderException
{
// Settings
// Local repository
// TransferListener
// EventMonitor
// Proxy
// Settings
if ( request.getSettings() == null )
{
// A local repository set in the request should win over what's in a settings.xml file.
File userSettingsPath = mavenTools.getUserSettingsPath( request.getSettingsFile() );
File globalSettingsFile = mavenTools.getGlobalSettingsPath();

View File

@ -54,7 +54,7 @@ public void testMavenEmbedder()
// Goal/Phase execution tests
// ----------------------------------------------------------------------
public void xtestPhaseExecution()
public void testPhaseExecution()
throws Exception
{
File testDirectory = new File( basedir, "src/test/embedder-test-project" );