elharo commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2080293469


##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,75 +71,74 @@
 public class DefaultModelProcessor implements ModelProcessor {
 
     private final ModelXmlFactory modelXmlFactory;
-    private final List<ModelParser> modelParsers;
+    private final @Nullable List<ModelParser> modelParsers;
 
-    @Inject
     public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable 
List<ModelParser> modelParsers) {
         this.modelXmlFactory = modelXmlFactory;
         this.modelParsers = modelParsers;
     }
 
+    /**
+     * @implNote The ModelProcessor#locatePom never returns null while the 
ModelParser#locatePom needs to return an existing path!
+     */
     @Override
     public Path locateExistingPom(Path projectDirectory) {
-        // Note that the ModelProcessor#locatePom never returns null
-        // while the ModelParser#locatePom needs to return an existing path!
-        Path pom = modelParsers.stream()
-                .map(m -> m.locate(projectDirectory)
-                        .map(org.apache.maven.api.services.Source::getPath)
-                        .orElse(null))
-                .filter(Objects::nonNull)
+        return modelParsers.stream()
+                .map(parser -> parser.locate(projectDirectory))
+                .filter(Optional::isPresent)
+                .map(Optional::get)
+                .map(Source::getPath)
+                .peek(path -> throwIfWrongProjectDirLocation(path, 
projectDirectory))
                 .findFirst()
-                .orElseGet(() -> doLocateExistingPom(projectDirectory));
+                .orElseGet(() -> 
locateExistingPomWithUserDirDefault(projectDirectory));
+    }
+
+    private static void throwIfWrongProjectDirLocation(Path pom, Path 
projectDirectory) {
         if (pom != null && !pom.equals(projectDirectory) && 
!pom.getParent().equals(projectDirectory)) {
             throw new IllegalArgumentException("The POM found does not belong 
to the given directory: " + pom);
         }
-        return pom;
+    }
+
+    private Path locateExistingPomWithUserDirDefault(Path project) {
+        return locateExistingPomInDirOrFile(project != null ? project : 
Paths.get(System.getProperty("user.dir")));
+    }
+
+    private static Path locateExistingPomInDirOrFile(Path project) {
+        return Files.isDirectory(project) ? 
isRegularFileOrNull(project.resolve("pom.xml")) : project;
+    }
+
+    private static Path isRegularFileOrNull(Path pom) {
+        return Files.isRegularFile(pom) ? pom : null;
     }
 
     @Override
     public Model read(XmlReaderRequest request) throws IOException {
         Objects.requireNonNull(request, "source cannot be null");

Review Comment:
   source --> request



-- 
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: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to