You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2026/08/06 11:57:32 UTC

(maven-resolver-ant-tasks) branch master updated: Apply only the active settings.xml proxy (#165) (#175)

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver-ant-tasks.git


The following commit(s) were added to refs/heads/master by this push:
     new a1308ac  Apply only the active settings.xml proxy (#165) (#175)
a1308ac is described below

commit a1308ac2f8a4503faa2bf925e90d7ca71cfb03bc
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Thu Aug 6 11:57:26 2026 +0000

    Apply only the active settings.xml proxy (#165) (#175)
---
 .../maven/resolver/internal/ant/AntRepoSys.java    |  11 +-
 .../resolver/internal/ant/ProxySelectorTest.java   | 145 +++++++++++++++++++++
 2 files changed, 151 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/maven/resolver/internal/ant/AntRepoSys.java b/src/main/java/org/apache/maven/resolver/internal/ant/AntRepoSys.java
index 14e2719..fa31322 100644
--- a/src/main/java/org/apache/maven/resolver/internal/ant/AntRepoSys.java
+++ b/src/main/java/org/apache/maven/resolver/internal/ant/AntRepoSys.java
@@ -418,14 +418,15 @@ public class AntRepoSys {
         }
 
         Settings settings = getSettings();
-        for (org.apache.maven.settings.Proxy proxy : settings.getProxies()) {
+        org.apache.maven.settings.Proxy activeProxy = settings.getActiveProxy();
+        if (activeProxy != null) {
             AuthenticationBuilder auth = new AuthenticationBuilder();
-            auth.addUsername(proxy.getUsername()).addPassword(proxy.getPassword());
+            auth.addUsername(activeProxy.getUsername()).addPassword(activeProxy.getPassword());
             selector.add(
                     new org.eclipse.aether.repository.Proxy(
-                            proxy.getProtocol(), proxy.getHost(),
-                            proxy.getPort(), auth.build()),
-                    proxy.getNonProxyHosts());
+                            activeProxy.getProtocol(), activeProxy.getHost(),
+                            activeProxy.getPort(), auth.build()),
+                    activeProxy.getNonProxyHosts());
         }
 
         return selector;
diff --git a/src/test/java/org/apache/maven/resolver/internal/ant/ProxySelectorTest.java b/src/test/java/org/apache/maven/resolver/internal/ant/ProxySelectorTest.java
new file mode 100644
index 0000000..20c2046
--- /dev/null
+++ b/src/test/java/org/apache/maven/resolver/internal/ant/ProxySelectorTest.java
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.resolver.internal.ant;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+
+import junit.framework.JUnit4TestAdapter;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+import org.eclipse.aether.RepositorySystemSession;
+import org.eclipse.aether.repository.Proxy;
+import org.eclipse.aether.repository.ProxySelector;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class ProxySelectorTest {
+    public static junit.framework.Test suite() {
+        return new JUnit4TestAdapter(ProxySelectorTest.class);
+    }
+
+    @Rule
+    public final TemporaryFolder folder = new TemporaryFolder();
+
+    private Project project;
+
+    private Task task;
+
+    @Before
+    public void setUp() {
+        project = new Project();
+        project.setProperty("user.home", System.getProperty("user.home"));
+        project.setProperty(
+                "maven.repo.local",
+                new File(new File("").getAbsoluteFile(), "target/ant/local-repo").getAbsolutePath());
+
+        task = new Task() {};
+        task.setProject(project);
+    }
+
+    /**
+     * A proxy declared in settings.xml with {@code <active>false</active>} must not be applied to transfers. Only the
+     * active proxy (the first one with {@code isActive() == true}) must be added to the selector.
+     *
+     * @throws Exception in case of problems
+     */
+    @Test
+    public void testInactiveProxyFromSettingsIsNotApplied() throws Exception {
+        File settings = writeSettings(
+                "<proxy>",
+                "  <id>inactive-proxy</id>",
+                "  <active>false</active>",
+                "  <protocol>http</protocol>",
+                "  <host>inactive-proxy.example.com</host>",
+                "  <port>8080</port>",
+                "</proxy>",
+                "<proxy>",
+                "  <id>active-proxy</id>",
+                "  <active>true</active>",
+                "  <protocol>http</protocol>",
+                "  <host>active-proxy.example.com</host>",
+                "  <port>8081</port>",
+                "</proxy>");
+
+        AntRepoSys repoSys = AntRepoSys.getInstance(project);
+        repoSys.setUserSettings(settings);
+
+        try (RepositorySystemSession.CloseableSession session = repoSys.getSession(task, null)) {
+            Proxy proxy = proxyForCentral(session);
+            assertNotNull(proxy);
+            assertEquals("active-proxy.example.com", proxy.getHost());
+            assertEquals(8081, proxy.getPort());
+        }
+    }
+
+    /**
+     * When settings.xml declares only inactive proxies, no proxy may be applied to transfers.
+     *
+     * @throws Exception in case of problems
+     */
+    @Test
+    public void testOnlyInactiveProxiesApplyNoProxy() throws Exception {
+        File settings = writeSettings(
+                "<proxy>",
+                "  <id>inactive-proxy</id>",
+                "  <active>false</active>",
+                "  <protocol>http</protocol>",
+                "  <host>inactive-proxy.example.com</host>",
+                "  <port>8080</port>",
+                "</proxy>");
+
+        AntRepoSys repoSys = AntRepoSys.getInstance(project);
+        repoSys.setUserSettings(settings);
+
+        try (RepositorySystemSession.CloseableSession session = repoSys.getSession(task, null)) {
+            assertNull(proxyForCentral(session));
+        }
+    }
+
+    private Proxy proxyForCentral(RepositorySystemSession session) {
+        ProxySelector selector = session.getProxySelector();
+        RemoteRepository repo =
+                new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
+        return selector.getProxy(repo);
+    }
+
+    private File writeSettings(String... proxyElements) throws Exception {
+        File file = folder.newFile("settings.xml");
+        StringBuilder content = new StringBuilder();
+        content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
+        content.append("<settings>\n");
+        content.append("  <proxies>\n");
+        for (String element : proxyElements) {
+            content.append("    ").append(element).append('\n');
+        }
+        content.append("  </proxies>\n");
+        content.append("</settings>\n");
+        Files.write(file.toPath(), content.toString().getBytes(StandardCharsets.UTF_8));
+        return file;
+    }
+}