From 08e996bb28266a5b1707f45d20809ba44117e16a Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 30 Jun 2023 15:27:58 +0200 Subject: [PATCH] Fix search for topDirectory when using -f / --file --- .../main/java/org/apache/maven/cli/MavenCli.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 5070deffa1..a4ad1d23fa 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -333,7 +333,7 @@ public class MavenCli { for (String arg : cliRequest.args) { if (isAltFile) { // this is the argument following -f/--file - Path path = topDirectory.resolve(arg); + Path path = topDirectory.resolve(stripLeadingAndTrailingQuotes(arg)); if (Files.isDirectory(path)) { topDirectory = path; } else if (Files.isRegularFile(path)) { @@ -351,7 +351,7 @@ public class MavenCli { break; } else { // Check if this is the -f/--file option - isAltFile = arg.equals(String.valueOf(CLIManager.ALTERNATE_POM_FILE)) || arg.equals("file"); + isAltFile = arg.equals("-f") || arg.equals("--file"); } } topDirectory = getCanonicalPath(topDirectory); @@ -1616,6 +1616,18 @@ public class MavenCli { return interpolator; } + private static String stripLeadingAndTrailingQuotes(String str) { + final int length = str.length(); + if (length > 1 + && str.startsWith("\"") + && str.endsWith("\"") + && str.substring(1, length - 1).indexOf('"') == -1) { + str = str.substring(1, length - 1); + } + + return str; + } + private static Path getCanonicalPath(Path path) { try { return path.toRealPath();