[MNG-8159] Fix search for topDirectory when using -f / --file (#1589)

Backport 08e996bb28

Co-authored-by: Guillaume Nodet <gnodet@gmail.com>

---

https://issues.apache.org/jira/browse/MNG-8159
This commit is contained in:
James Z.M. Gao 2024-06-20 14:43:05 +08:00 committed by GitHub
parent 1b3828e05b
commit 6e8550cd6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 2 deletions

View File

@ -325,7 +325,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)) {
@ -343,7 +343,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);
@ -1593,6 +1593,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();