[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 Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/114ef6c5 Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/114ef6c5 Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/114ef6c5 Branch: refs/heads/MNG-6164 Commit: 114ef6c5a2802e8758e466af92b70f51fd7a2929 Parents: 862c84a Author: Christoph Böhme <christ...@b3e.net> Authored: Mon Feb 20 07:32:17 2017 +0100 Committer: Christian Schulte <schu...@apache.org> Committed: Mon Mar 6 17:35:24 2017 +0100 ---------------------------------------------------------------------- .../java/org/apache/maven/DefaultMaven.java | 2 +- .../maven/execution/ProjectDependencyGraph.java | 9 ++ .../apache/maven/graph/DefaultGraphBuilder.java | 109 ++++++++----------- .../graph/DefaultProjectDependencyGraph.java | 32 ++++++ .../graph/FilteredProjectDependencyGraph.java | 8 ++ .../AbstractCoreMavenComponentTestCase.java | 1 + .../maven/lifecycle/LifecycleExecutorTest.java | 5 + .../stub/ProjectDependencyGraphStub.java | 5 + 8 files changed, 106 insertions(+), 65 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/main/java/org/apache/maven/DefaultMaven.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 15b946e..7e46ad0 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -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 ); } http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/main/java/org/apache/maven/execution/ProjectDependencyGraph.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/execution/ProjectDependencyGraph.java b/maven-core/src/main/java/org/apache/maven/execution/ProjectDependencyGraph.java index 0d5584b..d206d73 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/ProjectDependencyGraph.java +++ b/maven-core/src/main/java/org/apache/maven/execution/ProjectDependencyGraph.java @@ -33,6 +33,15 @@ 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. * http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/main/java/org/apache/maven/graph/DefaultGraphBuilder.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/graph/DefaultGraphBuilder.java b/maven-core/src/main/java/org/apache/maven/graph/DefaultGraphBuilder.java index 680d584..2cbea84 100644 --- a/maven-core/src/main/java/org/apache/maven/graph/DefaultGraphBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/graph/DefaultGraphBuilder.java @@ -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 ) + try { - return dependencyGraph( session, session.getProjects(), false ); - } - - List<MavenProject> projects = session.getProjects(); + Result<ProjectDependencyGraph> result = sessionDependencyGraph( session ); - if ( projects == null ) - { - try - { - projects = getProjectsForMavenReactor( session ); - } - catch ( ProjectBuildingException e ) + if ( result == null ) { - return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, e ) ) ); + final List<MavenProject> projects = getProjectsForMavenReactor( session ); + validateProjects( projects ); + result = reactorDependencyGraph( session, projects ); } - validateProjects( projects ); - - return dependencyGraph( session, projects, true ); - } - else - { - return dependencyGraph( session, projects, false ); + return result; } - } - - private Result<ProjectDependencyGraph> dependencyGraph( MavenSession session, List<MavenProject> projects, - boolean applyMakeBehaviour ) - { - MavenExecutionRequest request = session.getRequest(); - - ProjectDependencyGraph projectDependencyGraph = null; - - try + catch ( final ProjectBuildingException e ) { - projectDependencyGraph = new DefaultProjectDependencyGraph( projects ); - - if ( applyMakeBehaviour ) - { - 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 ); - } - } + return Result.error( Lists.newArrayList( new DefaultModelProblem( null, null, null, null, 0, 0, e ) ) ); } - catch ( CycleDetectedException 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 http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java b/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java index a5233e5..effab0b 100644 --- a/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java +++ b/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java @@ -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() ); http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java b/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java index 1d9c8db..ad3910d 100644 --- a/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java +++ b/maven-core/src/main/java/org/apache/maven/graph/FilteredProjectDependencyGraph.java @@ -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 ) http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java ---------------------------------------------------------------------- diff --git a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java index 57d374f..157a5ec 100644 --- a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java @@ -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; } http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java ---------------------------------------------------------------------- diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java index 951f133..5252a21 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java @@ -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() ); http://git-wip-us.apache.org/repos/asf/maven/blob/114ef6c5/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/ProjectDependencyGraphStub.java ---------------------------------------------------------------------- diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/ProjectDependencyGraphStub.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/ProjectDependencyGraphStub.java index b913caf..7af756d 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/ProjectDependencyGraphStub.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/ProjectDependencyGraphStub.java @@ -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...