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

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 5cf937ff Write an empty relativePath on dependency-reduced-pom.xml 
(#843)
5cf937ff is described below

commit 5cf937fff684575ff644eda75d1f57fd8ef542a8
Author: Abdullah <[email protected]>
AuthorDate: Sat Oct 31 14:15:32 2026 +0300

    Write an empty relativePath on dependency-reduced-pom.xml (#843)
    
    * Write an empty relativePath on dependency-reduced-pom.xml
    
    Maven 4 treats a missing relativePath as "..". When the generated
    POM sits next to a pom that is not the parent, that default walks
    into the wrong file and reports a parent cycle.
    
    If the file at the configured relativePath is not the actual parent,
    I write an empty relativePath so Maven goes to the repository.
    Local-only parents still keep a real relative path.
    
    Fixes #813
    
    * Rename parent path helper to isSameFile
    
    * Make isSameFile private and drop its unit test
    
    The helper is only reached from 
rewriteDependencyReducedPomIfWeHaveReduction.
    
    * Assert the full relativePath element in PomWriter tests
    
    * Let isSameFile throw IOException
    
    A failed canonicalization is not the same as a missing parent file.
    
    ---------
    
    Co-authored-by: Sylwester Lachiewicz <[email protected]>
---
 .../apache/maven/plugins/shade/mojo/ShadeMojo.java | 24 ++++++-
 .../maven/plugins/shade/pom/MavenJDOMWriter.java   |  8 ++-
 .../shade/pom/PomWriterRelativePathTest.java       | 84 ++++++++++++++++++++++
 3 files changed, 113 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java 
b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
index f2a1e8a1..3d07408b 100644
--- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
+++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
@@ -1414,8 +1414,17 @@ public class ShadeMojo extends AbstractMojo {
 
                     parentFile = parentFile.getCanonicalFile();
 
-                    String relPath = 
RelativizePath.convertToRelativePath(parentFile, f);
-                    model.getParent().setRelativePath(relPath);
+                    File actualParentFile =
+                            project.getParent() != null ? 
project.getParent().getFile() : null;
+                    if (isSameFile(parentFile, actualParentFile)) {
+                        String relPath = 
RelativizePath.convertToRelativePath(parentFile, f);
+                        model.getParent().setRelativePath(relPath);
+                    } else {
+                        // ../pom.xml has a different GAV (or does not have 
one).
+                        // An empty relativePath stops Maven 4 from walking 
into
+                        // that POM and reporting a false parent cycle.
+                        model.getParent().setRelativePath("");
+                    }
                 }
 
                 try {
@@ -1444,6 +1453,17 @@ public class ShadeMojo extends AbstractMojo {
         }
     }
 
+    /**
+     * True when the configured relativePath file is the project's parent POM.
+     */
+    private static boolean isSameFile(File configured, File actual) throws 
IOException {
+        return actual != null
+                && configured != null
+                && actual.isFile()
+                && configured.isFile()
+                && 
actual.getCanonicalFile().equals(configured.getCanonicalFile());
+    }
+
     private void removeSystemScopedDependencies(Set<String> artifactsToRemove, 
List<Dependency> originalDependencies) {
         for (Dependency dependency : originalDependencies) {
             if (dependency.getScope() != null && 
dependency.getScope().equalsIgnoreCase("system")) {
diff --git 
a/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java 
b/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java
index 6a4243c3..9cac31e2 100644
--- a/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java
+++ b/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java
@@ -1524,7 +1524,13 @@ public class MavenJDOMWriter {
             findAndReplaceSimpleElement(innerCount, root, "artifactId", 
value.getArtifactId(), null);
             findAndReplaceSimpleElement(innerCount, root, "groupId", 
value.getGroupId(), null);
             findAndReplaceSimpleElement(innerCount, root, "version", 
value.getVersion(), null);
-            findAndReplaceSimpleElement(innerCount, root, "relativePath", 
value.getRelativePath(), "../pom.xml");
+            String relativePath = value.getRelativePath();
+            if (relativePath != null && relativePath.isEmpty()) {
+                Element rel = updateElement(innerCount, root, "relativePath", 
true);
+                rel.setText("");
+            } else {
+                findAndReplaceSimpleElement(innerCount, root, "relativePath", 
relativePath, "../pom.xml");
+            }
         }
     }
 
diff --git 
a/src/test/java/org/apache/maven/plugins/shade/pom/PomWriterRelativePathTest.java
 
b/src/test/java/org/apache/maven/plugins/shade/pom/PomWriterRelativePathTest.java
new file mode 100644
index 00000000..969124cb
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugins/shade/pom/PomWriterRelativePathTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.plugins.shade.pom;
+
+import java.io.StringWriter;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Parent;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class PomWriterRelativePathTest {
+    @Test
+    public void emptyRelativePathIsWritten() throws Exception {
+        String xml = writeParentRelativePath("");
+
+        assertTrue(xml.contains("<relativePath />"), xml);
+        assertFalse(xml.contains("<relativePath>../pom.xml</relativePath>"), 
xml);
+    }
+
+    @Test
+    public void defaultRelativePathIsOmitted() throws Exception {
+        Model model = new Model();
+        model.setModelVersion("4.0.0");
+        model.setGroupId("g");
+        model.setArtifactId("a");
+        model.setVersion("1.0");
+        Parent parent = new Parent();
+        parent.setGroupId("pg");
+        parent.setArtifactId("pa");
+        parent.setVersion("1.0");
+        model.setParent(parent);
+
+        StringWriter writer = new StringWriter();
+        PomWriter.write(writer, model, true);
+        String xml = writer.toString();
+
+        assertFalse(xml.contains("relativePath"), xml);
+    }
+
+    @Test
+    public void customRelativePathIsWritten() throws Exception {
+        String xml = writeParentRelativePath("../parent/pom.xml");
+
+        
assertTrue(xml.contains("<relativePath>../parent/pom.xml</relativePath>"), xml);
+        assertFalse(xml.contains("<relativePath />"), xml);
+    }
+
+    private static String writeParentRelativePath(String relativePath) throws 
Exception {
+        Model model = new Model();
+        model.setModelVersion("4.0.0");
+        model.setGroupId("g");
+        model.setArtifactId("a");
+        model.setVersion("1.0");
+        Parent parent = new Parent();
+        parent.setGroupId("pg");
+        parent.setArtifactId("pa");
+        parent.setVersion("1.0");
+        parent.setRelativePath(relativePath);
+        model.setParent(parent);
+
+        StringWriter writer = new StringWriter();
+        PomWriter.write(writer, model, true);
+        return writer.toString();
+    }
+}

Reply via email to