From 114ef6c5a2802e8758e466af92b70f51fd7a2929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=B6hme?= Date: Mon, 20 Feb 2017 07:32:17 +0100 Subject: [PATCH] [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 --- .../java/org/apache/maven/DefaultMaven.java | 2 +- .../execution/ProjectDependencyGraph.java | 9 ++ .../maven/graph/DefaultGraphBuilder.java | 113 ++++++++---------- .../graph/DefaultProjectDependencyGraph.java | 32 +++++ .../graph/FilteredProjectDependencyGraph.java | 8 ++ .../AbstractCoreMavenComponentTestCase.java | 1 + .../lifecycle/LifecycleExecutorTest.java | 5 + .../stub/ProjectDependencyGraphStub.java | 5 + 8 files changed, 108 insertions(+), 67 deletions(-) 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 15b946e887..7e46ad046f 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 ); } 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 0d5584b8af..d206d730e0 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 @@ -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 getAllProjects(); + /** * Gets all projects in their intended build order, i.e. after topologically sorting the projects according to their * inter-dependencies. 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 680d584693..2cbea8457b 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 build( MavenSession session ) { - if ( session.getProjectDependencyGraph() != null ) - { - return dependencyGraph( session, session.getProjects(), false ); - } - - List 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 dependencyGraph( MavenSession session, List projects, - boolean applyMakeBehaviour ) - { - MavenExecutionRequest request = session.getRequest(); - - ProjectDependencyGraph projectDependencyGraph = null; - try { - projectDependencyGraph = new DefaultProjectDependencyGraph( projects ); + Result result = sessionDependencyGraph( session ); - if ( applyMakeBehaviour ) + if ( result == null ) { - List 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 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 sessionDependencyGraph( final MavenSession session ) + throws CycleDetectedException, DuplicateProjectException + { + Result 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 reactorDependencyGraph( MavenSession session, List projects ) + throws CycleDetectedException, DuplicateProjectException, MavenExecutionException + { + ProjectDependencyGraph projectDependencyGraph = new DefaultProjectDependencyGraph( projects ); + List 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 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 a5233e5248..effab0bc12 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 allProjects; + /** * Creates a new project dependency graph based on the specified projects. * @@ -53,9 +56,38 @@ public class DefaultProjectDependencyGraph public DefaultProjectDependencyGraph( Collection 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 allProjects, + final Collection 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 getAllProjects() + { + return this.allProjects; + } + public List getSortedProjects() { return new ArrayList<>( sorter.getSortedProjects() ); 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 1d9c8db5d5..ad3910d7af 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 getAllProjects() + { + return this.projectDependencyGraph.getAllProjects(); + } + public List getSortedProjects() { if ( sortedProjects == null ) 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 57d374fd88..157a5ecdef 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; } 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 951f133df4..5252a21e6f 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 getAllProjects() + { + return session.getAllProjects(); + } + public List getSortedProjects() { return Collections.singletonList( session.getCurrentProject() ); 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 b913caf5fc..7af756d7d5 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 getAllProjects() + { + return Arrays.asList( A, B, C, X, Y, Z, UNKNOWN ); + } + public List getSortedProjects() { return Arrays.asList( A, B, C, X, Y, Z ); // I'm not entirely sure about the order but this should do...