You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/10/15 12:09:47 UTC

(maven) branch master updated: fix: concurrent builder drops later lifecycle phases when merging overlapping task segments

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 1e32f12f5f fix: concurrent builder drops later lifecycle phases when merging overlapping task segments
1e32f12f5f is described below

commit 1e32f12f5f5885bca306922686703ace9af5e2d9
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Oct 15 14:09:40 2026 +0200

    fix: concurrent builder drops later lifecycle phases when merging overlapping task segments
    
    - Unskip the surviving step when the merged-in step is active in
      BuildPlan.merge(BuildStep), so plan() can bind mojos via addMojo()
    - Insert non-lifecycle steps at the "active frontier" instead of after
      after:ALL, ensuring correct ordering (package → javadoc → install)
    - Check both directions before adding ordering edges in
      BuildStep.executeAfter() to prevent cycles
    - Disable advanced merge logic for computeForkPlan merges (detected by
      "$plan$" in the add map) to avoid dangling fork step references
    
    Fixes #13529
---
 .../lifecycle/internal/concurrent/BuildPlan.java   | 82 ++++++++++++++++--
 .../lifecycle/internal/concurrent/BuildStep.java   |  2 +-
 .../internal/concurrent/BuildPlanCreatorTest.java  | 97 ++++++++++++++++++++++
 3 files changed, 171 insertions(+), 10 deletions(-)

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 f87b9fb720..211712419e 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
@@ -96,15 +96,74 @@ public void then(BuildPlan step) {
     }
 
     private Map<String, BuildStep> merge(Map<String, BuildStep> org, Map<String, BuildStep> add) {
-        // all new phases should be added after the existing ones
-        List<BuildStep> lasts =
-                org.values().stream().filter(b -> b.successors.isEmpty()).toList();
-        List<BuildStep> firsts =
-                add.values().stream().filter(b -> b.predecessors.isEmpty()).toList();
-        firsts.stream()
-                .filter(addNode -> !org.containsKey(addNode.name))
-                .forEach(addNode -> lasts.forEach(orgNode -> addNode.executeAfter(orgNode)));
-        add.forEach((name, node) -> org.merge(name, node, this::merge));
+        // When add contains infrastructure steps ($plan$, $setup$, $teardown$), this merge
+        // is from computeForkPlan (execution plan merged into a fork). In that case, use the
+        // original terminal-step chaining to avoid creating cycles with the frontier approach.
+        boolean isSegmentMerge = !add.containsKey(BuildStep.PLAN);
+
+        if (isSegmentMerge) {
+            // Track which steps are currently skipped before the merge
+            Set<String> skippedInOrg = org.entrySet().stream()
+                    .filter(e -> e.getValue().skip.get())
+                    .map(Map.Entry::getKey)
+                    .collect(Collectors.toSet());
+
+            // The "active frontier" consists of non-skipped steps that have at least one
+            // skipped same-project successor. This is the boundary between the active and
+            // skipped regions — the right insertion point for new non-lifecycle steps
+            // (e.g., aggregator goals). Using the frontier avoids creating transitive
+            // dependencies on all lifecycle phases through wrapper steps like after:ALL.
+            List<BuildStep> activeFrontier = org.values().stream()
+                    .filter(b -> !b.skip.get())
+                    .filter(b -> b.successors.stream().anyMatch(s -> s.project == b.project && s.skip.get()))
+                    .toList();
+            if (activeFrontier.isEmpty()) {
+                activeFrontier = org.values().stream()
+                        .filter(b -> b.successors.isEmpty())
+                        .toList();
+            }
+
+            // Chain truly new steps (not in org at all) after the active frontier
+            List<BuildStep> insertionPoints = activeFrontier;
+            List<BuildStep> firsts =
+                    add.values().stream().filter(b -> b.predecessors.isEmpty()).toList();
+            firsts.stream()
+                    .filter(addNode -> !org.containsKey(addNode.name))
+                    .forEach(addNode -> insertionPoints.forEach(orgNode -> addNode.executeAfter(orgNode)));
+
+            // Merge all steps by name
+            add.forEach((name, node) -> org.merge(name, node, this::merge));
+
+            // Chain newly-activated entry points after non-lifecycle goal steps.
+            // When a follow-up plan un-skips phases beyond the existing plan, those
+            // newly-active phases must wait for any non-lifecycle steps (e.g., aggregator
+            // goals) that were inserted at the frontier.
+            List<BuildStep> goalLasts = org.values().stream()
+                    .filter(b -> b.successors.stream().noneMatch(s -> s.project == b.project))
+                    .filter(b -> !b.skip.get())
+                    .filter(b -> b.phase == null)
+                    .toList();
+            if (!goalLasts.isEmpty()) {
+                org.values().stream()
+                        .filter(step -> skippedInOrg.contains(step.name))
+                        .filter(step -> !step.skip.get())
+                        .filter(step -> step.predecessors.stream()
+                                .filter(p -> p.project == step.project)
+                                .allMatch(p -> !skippedInOrg.contains(p.name)))
+                        .forEach(step -> goalLasts.forEach(step::executeAfter));
+            }
+        } else {
+            // computeForkPlan path: use simple terminal-step chaining
+            List<BuildStep> lasts =
+                    org.values().stream().filter(b -> b.successors.isEmpty()).toList();
+            List<BuildStep> firsts =
+                    add.values().stream().filter(b -> b.predecessors.isEmpty()).toList();
+            firsts.stream()
+                    .filter(addNode -> !org.containsKey(addNode.name))
+                    .forEach(addNode -> lasts.forEach(orgNode -> addNode.executeAfter(orgNode)));
+            add.forEach((name, node) -> org.merge(name, node, this::merge));
+        }
+
         return org;
     }
 
