Repository: quarkusio/quarkus
Issue: Failure to pull non-cached deps during test launch with proxy private repository

### Describe the bug

Quarkus (3.x) fails to pull dependencies from a private repository during tests, with this error:

```
Caused by: org.apache.http.client.HttpResponseException: status code: 401, reason phrase: Unauthorized (401)
        at org.eclipse.aether.transport.http.HttpTransporter.handleStatus(HttpTransporter.java:527)
        at org.eclipse.aether.transport.http.HttpTransporter.execute(HttpTransporter.java:396)
        at org.eclipse.aether.transport.http.HttpTransporter.implGet(HttpTransporter.java:343)
        at org.eclipse.aether.spi.connector.transport.AbstractTransporter.get(AbstractTransporter.java:64)
        at org.eclipse.aether.connector.basic.BasicRepositoryConnector$GetTaskRunner.runTask(BasicRepositoryConnector.java:482)
        at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:414)
        ... 67 more
```

This happens if during the test launch, quarkus needs some extra dependency that maven couldn't resolve and cache (typically the `*-deployment` libraries).

The problem is because it tries to pull dependencies from repository defined in the settings.xml; but somehow misses the settings-security.xml setup. If explicitly providing such setup to maven, it works (similar problem can also be replicated in the IDEs)

The error is pretty obscure, because success or failure depends on having the dependency locally cached or not by just mere chance. So upgrading to a newer Quarkus version might make it fail (because of transitive dependency changes). This tipically hits pipelines quite hard and make usage of maven args or parameters mandatory in CI/CD and cumbersome for local dev. In CI/CD this can be overcome with workarounds such as `MAVEN_ARGS = "-Dsettings.security=~/.m2/settings-security.xml"` (if using maven >= 3.9).

Using pom.xml-> surefire config -> systemPropertyVariables -> `<settings.security>${settings.security}</settings.security>` doesn't work (looks like no system property used there works but /shrug)

Link to reproducer that's just a copy of the `quarkus-quickstarts/getting-started:3.1.3.Final` [project ](https://github.com/quarkusio/quarkus-quickstarts/tree/main/getting-started).

https://github.com/agseijas/maven-security-quarkus/blob/master/README.md


### Expected behavior

The `GreetingResourceTest` test passes and the build ends successfully with maven

### Actual behavior

Error pulling dependencies during test launch:

```text
Caused by: org.apache.http.client.HttpResponseException: status code: 401, reason phrase: Unauthorized (401)
        at org.eclipse.aether.transport.http.HttpTransporter.handleStatus(HttpTransporter.java:527)
        at org.eclipse.aether.transport.http.HttpTransporter.execute(HttpTransporter.java:396)
        at org.eclipse.aether.transport.http.HttpTransporter.implGet(HttpTransporter.java:343)
        at org.eclipse.aether.spi.connector.transport.AbstractTransporter.get(AbstractTransporter.java:64)
        at org.eclipse.aether.connector.basic.BasicRepositoryConnector$GetTaskRunner.runTask(BasicRepositoryConnector.java:482)
        at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:414)
        ... 67 more
```


### How to Reproduce?

Check the README file that explains the steps and tools setup needed to reproduce the issue:

https://github.com/agseijas/maven-security-quarkus/blob/master/README.md

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

openjdk version "20.0.1" 2023-04-18

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

3.1.3.Final

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Apache Maven 3.9.3 (21122926829f1ead511c958d89bd2f672198ae9f)

### Additional information

_No response_

Changed files:
independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java

diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java
index a6e7f45eb3a..712de094100 100644
--- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java
+++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java
@@ -95,6 +95,7 @@ public class BootstrapMavenContext {
     private static final String MAVEN_SETTINGS = "maven.settings";
     private static final String MAVEN_TOP_LEVEL_PROJECT_BASEDIR = "maven.top-level-basedir";
     private static final String SETTINGS_XML = "settings.xml";
+    private static final String SETTINGS_SECURITY = "settings.security";
 
     private static final String EFFECTIVE_MODEL_BUILDER_PROP = "quarkus.bootstrap.effective-model-builder";
 
@@ -271,11 +272,17 @@ public boolean isOffline() throws BootstrapMavenException {
     }
 
     public RepositorySystem getRepositorySystem() throws BootstrapMavenException {
-        return repoSystem == null ? repoSystem = newRepositorySystem() : repoSystem;
+        if (repoSystem == null) {
+            initRepoSystemAndManager();
+        }
+        return repoSystem;
     }
 
     public RemoteRepositoryManager getRemoteRepositoryManager() {
-        return remoteRepoManager == null ? remoteRepoManager = newRemoteRepositoryManager() : remoteRepoManager;
+        if (remoteRepoManager == null) {
+            initRepoSystemAndManager();
+        }
+        return remoteRepoManager;
     }
 
     public RepositorySystemSession getRepositorySystemSession() throws BootstrapMavenException {
@@ -291,7 +298,10 @@ public List<RemoteRepository> getRemotePluginRepositories() throws BootstrapMave
     }
 
     private SettingsDecrypter getSettingsDecrypter() {
-        return settingsDecrypter == null ? settingsDecrypter = newSettingsDecrypter() : settingsDecrypter;
+        if (settingsDecrypter == null) {
+            initRepoSystemAndManager();
+        }
+        return settingsDecrypter;
     }
 
     public Settings getEffectiveSettings() throws BootstrapMavenException {
@@ -463,7 +473,17 @@ private DefaultRepositorySystemSession newRepositorySystemSession() throws Boots
         final SettingsDecryptionRequest decrypt = new DefaultSettingsDecryptionRequest();
         decrypt.setProxies(settings.getProxies());
         decrypt.setServers(settings.getServers());
+        // set settings.security property to ~/.m2/settings-security.xml unless it's already set to some other value
+        File settingsSecurityXml = null;
+        final boolean setSettingsSecurity = !System.getProperties().contains(SETTINGS_SECURITY)
+                && ((settingsSecurityXml = new File(getUserMavenConfigurationHome(), "settings-security.xml")).exists());
+        if (setSettingsSecurity) {
+            System.setProperty(SETTINGS_SECURITY, settingsSecurityXml.toString());
+        }
         final SettingsDecryptionResult decrypted = getSettingsDecrypter().decrypt(decrypt);
+        if (setSettingsSecurity) {
+            System.clearProperty(SETTINGS_SECURITY);
+        }
         if (!decrypted.getProblems().isEmpty() && log.isDebugEnabled()) {
             // this is how maven handles these
             for (SettingsProblem p : decrypted.getProblems()) {
@@ -842,21 +862,6 @@ private static Proxy toAetherProxy(org.apache.maven.settings.Proxy proxy) {
         return new Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), auth);
     }
 
-    private RepositorySystem newRepositorySystem() throws BootstrapMavenException {
-        initRepoSystemAndManager();
-        return repoSystem;
-    }
-
-    public RemoteRepositoryManager newRemoteRepositoryManager() {
-        initRepoSystemAndManager();
-        return remoteRepoManager;
-    }
-
-    private SettingsDecrypter newSettingsDecrypter() {
-        initRepoSystemAndManager();
-        return settingsDecrypter;
-    }
-
     private void initRepoSystemAndManager() {
         final MavenFactory factory = configureMavenFactory();
         if (repoSystem == null) {
@@ -871,7 +876,7 @@ private void initRepoSystemAndManager() {
     }
 
     protected MavenFactory configureMavenFactory() {
-        final MavenFactory factory = MavenFactory.create(RepositorySystem.class.getClassLoader(), builder -> {
+        return MavenFactory.create(RepositorySystem.class.getClassLoader(), builder -> {
             builder.addBean(ModelBuilder.class).setSupplier(new BeanSupplier<ModelBuilder>() {
                 @Override
                 public ModelBuilder get(Scope scope) {
@@ -879,7 +884,6 @@ public ModelBuilder get(Scope scope) {
                 }
             }).setPriority(100).build();
         });
-        return factory;
     }
 
     private static String getUserAgent() {