[MNG-5965] Parallel build multiplies work if multiple goals are given
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 4 Jul 2017 15:33:10 +0000 (17:33 +0200)
committerMichael Osipov <michaelo@apache.org>
Wed, 26 Dec 2018 14:24:16 +0000 (15:24 +0100)
This closes #125

maven-core/src/main/java/org/apache/maven/lifecycle/internal/builder/multithreaded/ConcurrencyDependencyGraph.java
maven-core/src/main/java/org/apache/maven/lifecycle/internal/builder/multithreaded/MultiThreadedBuilder.java
maven-core/src/test/java/org/apache/maven/lifecycle/internal/ConcurrencyDependencyGraphTest.java

index 190e0f7..efa8c28 100644 (file)
@@ -26,6 +26,7 @@ import org.apache.maven.project.MavenProject;
 
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -61,12 +62,12 @@ public class ConcurrencyDependencyGraph
     /**
      * Gets all the builds that have no reactor-dependencies
      *
-     * @return A list of all the initial builds
+     * @return A set of all the initial builds
      */
 
     public List<MavenProject> getRootSchedulableBuilds()
     {
-        List<MavenProject> result = new ArrayList<>();
+        Set<MavenProject> result = new LinkedHashSet<>();
         for ( ProjectSegment projectBuild : projectBuilds )
         {
             if ( projectDependencyGraph.getUpstreamProjects( projectBuild.getProject(), false ).isEmpty() )
@@ -74,7 +75,7 @@ public class ConcurrencyDependencyGraph
                 result.add( projectBuild.getProject() );
             }
         }
-        return result;
+        return new ArrayList<>( result );
     }
 
     /**
@@ -151,4 +152,4 @@ public class ConcurrencyDependencyGraph
         activeDependencies.removeAll( finishedProjects );
         return activeDependencies;
     }
-}
\ No newline at end of file
+}
index 1414a12..bfbfb02 100644 (file)
@@ -49,7 +49,7 @@ import org.codehaus.plexus.logging.Logger;
  * This builder uses a number of threads equal to the minimum of the degree of concurrency (which is the thread count
  * set with <code>-T</code> on the command-line) and the number of projects to build. As such, building a single project
  * will always result in a sequential build, regardless of the thread count.
- * </p> 
+ * </p>
  * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
  *
  * @since 3.0
@@ -87,17 +87,19 @@ public class MultiThreadedBuilder
         }
         ExecutorService executor = Executors.newFixedThreadPool( nThreads, new BuildThreadFactory() );
         CompletionService<ProjectSegment> service = new ExecutorCompletionService<>( executor );
-        ConcurrencyDependencyGraph analyzer =
-            new ConcurrencyDependencyGraph( projectBuilds, session.getProjectDependencyGraph() );
 
         // Currently disabled
         ThreadOutputMuxer muxer = null; // new ThreadOutputMuxer( analyzer.getProjectBuilds(), System.out );
 
         for ( TaskSegment taskSegment : taskSegments )
         {
+            ProjectBuildList segmentProjectBuilds = projectBuilds.getByTaskSegment( taskSegment );
             Map<MavenProject, ProjectSegment> projectBuildMap = projectBuilds.selectSegment( taskSegment );
             try
             {
+                ConcurrencyDependencyGraph analyzer =
+                    new ConcurrencyDependencyGraph( segmentProjectBuilds,
+                                                    session.getProjectDependencyGraph() );
                 multiThreadedProjectTaskSegmentBuild( analyzer, reactorContext, session, service, taskSegment,
                                                       projectBuildMap, muxer );
                 if ( reactorContext.getReactorBuildStatus().isHalted() )
@@ -143,7 +145,7 @@ public class MultiThreadedBuilder
                     break;
                 }
 
-                // MNG-6170: Only schedule other modules from reactor if we have more modules to build than one. 
+                // MNG-6170: Only schedule other modules from reactor if we have more modules to build than one.
                 if ( analyzer.getNumberOfBuilds() > 1 )
                 {
                     final List<MavenProject> newItemsThatCanBeBuilt =
index 9ab0601..6118bbe 100644 (file)
@@ -53,7 +53,7 @@ public class ConcurrencyDependencyGraphTest
 
         final List<MavenProject> projectBuilds = graph.getRootSchedulableBuilds();
         assertEquals( 1, projectBuilds.size() );
-        assertEquals( A, projectBuilds.get( 0 ) );
+        assertEquals( A, projectBuilds.iterator().next() );
 
         final List<MavenProject> subsequent = graph.markAsFinished( A );
         assertEquals( 2, subsequent.size() );