@@ -112,6 +171,11 @@ private BuildStep merge(BuildStep node1, BuildStep node2) {
         node1.predecessors.addAll(node2.predecessors);
         node1.successors.addAll(node2.successors);
         node2.mojos.forEach((k, v) -> node1.mojos.merge(k, v, this::mergeMojos));
+        // Un-skip the surviving step if the merged-in step is active.
+        // Without this, plan() silently drops mojos via addMojo()'s skip guard.
+        if (!node2.skip.get()) {
+            node1.skip.set(false);
+        }
         return node1;
     }
 
diff --git a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildStep.java b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildStep.java
index 8a5f645c1d..6c5f1e31fc 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildStep.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildStep.java
@@ -116,7 +116,7 @@ public void addMojo(MojoExecution mojo, int priority) {
     }
 
     public void executeAfter(BuildStep stepToExecuteBefore) {
-        if (!isSuccessorOf(stepToExecuteBefore)) {
+        if (!isSuccessorOf(stepToExecuteBefore) && !stepToExecuteBefore.isSuccessorOf(this)) {
             predecessors.add(stepToExecuteBefore);
             stepToExecuteBefore.successors.add(this);
         }
diff --git a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanCreatorTest.java b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanCreatorTest.java
index 3a8cb0afb9..78f9bfbbac 100644
--- a/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanCreatorTest.java
+++ b/impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanCreatorTest.java
@@ -35,6 +35,7 @@
 import static org.apache.maven.api.Lifecycle.AFTER;
 import static org.apache.maven.api.Lifecycle.BEFORE;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -87,6 +88,102 @@ protected void mojo(Consumer<String> writer, MojoExecution mojoExecution) {}
         });
     }
 
