[MNG-6173] MavenSession.getAllProjects() should return all projects in the reactor

This commit removes the initialization of the MavenSession object from the
DefaultGraphBuilder. The GraphBuilder implementation should only build the
graph and should not touch the MavenSession object passed into it.

To allow complete initialization of the MavenSession the ProjectDependencyGraph
interface got enhanced so that GraphBuilder implementations can communicate all
collected projects in addition to the processed list of projects.

This closes #105
This commit is contained in:
Christoph Böhme 2017-02-20 07:32:17 +01:00 committed by Christian Schulte
parent 862c84a52b
commit 114ef6c5a2
8 changed files with 108 additions and 67 deletions

View File

@ -525,7 +525,7 @@ public class DefaultMaven
{
ProjectDependencyGraph projectDependencyGraph = graphResult.get();
session.setProjects( projectDependencyGraph.getSortedProjects() );
session.setAllProjects( projectDependencyGraph.getSortedProjects() );
session.setAllProjects( projectDependencyGraph.getAllProjects() );
session.setProjectDependencyGraph( projectDependencyGraph );
}

View File

@ -32,6 +32,15 @@ import org.apache.maven.project.MavenProject;
public interface ProjectDependencyGraph
{
/**
* Gets all collected projects.
*
* @return All collected projects.
*
* @since 3.5.0-alpha-2
*/
List<MavenProject> getAllProjects();
/**
* Gets all projects in their intended build order, i.e. after topologically sorting the projects according to their
* inter-dependencies.

View File

@ -28,12 +28,12 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Lists;
import org.apache.maven.DefaultMaven;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.ProjectCycleException;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.model.Plugin;
@ -43,6 +43,7 @@ import org.apache.maven.model.building.ModelProblemUtils;
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.building.Result;
import org.apache.maven.model.building.UrlModelSource;
import org.apache.maven.project.DuplicateProjectException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
@ -54,12 +55,11 @@ import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import com.google.common.collect.Lists;
@Component( role = GraphBuilder.class, hint = GraphBuilder.HINT )
public class DefaultGraphBuilder
implements GraphBuilder
{
@Requirement
private Logger logger;
@ -69,77 +69,68 @@ public class DefaultGraphBuilder
@Override
public Result<ProjectDependencyGraph> build( MavenSession session )
{
if ( session.getProjectDependencyGraph() != null )
{
return dependencyGraph( session, session.getProjects(), false );
}
List<MavenProject> projects = session.getProjects();
if ( projects == null )
{
try
{
projects = getProjectsForMavenReactor( session );
}
catch ( ProjectBuildingException e )
{
return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, e ) ) );
}
validateProjects( projects );
return dependencyGraph( session, projects, true );
}
else
{
return dependencyGraph( session, projects, false );
}
}
private Result<ProjectDependencyGraph> dependencyGraph( MavenSession session, List<MavenProject> projects,
boolean applyMakeBehaviour )
{
MavenExecutionRequest request = session.getRequest();
ProjectDependencyGraph projectDependencyGraph = null;
try
{
projectDependencyGraph = new DefaultProjectDependencyGraph( projects );
Result<ProjectDependencyGraph> result = sessionDependencyGraph( session );
if ( applyMakeBehaviour )
if ( result == null )
{
List<MavenProject> activeProjects = projectDependencyGraph.getSortedProjects();
activeProjects = trimSelectedProjects( activeProjects, projectDependencyGraph, request );
activeProjects = trimExcludedProjects( activeProjects, request );
activeProjects = trimResumedProjects( activeProjects, request );
if ( activeProjects.size() != projectDependencyGraph.getSortedProjects().size() )
{
projectDependencyGraph =
new FilteredProjectDependencyGraph( projectDependencyGraph, activeProjects );
}
final List<MavenProject> projects = getProjectsForMavenReactor( session );
validateProjects( projects );
result = reactorDependencyGraph( session, projects );
}
return result;
}
catch ( CycleDetectedException e )
catch ( final ProjectBuildingException e )
{
return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, e ) ) );
}
catch ( final CycleDetectedException e )
{
String message = "The projects in the reactor contain a cyclic reference: " + e.getMessage();
ProjectCycleException error = new ProjectCycleException( message, e );
return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, error ) ) );
}
catch ( org.apache.maven.project.DuplicateProjectException e )
catch ( final DuplicateProjectException e )
{
return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, e ) ) );
}
catch ( MavenExecutionException e )
catch ( final MavenExecutionException e )
{
return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, e ) ) );
}
}
private Result<ProjectDependencyGraph> sessionDependencyGraph( final MavenSession session )
throws CycleDetectedException, DuplicateProjectException
{
Result<ProjectDependencyGraph> result = null;
if ( session.getProjectDependencyGraph() != null || session.getProjects() != null )
{
final ProjectDependencyGraph graph =
new DefaultProjectDependencyGraph( session.getAllProjects(), session.getProjects() );
result = Result.success( graph );
}
session.setProjects( projectDependencyGraph.getSortedProjects() );
session.setProjectDependencyGraph( projectDependencyGraph );
return result;
}
private Result<ProjectDependencyGraph> reactorDependencyGraph( MavenSession session, List<MavenProject> projects )
throws CycleDetectedException, DuplicateProjectException, MavenExecutionException
{
ProjectDependencyGraph projectDependencyGraph = new DefaultProjectDependencyGraph( projects );
List<MavenProject> activeProjects = projectDependencyGraph.getSortedProjects();
activeProjects = trimSelectedProjects( activeProjects, projectDependencyGraph, session.getRequest() );
activeProjects = trimExcludedProjects( activeProjects, session.getRequest() );
activeProjects = trimResumedProjects( activeProjects, session.getRequest() );
if ( activeProjects.size() != projectDependencyGraph.getSortedProjects().size() )
{
projectDependencyGraph = new FilteredProjectDependencyGraph( projectDependencyGraph, activeProjects );
}
return Result.success( projectDependencyGraph );
}
@ -368,16 +359,6 @@ public class DefaultGraphBuilder
return false;
}
private MavenExecutionResult addExceptionToResult( MavenExecutionResult result, Throwable e )
{
if ( !result.getExceptions().contains( e ) )
{
result.addException( e );
}
return result;
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Project collection

View File

@ -21,6 +21,7 @@ package org.apache.maven.graph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -43,6 +44,8 @@ public class DefaultProjectDependencyGraph
private ProjectSorter sorter;
private List<MavenProject> allProjects;
/**
* Creates a new project dependency graph based on the specified projects.
*
@ -53,9 +56,38 @@ public class DefaultProjectDependencyGraph
public DefaultProjectDependencyGraph( Collection<MavenProject> projects )
throws CycleDetectedException, DuplicateProjectException
{
super();
this.allProjects = Collections.unmodifiableList( new ArrayList<>( projects ) );
this.sorter = new ProjectSorter( projects );
}
/**
* Creates a new project dependency graph based on the specified projects.
*
* @param allProjects All collected projects.
* @param projects The projects to create the dependency graph with.
*
* @throws DuplicateProjectException
* @throws CycleDetectedException
* @since 3.5.0-alpha-2
*/
public DefaultProjectDependencyGraph( final List<MavenProject> allProjects,
final Collection<MavenProject> projects )
throws CycleDetectedException, DuplicateProjectException
{
super();
this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) );
this.sorter = new ProjectSorter( projects );
}
/**
* @since 3.5.0-alpha-2
*/
public List<MavenProject> getAllProjects()
{
return this.allProjects;
}
public List<MavenProject> getSortedProjects()
{
return new ArrayList<>( sorter.getSortedProjects() );

View File

@ -64,6 +64,14 @@ class FilteredProjectDependencyGraph
}
}
/**
* @since 3.5.0-alpha-2
*/
public List<MavenProject> getAllProjects()
{
return this.projectDependencyGraph.getAllProjects();
}
public List<MavenProject> getSortedProjects()
{
if ( sortedProjects == null )

View File

@ -157,6 +157,7 @@ public abstract class AbstractCoreMavenComponentTestCase
new MavenSession( getContainer(), configuration.getRepositorySession(), request,
new DefaultMavenExecutionResult() );
session.setProjects( Arrays.asList( project ) );
session.setAllProjects( session.getProjects() );
return session;
}

View File

@ -400,6 +400,11 @@ public class LifecycleExecutorTest
return Collections.emptyList();
}
public List<MavenProject> getAllProjects()
{
return session.getAllProjects();
}
public List<MavenProject> getSortedProjects()
{
return Collections.singletonList( session.getCurrentProject() );

View File

@ -169,6 +169,11 @@ public class ProjectDependencyGraphStub
return dependencies;
}
public List<MavenProject> getAllProjects()
{
return Arrays.asList( A, B, C, X, Y, Z, UNKNOWN );
}
public List<MavenProject> getSortedProjects()
{
return Arrays.asList( A, B, C, X, Y, Z ); // I'm not entirely sure about the order but this should do...