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/14 12:49:15 UTC

(maven) branch master updated: fix: keep location of inherited plugins (fix #10717)

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 b245782f09 fix: keep location of inherited plugins (fix #10717)
b245782f09 is described below

commit b245782f09cc3d132703127d3cb632699030cd15
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Wed Oct 14 14:49:08 2026 +0200

    fix: keep location of inherited plugins (fix #10717)
    
    - DefaultInheritanceAssembler lost the element location (empty key) when merging an inherited plugin or report plugin into a new instance; the location is now preserved
    - Affects tools (e.g. OpenRewrite Maven plugin) that resolve config paths against the declaring POM via getLocation("")
    - Test: DefaultInheritanceAssemblerTest#testInheritedPluginKeepsItsLocation covers build and report plugins inherited from a parent
---
 .../impl/model/DefaultInheritanceAssembler.java    | 34 ++++++++++
 .../model/DefaultInheritanceAssemblerTest.java     | 74 ++++++++++++++++++++++
 2 files changed, 108 insertions(+)

diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java
index b6d7230b4f..2f28c7c53e 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java
@@ -252,6 +252,7 @@ protected void mergePluginContainer_Plugins(
                         // NOTE: Enforce recursive merge to trigger merging/inheritance logic for executions
                         Plugin plugin = Plugin.newInstance(false);
                         plugin = mergePlugin(plugin, element, sourceDominant, context);
+                        plugin = withElementLocation(plugin, element);
 
                         Object key = getPluginKey().apply(plugin);
 
@@ -292,6 +293,38 @@ protected void mergePluginContainer_Plugins(
             }
         }
 
+        /**
+         * Keeps the location of the {@code <plugin>} element itself, which is stored under the empty key and
+         * which merging into a new instance does not copy, so that the inherited plugin still tells which POM
+         * declares it.
+         *
+         * @param merged the plugin resulting from the merge
+         * @param declared the plugin as declared in the parent POM
+         * @return the merged plugin, with the location of the declared plugin element if it is known
+         */
+        private static Plugin withElementLocation(Plugin merged, Plugin declared) {
+            InputLocation location = declared.getLocation("");
+            return location != null
+                    ? Plugin.newBuilder(merged, true).location("", location).build()
+                    : merged;
+        }
+
+        /**
+         * Same as {@link #withElementLocation(Plugin, Plugin)} for a report plugin.
+         *
+         * @param merged the report plugin resulting from the merge
+         * @param declared the report plugin as declared in the parent POM
+         * @return the merged report plugin, with the location of the declared element if it is known
+         */
+        private static ReportPlugin withElementLocation(ReportPlugin merged, ReportPlugin declared) {
+            InputLocation location = declared.getLocation("");
+            return location != null
+                    ? ReportPlugin.newBuilder(merged, true)
+                            .location("", location)
+                            .build()
+                    : merged;
+        }
+
         @Override
         protected Plugin mergePlugin(
                 Plugin target, Plugin source, boolean sourceDominant, Map<Object, Object> context) {
@@ -325,6 +358,7 @@ protected void mergeReporting_Plugins(
                         // NOTE: Enforce recursive merge to trigger merging/inheritance logic for executions as well
                         ReportPlugin plugin = ReportPlugin.newInstance(false);
                         plugin = mergeReportPlugin(plugin, element, sourceDominant, context);
+                        plugin = withElementLocation(plugin, element);
 
                         merged.put(getReportPluginKey().apply(element), plugin);
                     }
diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java
index b7e32d3c89..ecfc895a0c 100644
--- a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java
+++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java
@@ -18,12 +18,16 @@
  */
 package org.apache.maven.impl.model;
 
+import java.io.StringReader;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
 
+import org.apache.maven.api.model.InputLocation;
 import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Plugin;
+import org.apache.maven.api.model.ReportPlugin;
 import org.apache.maven.api.services.xml.XmlReaderRequest;
 import org.apache.maven.api.services.xml.XmlWriterRequest;
 import org.apache.maven.impl.DefaultModelXmlFactory;
@@ -33,7 +37,9 @@
 import org.xmlunit.diff.Diff;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 class DefaultInheritanceAssemblerTest {
 
@@ -55,6 +61,74 @@ private Model getModel(String name) throws Exception {
         return xmlFactory.read(XmlReaderRequest.builder().path(getPom(name)).build());
     }
 
+    /**
+     * An inherited plugin must still report the POM that declares it, as the location of the plugin element
+     * itself is what tools use to resolve paths in the plugin configuration against the declaring POM.
+     */
+    @Test
+    void testInheritedPluginKeepsItsLocation() throws Exception {
+        Model parent = readWithLocations(
+                "<project><modelVersion>4.0.0</modelVersion><groupId>g</groupId><artifactId>parent</artifactId>"
+                        + "<version>1</version><packaging>pom</packaging><build><plugins><plugin>"
+                        + "<artifactId>some-plugin</artifactId></plugin></plugins></build>"
+                        + "<reporting><plugins><plugin><artifactId>some-report</artifactId></plugin></plugins>"
+                        + "</reporting></project>",
+                "parent.xml");
+        Model child = readWithLocations(
+                "<project><modelVersion>4.0.0</modelVersion><parent><groupId>g</groupId>"
+                        + "<artifactId>parent</artifactId><version>1</version></parent>"
+                        + "<artifactId>child</artifactId></project>",
+                "child.xml");
+
+        Model assembled = assembler.assembleModelInheritance(child, parent, null, null);
+
+        Plugin plugin = assembled.getBuild().getPlugins().get(0);
+        InputLocation location = plugin.getLocation("");
+        assertNotNull(location);
+        assertEquals(parent.getBuild().getPlugins().get(0).getLocation(""), location);
+        assertEquals("g:parent:1", location.getSource().getModelId());
+
+        ReportPlugin reportPlugin = assembled.getReporting().getPlugins().get(0);
+        location = reportPlugin.getLocation("");
+        assertNotNull(location);
+        assertEquals(parent.getReporting().getPlugins().get(0).getLocation(""), location);
+        assertEquals("g:parent:1", location.getSource().getModelId());
+    }
+
+    /**
+     * A plugin that the child declares itself keeps the location of the child declaration, the location of the
+     * inherited one must not replace it.
+     */
+    @Test
+    void testChildPluginKeepsItsOwnLocation() throws Exception {
+        Model parent = readWithLocations(
+                "<project><modelVersion>4.0.0</modelVersion><groupId>g</groupId><artifactId>parent</artifactId>"
+                        + "<version>1</version><packaging>pom</packaging><build><plugins><plugin>"
+                        + "<artifactId>some-plugin</artifactId></plugin></plugins></build></project>",
+                "parent.xml");
+        Model child = readWithLocations(
+                "<project><modelVersion>4.0.0</modelVersion><parent><groupId>g</groupId>"
+                        + "<artifactId>parent</artifactId><version>1</version></parent>"
+                        + "<artifactId>child</artifactId><build><plugins><plugin>"
+                        + "<artifactId>some-plugin</artifactId></plugin></plugins></build></project>",
+                "child.xml");
+
+        Model assembled = assembler.assembleModelInheritance(child, parent, null, null);
+
+        InputLocation location = assembled.getBuild().getPlugins().get(0).getLocation("");
+        assertNotNull(location);
+        assertEquals(child.getBuild().getPlugins().get(0).getLocation(""), location);
+        assertEquals("g:child:1", location.getSource().getModelId());
+    }
+
+    private Model readWithLocations(String xml, String location) throws Exception {
+        return xmlFactory.read(XmlReaderRequest.builder()
+                .reader(new StringReader(xml))
+                .location(location)
+                .addLocationInformation(true)
+                .build());
+    }
+
     @Test
     void testPluginConfiguration() throws Exception {
         testInheritance("plugin-configuration");