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

cstamas pushed a commit to branch maven-3.10.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.10.x by this push:
     new 818c172078 Let build extensions override core lifecycle mappings again 
(fixes #14015) (#14016)
818c172078 is described below

commit 818c1720783fafd38478d518361f0220a14ba4af
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Nov 17 18:41:59 2026 +0100

    Let build extensions override core lifecycle mappings again (fixes #14015) 
(#14016)
    
    The injected Sisu map lists every visible bean in rank order, so a hint
    such as "jar" can appear twice; copying it into a HashMap keeps the
    lowest-ranked entry, core's. Look the mapping up with get() and keep the
    first lifecycle per id, so an extension's mapping wins as in 3.10.0.
---
 .../apache/maven/lifecycle/DefaultLifecycles.java  |  6 +-
 .../internal/DefaultLifecyclePluginAnalyzer.java   |  8 +--
 .../maven/lifecycle/DefaultLifecyclesTest.java     | 26 +++++++
 .../DefaultLifecyclePluginAnalyzerTest.java        | 81 ++++++++++++++++++++++
 .../lifecycle/internal/stub/RankedEntriesMap.java  | 60 ++++++++++++++++
 5 files changed, 174 insertions(+), 7 deletions(-)

diff --git 
a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java 
b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java
index 234ea3034d..47be5a4d72 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java
@@ -131,8 +131,10 @@ public Map<String, Lifecycle> getPhaseToLifecycleMap() {
     public List<Lifecycle> getLifeCycles() {
         // ensure canonical order of standard lifecycles
 
-        // take "snapshot": will filter by visibility
-        Map<String, Lifecycle> lifecycles = new 
LinkedHashMap<>(this.lifecycles);
+        // take "snapshot": will filter by visibility; keep the highest-ranked 
lifecycle for each id, so a build
+        // extension can override a lifecycle that core already defines
+        Map<String, Lifecycle> lifecycles = new LinkedHashMap<>();
+        this.lifecycles.forEach(lifecycles::putIfAbsent);
         // apply enabled
         lifecycles.entrySet().removeIf(e -> !isEnabled(e.getKey()));
 
diff --git 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
index a892916bea..8b0334434d 100644
--- 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
+++ 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
@@ -22,7 +22,6 @@
 import javax.inject.Named;
 import javax.inject.Singleton;
 
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -83,10 +82,9 @@ public Set<Plugin> 
getPluginsBoundByDefaultToAllLifecycles(String packaging) {
                     + Thread.currentThread().getContextClassLoader());
         }
 
-        // take "snapshot": will filter by visibility
-        Map<String, LifecycleMapping> filtered = new 
HashMap<>(this.lifecycleMappings);
-
-        LifecycleMapping lifecycleMappingForPackaging = 
filtered.get(packaging);
+        // look up directly: the injected map filters by visibility and 
returns the highest-ranked mapping,
+        // so a build extension can override a packaging that core already maps
+        LifecycleMapping lifecycleMappingForPackaging = 
lifecycleMappings.get(packaging);
 
         if (lifecycleMappingForPackaging == null) {
             return null;
diff --git 
a/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
 
b/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
index ece15216f3..af1e450e8c 100644
--- 
a/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
+++ 
b/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
@@ -28,6 +28,7 @@
 import java.util.stream.Collectors;
 
 import org.apache.maven.lifecycle.internal.stub.LoggerStub;
+import org.apache.maven.lifecycle.internal.stub.RankedEntriesMap;
 import org.apache.maven.lifecycle.providers.lifecycle.CleanLifecycleProvider;
 import org.apache.maven.lifecycle.providers.lifecycle.DefaultLifecycleProvider;
 import org.apache.maven.lifecycle.providers.lifecycle.SiteLifecycleProvider;
@@ -37,6 +38,7 @@
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 
 /**
  * @author Kristian Rosenvold
@@ -78,4 +80,28 @@ public void testSiteLifecycleDisabled() {
             System.clearProperty("maven.site.lifecycle.disabled");
         }
     }
+
+    @Test
+    public void testHighestRankedLifecycleWinsForDuplicateId() {
+        // a build extension registers its own "clean" lifecycle, which ranks 
above the one in core
+        Lifecycle extensionClean =
+                new Lifecycle(CleanLifecycleProvider.NAME, 
Collections.singletonList("extension-clean"), null);
+        RankedEntriesMap<Lifecycle> all = new RankedEntriesMap<Lifecycle>()
+                .add(CleanLifecycleProvider.NAME, extensionClean)
+                .add(
+                        CleanLifecycleProvider.NAME,
+                        new Lifecycle(CleanLifecycleProvider.NAME, 
Collections.singletonList("clean"), null))
+                .add(
+                        DefaultLifecycleProvider.NAME,
+                        new Lifecycle(DefaultLifecycleProvider.NAME, 
Collections.singletonList("compile"), null))
+                .add(
+                        SiteLifecycleProvider.NAME,
+                        new Lifecycle(SiteLifecycleProvider.NAME, 
Collections.singletonList("site"), null));
+
+        DefaultLifecycles lifecycles = new DefaultLifecycles(all, new 
LoggerStub());
+
+        assertSame(extensionClean, lifecycles.getLifeCycles().get(0));
+        assertSame(extensionClean, lifecycles.get("extension-clean"));
+        assertNull(lifecycles.get("clean"));
+    }
 }
diff --git 
a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzerTest.java
 
b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzerTest.java
new file mode 100644
index 0000000000..324afb67d2
--- /dev/null
+++ 
b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzerTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.lifecycle.internal;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.maven.lifecycle.DefaultLifecycles;
+import org.apache.maven.lifecycle.Lifecycle;
+import org.apache.maven.lifecycle.internal.stub.LoggerStub;
+import org.apache.maven.lifecycle.internal.stub.RankedEntriesMap;
+import org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping;
+import org.apache.maven.lifecycle.mapping.LifecycleMapping;
+import org.apache.maven.lifecycle.mapping.LifecyclePhase;
+import org.apache.maven.model.Plugin;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class DefaultLifecyclePluginAnalyzerTest {
+
+    @Test
+    void highestRankedMappingWinsForDuplicatePackaging() throws Exception {
+        // a build extension registers its own "jar" mapping, which ranks 
above the one in core
+        RankedEntriesMap<LifecycleMapping> mappings = new 
RankedEntriesMap<LifecycleMapping>()
+                .add("jar", 
mapping("org.example:extension-plugin:1.0:prepare"))
+                .add("jar", mapping("org.example:core-plugin:1.0:prepare"));
+
+        Set<Plugin> plugins = 
analyzer(mappings).getPluginsBoundByDefaultToAllLifecycles("jar");
+
+        assertEquals(
+                Collections.singletonList("org.example:extension-plugin"),
+                
plugins.stream().map(Plugin::getKey).collect(Collectors.toList()));
+    }
+
+    private static LifecycleMapping mapping(String goal) {
+        org.apache.maven.lifecycle.mapping.Lifecycle lifecycle = new 
org.apache.maven.lifecycle.mapping.Lifecycle();
+        lifecycle.setId("default");
+        lifecycle.setLifecyclePhases(Collections.singletonMap("initialize", 
new LifecyclePhase(goal)));
+        return new 
DefaultLifecycleMapping(Collections.singletonList(lifecycle));
+    }
+
+    private static DefaultLifecyclePluginAnalyzer analyzer(Map<String, 
LifecycleMapping> mappings) throws Exception {
+        Map<String, Lifecycle> lifecycles = new LinkedHashMap<>();
+        lifecycles.put("default", new Lifecycle("default", 
Arrays.asList("initialize"), null));
+        lifecycles.put("clean", new Lifecycle("clean", Arrays.asList("clean"), 
null));
+        lifecycles.put("site", new Lifecycle("site", Arrays.asList("site"), 
null));
+
+        DefaultLifecyclePluginAnalyzer analyzer = new 
DefaultLifecyclePluginAnalyzer();
+        set(analyzer, "lifecycleMappings", mappings);
+        set(analyzer, "defaultLifeCycles", new DefaultLifecycles(lifecycles, 
new LoggerStub()));
+        return analyzer;
+    }
+
+    private static void set(Object target, String name, Object value) throws 
Exception {
+        Field field = 
DefaultLifecyclePluginAnalyzer.class.getDeclaredField(name);
+        field.setAccessible(true);
+        field.set(target, value);
+    }
+}
diff --git 
a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/RankedEntriesMap.java
 
b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/RankedEntriesMap.java
new file mode 100644
index 0000000000..7edddc9f22
--- /dev/null
+++ 
b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/RankedEntriesMap.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.lifecycle.internal.stub;
+
+import java.util.AbstractMap;
+import java.util.AbstractSet;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Read-only map that behaves like the {@code Map<String, T>} Sisu injects: 
its entries are all visible beans in rank
+ * order, so one key can appear more than once, and {@link #get(Object)} 
returns the first (highest-ranked) match.
+ *
+ * @param <V> the type of the values
+ */
+public class RankedEntriesMap<V> extends AbstractMap<String, V> {
+    private final List<Map.Entry<String, V>> entries = new ArrayList<>();
+
+    /**
+     * Adds an entry ranked below all entries added before it.
+     */
+    public RankedEntriesMap<V> add(String key, V value) {
+        entries.add(new SimpleImmutableEntry<>(key, value));
+        return this;
+    }
+
+    @Override
+    public Set<Map.Entry<String, V>> entrySet() {
+        return new AbstractSet<Map.Entry<String, V>>() {
+            @Override
+            public Iterator<Map.Entry<String, V>> iterator() {
+                return entries.iterator();
+            }
+
+            @Override
+            public int size() {
+                return entries.size();
+            }
+        };
+    }
+}

Reply via email to