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/22 17:33:31 UTC

(maven) branch maven-4.0.x updated: [backport maven-4.0.x] Resolve the parent POM of a source against the directory of its POM (#13684)

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

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


The following commit(s) were added to refs/heads/maven-4.0.x by this push:
     new d826c67f3b [backport maven-4.0.x] Resolve the parent POM of a source against the directory of its POM (#13684)
d826c67f3b is described below

commit d826c67f3b4def06eb67fe1e2d423e607c52845b
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Oct 22 19:33:24 2026 +0200

    [backport maven-4.0.x] Resolve the parent POM of a source against the directory of its POM (#13684)
---
 .../java/org/apache/maven/api/services/Source.java |  17 +--
 .../org/apache/maven/api/services/SourcesTest.java |  17 +++
 .../maven/model/building/FileModelSource.java      |   3 +
 .../maven/internal/impl/DefaultProjectBuilder.java |  43 +++++++-
 .../DefaultProjectBuilderSourceWrapperTest.java    | 119 +++++++++++++++++++++
 5 files changed, 189 insertions(+), 10 deletions(-)

diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java
index 2893faa4e2..05ba0d40dd 100644
--- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java
+++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java
@@ -93,17 +93,20 @@ public interface Source {
      * <p>
      * The resolution strategy depends on the source type:
      * <ul>
-     *   <li>File sources resolve against their parent directory</li>
+     *   <li>Path sources resolve the relative path against their own path, so they are meant to be used on a
+     *       directory. For a file, the result is a path below that file. To resolve against the directory
+     *       containing a file, use {@link ModelSource#resolve(ModelLocator, String)}.</li>
      *   <li>URL sources resolve against their base URL</li>
      *   <li>Other sources may not support resolution and return null</li>
      * </ul>
      * <p>
-     * The implementation must handle:
-     * <ul>
-     *   <li>Both forward and back slashes as path separators</li>
-     *   <li>Parent directory references (..)</li>
-     *   <li>Both file and directory targets</li>
-     * </ul>
+     * A path source passes the relative path to {@link java.nio.file.Path#resolve(String)}, so the path
+     * separators are those of the platform. The resulting path is normalized with
+     * {@link java.nio.file.Path#normalize()}, which removes the {@code ..} references and does not access the file
+     * system.
+     * <p>
+     * Both forward and back slashes are only handled as separators by
+     * {@link ModelSource#resolve(ModelLocator, String)}.
      *
      * @param relative path to resolve relative to this source
      * @return the resolved source, or null if resolution not possible
diff --git a/api/maven-api-core/src/test/java/org/apache/maven/api/services/SourcesTest.java b/api/maven-api-core/src/test/java/org/apache/maven/api/services/SourcesTest.java
index 8b6a566685..d452ea8a34 100644
--- a/api/maven-api-core/src/test/java/org/apache/maven/api/services/SourcesTest.java
+++ b/api/maven-api-core/src/test/java/org/apache/maven/api/services/SourcesTest.java
@@ -88,6 +88,23 @@ void testPathSourceFunctionality() {
         assertEquals(path.resolve("subdir").normalize(), resolved.getPath());
     }
 
+    /**
+     * A path source resolves against its own path, for a file as well as for a directory: the result is a path below
+     * it, not a sibling. Resolution against the parent directory is the job of the model source.
+     */
+    @Test
+    void testPathSourceResolvesAgainstItsOwnPath() {
+        Path pom = Paths.get("/tmp/x/pom.xml");
+        Source source = Sources.fromPath(pom);
+
+        assertEquals(
+                Paths.get("/tmp/x/pom.xml/child.xml"),
+                source.resolve("child.xml").getPath());
+        assertEquals(
+                Paths.get("/tmp/x/other/pom.xml"),
+                source.resolve("../other/pom.xml").getPath());
+    }
+
     @Test
     void testBuildPathSourceFunctionality() {
         // Test build source functionality
diff --git a/compat/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java b/compat/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java
index ef0944e081..c5d00c298f 100644
--- a/compat/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java
+++ b/compat/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java
@@ -71,6 +71,9 @@ public ModelSource2 getRelatedSource(String relPath) {
 
         if (Files.isDirectory(relatedPom)) {
             // TODO figure out how to reuse ModelLocator.locatePom(File) here
+            // so that polyglot ModelParser extensions are honoured.
+            // The new API solves this via ModelSource.resolve(ModelLocator, String).
+            // Same limitation exists in DefaultProjectBuilder.SourceWrapper.getRelatedSource(String).
             relatedPom = relatedPom.resolve("pom.xml");
         }
 
diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java
index 62ae3b9db9..f49e66c09d 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java
@@ -26,6 +26,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
 import java.util.Collection;
 import java.util.List;
@@ -40,6 +42,7 @@
 import org.apache.maven.api.services.ProjectBuilderRequest;
 import org.apache.maven.api.services.ProjectBuilderResult;
 import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.Sources;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.impl.DefaultDependencyResolverResult;
 import org.apache.maven.impl.InternalSession;
@@ -199,7 +202,7 @@ public Optional<DependencyResolverResult> getDependencyResolverResult() {
         }
     }
 
-    private static class SourceWrapper implements ModelSource2 {
+    static class SourceWrapper implements ModelSource2 {
         private final Source source;
 
         SourceWrapper(Source source) {
@@ -216,10 +219,44 @@ public String getLocation() {
             return source.getLocation();
         }
 
+        /**
+         * Returns the POM which the relative path designates, for instance the parent POM of the one of this source.
+         * The path is resolved against the directory which contains the file of this source, like a
+         * {@code FileModelSource} does, and not against the file itself, which {@link Source#resolve(String)} would do.
+         *
+         * @param relPath the path of the related POM, or of its directory, relative to the directory of this source
+         * @return the related source, or {@code null} if this source is not backed by a file, or if there is no
+         *         readable POM at that path
+         */
         @Override
         public ModelSource2 getRelatedSource(String relPath) {
-            Source rel = source.resolve(relPath);
-            return rel != null ? new SourceWrapper(rel) : null;
+            Path path = source.getPath();
+            if (path == null || path.getParent() == null) {
+                // there is no directory to resolve a relative path against
+                return null;
+            }
+
+            // the relative path of a parent may use either kind of slash, whatever the platform is
+            Path related;
+            try {
+                related = path.getParent()
+                        .resolve(relPath.replace('\\', File.separatorChar).replace('/', File.separatorChar));
+            } catch (InvalidPathException e) {
+                return null;
+            }
+
+            if (Files.isDirectory(related)) {
+                // TODO use ModelSource.ModelLocator.locateExistingPom(Path) instead of hardcoding "pom.xml"
+                // so that polyglot ModelParser extensions are honoured.
+                // Same limitation exists in FileModelSource.getRelatedSource(String).
+                related = related.resolve("pom.xml");
+            }
+
+            if (Files.isRegularFile(related) && Files.isReadable(related)) {
+                return new SourceWrapper(Sources.fromPath(related));
+            }
+
+            return null;
         }
 
         @Override
diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultProjectBuilderSourceWrapperTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultProjectBuilderSourceWrapperTest.java
new file mode 100644
index 0000000000..7ceb5a1f73
--- /dev/null
+++ b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultProjectBuilderSourceWrapperTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.internal.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.Sources;
+import org.apache.maven.internal.impl.DefaultProjectBuilder.SourceWrapper;
+import org.apache.maven.model.building.ModelSource2;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Checks that the related POM, which is how the parent POM is found, is resolved against the directory of the POM
+ * of the source, and not against the POM itself.
+ */
+class DefaultProjectBuilderSourceWrapperTest {
+
+    @TempDir
+    Path dir;
+
+    private Path pom(String relative) throws IOException {
+        Path pom = dir.resolve(relative);
+        Files.createDirectories(pom.getParent());
+        Files.writeString(pom, "<project/>");
+        return pom;
+    }
+
+    @Test
+    void testParentPomIsNotTheSamePom() throws IOException {
+        Path parent = pom("pom.xml");
+        Path child = pom("child/pom.xml");
+
+        ModelSource2 related = new SourceWrapper(Sources.fromPath(child)).getRelatedSource("../pom.xml");
+
+        assertNotNull(related);
+        assertEquals(parent.toUri(), related.getLocationURI());
+    }
+
+    @Test
+    void testDirectoryDesignatesItsPom() throws IOException {
+        Path parent = pom("pom.xml");
+        Path child = pom("child/pom.xml");
+
+        ModelSource2 related = new SourceWrapper(Sources.fromPath(child)).getRelatedSource("..");
+
+        assertNotNull(related);
+        assertEquals(parent.toUri(), related.getLocationURI());
+    }
+
+    @Test
+    void testBackslashesAreSeparators() throws IOException {
+        pom("a/b/pom.xml");
+        Path child = pom("c/pom.xml");
+
+        ModelSource2 related = new SourceWrapper(Sources.fromPath(child)).getRelatedSource("..\\a\\b\\pom.xml");
+
+        assertNotNull(related);
+        assertEquals(dir.resolve("a/b/pom.xml").toUri(), related.getLocationURI());
+    }
+
+    @Test
+    void testMissingPomIsNull() throws IOException {
+        Path child = pom("child/pom.xml");
+
+        assertNull(new SourceWrapper(Sources.fromPath(child)).getRelatedSource("../pom.xml"));
+    }
+
+    @Test
+    void testSourceWithoutFileIsNull() {
+        Source inMemory = new Source() {
+            @Override
+            public Path getPath() {
+                return null;
+            }
+
+            @Override
+            public InputStream openStream() {
+                return InputStream.nullInputStream();
+            }
+
+            @Override
+            public String getLocation() {
+                return "<memory>";
+            }
+
+            @Override
+            public Source resolve(String relative) {
+                return null;
+            }
+        };
+
+        assertNull(new SourceWrapper(inMemory).getRelatedSource("../pom.xml"));
+    }
+}