gnodet commented on code in PR #13538:
URL: https://github.com/apache/maven/pull/13538#discussion_r4286855266


##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java:
##########
@@ -216,10 +219,41 @@ 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)) {
+                related = related.resolve("pom.xml");
+            }

Review Comment:
   This has the same hardcoded `"pom.xml"` limitation as 
`FileModelSource.getRelatedSource(String)` (line 73). Polyglot `ModelParser` 
extensions won't be consulted.
   
   The new API solves this properly via `ModelSource.resolve(ModelLocator, 
String)` which consults registered `ModelParser` implementations before falling 
back to `pom.xml`.
   
   ```suggestion
               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");
               }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to