diff --git a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlan.java b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlan.java index cb99d6c802ff..2a55cf3f8b57 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlan.java +++ b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlan.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -211,6 +212,13 @@ public Set duplicateIds() { return duplicateIds; } + /** + * Returns the build steps in topological order (dependencies before dependents). + *

+ * Note: The returned list is mutable and may be reordered in place + * by {@link #computeWeights(boolean, Map)} for scheduling purposes. After that call, + * the list reflects weight-based scheduling order rather than pure topological order. + */ public List sortedNodes() { if (sortedNodes == null) { synchronized (this) { @@ -238,27 +246,70 @@ private static void visitNode(BuildStep node, Set visited, ListWhen {@code parallel} is true, computes critical-path weights using + * {@code previousTimings} (actual durations in millis from a prior build) or + * graph depth as fallback, then sorts by descending weight so the scheduler + * picks the heaviest-path steps first. When weights are equal, steps from the + * same project are grouped together (by reactor order) so that a project's + * phases run consecutively when no higher-weight step from another project + * is competing for a thread.

+ * + *

When {@code parallel} is false (single-thread), all step weights are reset + * to zero and steps are sorted by project reactor order alone, producing + * Maven-3-like sequential per-project execution.

*/ - void computeWeights(Map previousTimings) { + void computeWeights(boolean parallel, Map previousTimings) { List sorted = sortedNodes(); - for (int i = sorted.size() - 1; i >= 0; i--) { - BuildStep step = sorted.get(i); - long maxSuccessorWeight = 0; - for (BuildStep successor : step.successors) { - maxSuccessorWeight = Math.max(maxSuccessorWeight, successor.weight); + if (parallel) { + for (int i = sorted.size() - 1; i >= 0; i--) { + BuildStep step = sorted.get(i); + long maxSuccessorWeight = 0; + for (BuildStep successor : step.successors) { + maxSuccessorWeight = Math.max(maxSuccessorWeight, successor.weight); + } + long selfWeight; + if (previousTimings.isEmpty()) { + selfWeight = 1; + } else { + Long millis = previousTimings.get(step.stepId()); + selfWeight = millis != null ? Math.max(1L, millis) : 1; + } + step.weight = selfWeight + maxSuccessorWeight; } - long selfWeight; - if (previousTimings.isEmpty()) { - selfWeight = 1; - } else { - Long millis = previousTimings.get(step.stepId()); - selfWeight = millis != null ? Math.max(1L, millis) : 1; + } else { + // Reset all weights to zero for single-thread mode so sorting is by project order alone + for (BuildStep step : sorted) { + step.weight = 0; } - step.weight = selfWeight + maxSuccessorWeight; } + Map projectIndex = projectIndex(); + sorted.sort(Comparator.comparingLong((BuildStep s) -> s.weight) + .reversed() + .thenComparingInt(s -> { + Integer index = projectIndex.get(s.project); + if (index == null) { + throw new IllegalStateException("BuildStep project not found in plan: " + s.project.getId()); + } + return index; + })); + } + + /** + * Builds an index mapping each project to its position in the reactor build order. + *

+ * Critical assumption: This relies on {@code plan} being a LinkedHashMap + * with projects inserted in reactor order. If projects are added out of order, the returned + * indices will not reflect correct reactor ordering, breaking single-thread build sequencing. + */ + private Map projectIndex() { + Map index = new HashMap<>(); + int i = 0; + for (MavenProject project : plan.keySet()) { + index.put(project, i++); + } + return index; } /** diff --git a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java index 7725806ddc0f..6c8498c0ec69 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java +++ b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java @@ -880,13 +880,13 @@ private void plan() { } Map previousTimings = Map.of(); - if (isStepTimingsEnabled()) { + if (threads > 1 && isStepTimingsEnabled()) { Path rootDir = rootDirectoryOrNull(); if (rootDir != null) { previousTimings = StepTimings.read(rootDir); } } - plan.computeWeights(previousTimings); + plan.computeWeights(threads > 1, previousTimings); plan.initReadyQueue(readyQueue); diff --git a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanWeightsTest.java b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanWeightsTest.java index 40ba5d736842..4b0b646fdcdd 100644 --- a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanWeightsTest.java +++ b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanWeightsTest.java @@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Unit tests for {@link BuildPlan#computeWeights(Map)}. + * Unit tests for {@link BuildPlan#computeWeights(boolean, Map)}. */ class BuildPlanWeightsTest { @@ -41,7 +41,7 @@ void singleStepGetsWeightOne() { BuildStep a = step("compile"); BuildPlan plan = planOf(a); - plan.computeWeights(Map.of()); + plan.computeWeights(true, Map.of()); assertEquals(1L, a.weight); } @@ -56,7 +56,7 @@ void linearChainPropagatesWeightsCorrectly() { link(b, c); BuildPlan plan = planOf(a, b, c); - plan.computeWeights(Map.of()); + plan.computeWeights(true, Map.of()); // c has no successors → weight = 1 // b depends on c → weight = 1 (self) + 1 (c) = 2 @@ -79,7 +79,7 @@ void heavierPathWinsAtBranchPoint() { link(root, right); BuildPlan plan = planOf(root, left, leaf, right); - plan.computeWeights(Map.of()); + plan.computeWeights(true, Map.of()); // leaf: 1, left: 2, right: 1, root: max(2,1)+1 = 3 assertEquals(1L, leaf.weight); @@ -94,7 +94,7 @@ void disconnectedStepsEachGetWeightOne() { BuildStep b = step("test"); BuildPlan plan = planOf(a, b); - plan.computeWeights(Map.of()); + plan.computeWeights(true, Map.of()); assertEquals(1L, a.weight); assertEquals(1L, b.weight); @@ -115,7 +115,7 @@ void timingDataIsUsedAsStepWeight() { Map timings = Map.of( a.stepId(), 50L, b.stepId(), 200L); - plan.computeWeights(timings); + plan.computeWeights(true, timings); // b: weight = 200 (self, no successor) // a: weight = 50 (self) + 200 (b) = 250 @@ -130,7 +130,7 @@ void missingTimingEntryFallsBackToOne() { BuildPlan plan = planOf(known, unknown); Map timings = Map.of(known.stepId(), 100L); - plan.computeWeights(timings); + plan.computeWeights(true, timings); assertEquals(100L, known.weight); assertEquals(1L, unknown.weight); // fallback @@ -142,7 +142,7 @@ void zeroMillisTimingClampedToOne() { BuildStep a = step("com.example", "a", "compile"); BuildPlan plan = planOf(a); - plan.computeWeights(Map.of(a.stepId(), 0L)); + plan.computeWeights(true, Map.of(a.stepId(), 0L)); // Math.max(1L, 0L) = 1 assertEquals(1L, a.weight); @@ -162,7 +162,7 @@ void sortedNodesAreOrderedByDescendingWeightAfterComputeWeights() { link(b, c); BuildPlan plan = planOf(a, b, c); - plan.computeWeights(Map.of()); + plan.computeWeights(true, Map.of()); // After computeWeights(), sortedNodes() must be ordered heaviest-first. java.util.List sorted = plan.sortedNodes(); @@ -171,6 +171,95 @@ void sortedNodesAreOrderedByDescendingWeightAfterComputeWeights() { assertEquals(a, sorted.get(0), "Heaviest step (root) must come first"); } + // ------------------------------------------------------------------------- + // Single-thread (non-parallel) mode groups steps by project + // ------------------------------------------------------------------------- + + @Test + void singleThreadGroupsStepsByProjectOrder() { + // Two independent projects, each with compile → test + MavenProject projectA = project("com.example", "a"); + MavenProject projectB = project("com.example", "b"); + BuildStep a1 = new BuildStep("compile", projectA, null); + BuildStep a2 = new BuildStep("test", projectA, null); + BuildStep b1 = new BuildStep("compile", projectB, null); + BuildStep b2 = new BuildStep("test", projectB, null); + link(a1, a2); + link(b1, b2); + BuildPlan plan = planOfMulti( + Map.entry(projectA, java.util.List.of(a1, a2)), Map.entry(projectB, java.util.List.of(b1, b2))); + + plan.computeWeights(false, Map.of()); + + java.util.List sorted = plan.sortedNodes(); + // All steps for project A should come before all steps for project B + int lastA = -1, firstB = sorted.size(); + for (int i = 0; i < sorted.size(); i++) { + if (sorted.get(i).project == projectA) { + lastA = i; + } + if (sorted.get(i).project == projectB && i < firstB) { + firstB = i; + } + } + assertTrue(lastA < firstB, "All steps for project A should precede all steps for project B"); + } + + @Test + void parallelModeWithEqualWeightsStillGroupsByProject() { + // Same setup, but parallel mode — equal weights should still group by project + MavenProject projectA = project("com.example", "a2"); + MavenProject projectB = project("com.example", "b2"); + BuildStep a1 = new BuildStep("compile", projectA, null); + BuildStep a2 = new BuildStep("test", projectA, null); + BuildStep b1 = new BuildStep("compile", projectB, null); + BuildStep b2 = new BuildStep("test", projectB, null); + link(a1, a2); + link(b1, b2); + BuildPlan plan = planOfMulti( + Map.entry(projectA, java.util.List.of(a1, a2)), Map.entry(projectB, java.util.List.of(b1, b2))); + + plan.computeWeights(true, Map.of()); + + // a1 and b1 both have weight 2, a2 and b2 both have weight 1 + // With the project-order tiebreaker, same-weight steps from A come before B + java.util.List sorted = plan.sortedNodes(); + int a1Pos = sorted.indexOf(a1); + int b1Pos = sorted.indexOf(b1); + assertTrue(a1Pos < b1Pos, "Equal-weight steps from project A should come before project B"); + } + + @Test + void singleThreadWithCrossProjectDependenciesRespectsTopology() { + // Project A and B in reactor order [A, B], where B's compile depends on A's test + // Topology: a1 → a2 → b1 → b2 + MavenProject projectA = project("com.example", "a3"); + MavenProject projectB = project("com.example", "b3"); + BuildStep a1 = new BuildStep("compile", projectA, null); + BuildStep a2 = new BuildStep("test", projectA, null); + BuildStep b1 = new BuildStep("compile", projectB, null); + BuildStep b2 = new BuildStep("test", projectB, null); + link(a1, a2); + link(a2, b1); // Cross-project dependency: B's compile depends on A's test + link(b1, b2); + BuildPlan plan = planOfMulti( + Map.entry(projectA, java.util.List.of(a1, a2)), Map.entry(projectB, java.util.List.of(b1, b2))); + + plan.computeWeights(false, Map.of()); + + // Verify that topological order is maintained despite grouping by project + java.util.List sorted = plan.sortedNodes(); + int a1Pos = sorted.indexOf(a1); + int a2Pos = sorted.indexOf(a2); + int b1Pos = sorted.indexOf(b1); + int b2Pos = sorted.indexOf(b2); + assertTrue(a1Pos < a2Pos, "a1 must precede a2 (intra-project dependency)"); + assertTrue(a2Pos < b1Pos, "a2 must precede b1 (cross-project dependency)"); + assertTrue(b1Pos < b2Pos, "b1 must precede b2 (intra-project dependency)"); + // All A steps should come before all B steps due to the cross-project dependency + assertTrue(a2Pos < b1Pos && a1Pos < b1Pos, "All A steps must precede B steps"); + } + // ------------------------------------------------------------------------- // stepId() does not include version // ------------------------------------------------------------------------- @@ -231,4 +320,17 @@ private static BuildPlan planOf(BuildStep... steps) { } return plan; } + + @SafeVarargs + private static BuildPlan planOfMulti(Map.Entry>... entries) { + BuildPlan plan = new BuildPlan(); + for (Map.Entry> entry : entries) { + Map stepMap = new LinkedHashMap<>(); + for (BuildStep step : entry.getValue()) { + stepMap.put(step.name, step); + } + plan.addProject(entry.getKey(), stepMap); + } + return plan; + } }