+    /**
+     * Regression test for <a href="https://github.com/apache/maven/issues/13529">#13529</a>:
+     * merging overlapping lifecycle plans (e.g., {@code package} then {@code install})
+     * must un-skip the non-overlapping phases so that mojos can be bound during planning.
+     */
+    @Test
+    void testMergeUnskipsNonOverlappingPhases() {
+        MavenProject project = new MavenProject();
+        project.setCollectedProjects(List.of());
+        project.setArtifactId("test");
+        Map<MavenProject, List<MavenProject>> projects = Collections.singletonMap(project, Collections.emptyList());
+
+        BuildPlan plan = calculateLifecycleMappings(projects, "package");
+
+        // Verify install is skipped in the package plan
+        assertTrue(plan.requiredStep(project, "install").skip.get());
+
+        plan.then(calculateLifecycleMappings(projects, "install"));
+
+        // Verify install-related steps are NOT skipped after merge
+        for (String stepName : List.of("before:install", "install", "after:install")) {
+            assertFalse(
+                    plan.requiredStep(project, stepName).skip.get(),
+                    "Step " + stepName + " should not be skipped after merging package+install plans");
+        }
+
+        // Verify install is reachable from package
+        assertTrue(
+                plan.requiredStep(project, "install").isSuccessorOf(plan.requiredStep(project, "after:package")),
+                "install must be a successor of package");
+
+        // Verify all predecessor references point to steps actually in the plan
+        plan.allSteps()
+                .forEach(step -> step.predecessors.forEach(pred -> assertTrue(
+                        plan.step(pred.project, pred.name).isPresent(), "Predecessor not in plan: " + pred)));
+    }
+
+    /**
+     * Simulates {@code package javadoc:aggregate-jar install} with three task segments.
+     * The aggregator goal must run after package but before install, without creating
+     * cycles in the build plan.
+     */
+    @Test
+    void testMergeThreeSegmentsWithAggregatorGoal() {
+        MavenProject root = new MavenProject();
+        root.setCollectedProjects(List.of());
+        root.setArtifactId("root");
+        MavenProject child = new MavenProject();
+        child.setCollectedProjects(List.of());
+        child.setArtifactId("child");
+        Map<MavenProject, List<MavenProject>> allProjects = new HashMap<>();
+        allProjects.put(root, Collections.emptyList());
+        allProjects.put(child, Collections.singletonList(root));
+
+        // Segment 1: package (all projects)
+        BuildPlan plan = new BuildPlan(allProjects);
+        plan.then(calculateLifecycleMappings(allProjects, "package"));
+
+        // Segment 2: aggregator goal (root only)
+        BuildPlan goalPlan = new BuildPlan();
+        BuildStep goalStep = new BuildStep("javadoc:aggregate-jar", root, null);
+        Map<String, BuildStep> goalSteps = new HashMap<>();
+        goalSteps.put("javadoc:aggregate-jar", goalStep);
+        goalPlan.addProject(root, goalSteps);
+        plan.then(goalPlan);
+
+        // Segment 3: install (all projects)
+        plan.then(calculateLifecycleMappings(allProjects, "install"));
+
+        // Verify: all predecessor references point to plan objects (no dangling)
+        plan.allSteps()
+                .forEach(step -> step.predecessors.forEach(pred -> assertTrue(
+                        plan.step(pred.project, pred.name).isPresent(), "Predecessor not in plan: " + pred)));
+
+        // Verify: install is not skipped for both projects
+        for (MavenProject p : List.of(root, child)) {
+            assertFalse(
+                    plan.requiredStep(p, "install").skip.get(),
+                    "install should not be skipped for " + p.getArtifactId());
+        }
+
+        // Verify: no cycle — javadoc must NOT be a successor of install
+        BuildStep jdocStep = plan.requiredStep(root, "javadoc:aggregate-jar");
+        BuildStep installStep = plan.requiredStep(root, "install");
+        assertFalse(
+                jdocStep.isSuccessorOf(installStep), "javadoc must not be a successor of install (would create cycle)");
+
+        // Verify: javadoc runs after package
+        assertTrue(
+                jdocStep.isSuccessorOf(plan.requiredStep(root, "after:package")),
+                "javadoc must be a successor of package");
+
+        // Verify: install runs after javadoc
+        assertTrue(installStep.isSuccessorOf(jdocStep), "install must be a successor of javadoc:aggregate-jar");
+    }
+
     @Test
     void testAlias() {
         MavenProject p1 = new MavenProject();