Repository: quarkusio/quarkus
Issue: cli test fails without settings.xml

### Describe the bug

If no settings.xml is present in the maven user location, cli test will fail with 3 failures and 3 errors.

```shell
./mvnw -f devtools/cli test
...
[ERROR] Tests run: 79, Failures: 3, Errors: 3, Skipped: 0
```
[Full Maven Log](https://github.com/quarkusio/quarkus/files/9607348/maven.log)


### Expected behavior

cli test should be able to run without settings.xml

### Actual behavior

Fails without settings.xml

### How to Reproduce?

Steps to reproduce the behavior:
1. Ensure that no user settings.xml is defined
2. Run `./mvnw -f devtools/cli test`

### Output of `uname -a` or `ver`

Darwin mavenrunner-mac 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64

### Output of `java -version`

openjdk version "17.0.2" 2022-01-18

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

e19f65e535

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)

### Additional information

_No response_

Changed files:
devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java, devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java, devtools/cli/src/test/java/io/quarkus/cli/RegistryClientBuilderTestBase.java

diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
index d8bcb2b29ab..2c801a99990 100644
--- a/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
+++ b/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
@@ -1,6 +1,7 @@
 package io.quarkus.cli.build;
 
 import java.io.File;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
@@ -32,6 +33,7 @@
 import picocli.CommandLine;
 
 public class MavenRunner implements BuildSystemRunner {
+    public static String MAVEN_SETTINGS = "maven.settings";
     static final String[] windowsWrapper = { "mvnw.cmd", "mvnw.bat" };
     static final String otherWrapper = "mvnw";
 
@@ -238,13 +240,13 @@ void setMavenProperties(ArrayDeque<String> args, boolean batchMode) {
             args.addFirst("-Dstyle.color=always");
         }
 
-        String mavenSettings = propertiesOptions.properties.remove("maven.settings");
+        String mavenSettings = propertiesOptions.properties.remove(MAVEN_SETTINGS);
         if (mavenSettings != null && !mavenSettings.isEmpty()) {
             args.add("-s");
             args.add(mavenSettings);
         } else {
-            mavenSettings = System.getProperty("maven.settings");
-            if (mavenSettings != null && !mavenSettings.isEmpty()) {
+            mavenSettings = System.getProperty(MAVEN_SETTINGS);
+            if (mavenSettings != null && !mavenSettings.isEmpty() && Files.exists(Path.of(mavenSettings))) {
                 args.add("-s");
                 args.add(mavenSettings);
             }
diff --git a/devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java b/devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java
index ddf060402c3..2ee05931bef 100644
--- a/devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java
+++ b/devtools/cli/src/test/java/io/quarkus/cli/CliDriver.java
@@ -1,5 +1,8 @@
 package io.quarkus.cli;
 
+import static io.quarkus.cli.build.MavenRunner.MAVEN_SETTINGS;
+import static org.apache.maven.cli.MavenCli.LOCAL_REPO_PROPERTY;
+
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.PrintStream;
@@ -12,6 +15,9 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
+import java.util.function.BinaryOperator;
+import java.util.function.UnaryOperator;
 
 import org.junit.jupiter.api.Assertions;
 
@@ -20,6 +26,9 @@
 public class CliDriver {
     static final PrintStream stdout = System.out;
     static final PrintStream stderr = System.err;
+    private static final BinaryOperator<String> ARG_FORMATTER = (key, value) -> "-D" + key + "=" + value;
+    private static final UnaryOperator<String> REPO_ARG_FORMATTER = value -> ARG_FORMATTER.apply(LOCAL_REPO_PROPERTY, value);
+    private static final UnaryOperator<String> SETTINGS_ARG_FORMATTER = value -> ARG_FORMATTER.apply(MAVEN_SETTINGS, value);
 
     public static class CliDriverBuilder {
 
@@ -63,8 +72,10 @@ public Result execute() throws Exception {
                 newArgs.subList(index, newArgs.size()).clear();
             }
 
-            propagateProperty("maven.repo.local", mavenLocalRepo, newArgs);
-            propagateProperty("maven.settings", mavenSettings, newArgs);
+            Optional.ofNullable(mavenLocalRepo).or(CliDriver::getMavenLocalRepoProperty).map(REPO_ARG_FORMATTER)
+                    .ifPresent(newArgs::add);
+            Optional.ofNullable(mavenSettings).or(CliDriver::getMavenSettingsProperty).map(SETTINGS_ARG_FORMATTER)
+                    .ifPresent(newArgs::add);
 
             newArgs.add("--cli-test");
             newArgs.add("--cli-test-dir");
@@ -81,7 +92,7 @@ public Result execute() throws Exception {
             PrintStream errPs = new PrintStream(err);
             System.setErr(errPs);
 
-            final Map<String, String> originalProps = collectOverridenProps(newArgs);
+            final Map<String, String> originalProps = collectOverriddenProps(newArgs);
 
             Result result = new Result();
             QuarkusCli cli = new QuarkusCli();
@@ -109,7 +120,7 @@ protected void resetProperties(Map<String, String> originalProps) {
             }
         }
 
-        protected Map<String, String> collectOverridenProps(List<String> newArgs) {
+        protected Map<String, String> collectOverriddenProps(List<String> newArgs) {
             final Map<String, String> originalProps = new HashMap<>();
             for (String s : newArgs) {
                 if (s.startsWith("-D")) {
@@ -121,22 +132,14 @@ protected Map<String, String> collectOverridenProps(List<String> newArgs) {
                             originalProps.put(propName, origValue);
                         } else if (System.getProperties().contains(propName)) {
                             originalProps.put(propName, "true");
+                        } else {
+                            originalProps.put(propName, null);
                         }
                     }
                 }
             }
             return originalProps;
         }
-
-        private static void propagateProperty(String propName, String testValue, List<String> args) {
-            if (testValue == null) {
-                testValue = System.getProperty(propName);
-                if (testValue == null) {
-                    return;
-                }
-            }
-            args.add("-D" + propName + "=" + testValue);
-        }
     }
 
     public static CliDriverBuilder builder() {
@@ -144,14 +147,8 @@ public static CliDriverBuilder builder() {
     }
 
     public static void preserveLocalRepoSettings(Collection<String> args) {
-        String s = convertToProperty("maven.repo.local");
-        if (s != null) {
-            args.add(s);
-        }
-        s = convertToProperty("maven.settings");
-        if (s != null) {
-            args.add(s);
-        }
+        getMavenLocalRepoProperty().map(REPO_ARG_FORMATTER).ifPresent(args::add);
+        getMavenSettingsProperty().map(SETTINGS_ARG_FORMATTER).ifPresent(args::add);
     }
 
     public static Result executeArbitraryCommand(Path startingDir, String... args) throws Exception {
@@ -439,12 +436,12 @@ public static void validateApplicationProperties(Path projectRoot, List<String>
                 "Properties file should contain " + conf + ". Found:\\n" + propertiesFile));
     }
 
-    private static String convertToProperty(String name) {
-        String value = System.getProperty(name);
-        if (value != null) {
-            return "-D" + name + "=" + value;
-        }
-        return null;
+    private static Optional<String> getMavenLocalRepoProperty() {
+        return Optional.ofNullable(System.getProperty(LOCAL_REPO_PROPERTY));
+    }
+
+    private static Optional<String> getMavenSettingsProperty() {
+        return Optional.ofNullable(System.getProperty(MAVEN_SETTINGS)).filter(value -> Files.exists(Path.of(value)));
     }
 
     private static void retryDelete(File file) {
diff --git a/devtools/cli/src/test/java/io/quarkus/cli/RegistryClientBuilderTestBase.java b/devtools/cli/src/test/java/io/quarkus/cli/RegistryClientBuilderTestBase.java
index 00a402bcda5..d101dbe0018 100644
--- a/devtools/cli/src/test/java/io/quarkus/cli/RegistryClientBuilderTestBase.java
+++ b/devtools/cli/src/test/java/io/quarkus/cli/RegistryClientBuilderTestBase.java
@@ -1,7 +1,7 @@
 package io.quarkus.cli;
 
-import java.io.BufferedReader;
 import java.io.BufferedWriter;
+import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.nio.file.Files;
@@ -59,7 +59,7 @@ static void setup() throws Exception {
 
         final BootstrapMavenContext mavenContext = new BootstrapMavenContext(
                 BootstrapMavenContext.config().setWorkspaceDiscovery(false));
-        final Settings settings = getBaseMavenSettings(mavenContext.getUserSettings().toPath());
+        final Settings settings = getBaseMavenSettings(mavenContext.getUserSettings());
 
         Profile profile = new Profile();
         settings.addActiveProfile("qs-test-registry");
@@ -106,11 +106,9 @@ protected static String getCurrentQuarkusVersion() {
         return v;
     }
 
-    private static Settings getBaseMavenSettings(Path mavenSettings) throws IOException {
-        if (Files.exists(mavenSettings)) {
-            try (BufferedReader reader = Files.newBufferedReader(mavenSettings)) {
-                return new DefaultSettingsReader().read(reader, Map.of());
-            }
+    private static Settings getBaseMavenSettings(File mavenSettings) throws IOException {
+        if (mavenSettings != null && mavenSettings.exists()) {
+            return new DefaultSettingsReader().read(mavenSettings, Map.of());
         }
         return new Settings();
     }