How to configure a proxy server for both HTTP and HTTPS in Maven's settings.xml?

40,147

Solution 1

Maven proxy from settings.xml is used for both http and https, so you just need to define one proxy server and it will be used for both, you just need to leave only one proxy tag, like this:

<proxies>
    <proxy>
        <id>myhttpproxy</id>
        <active>true</active>
        <protocol>http</protocol>
        <host>192.168.1.2</host>
        <port>3128</port>
        <nonProxyHosts>localhost</nonProxyHosts>
    </proxy>
</proxies>

The protocol above is the protocol of the proxy server, not the proxied request.

Solution 2

Update 2022-01: a more up-to-date answer/solution for the current tech-stack (Eclipse 4.17 / 2020-09) can be found here (old: 4.6 / 2017)

It works without the extra ...<id>httpsproxy</id>... entry (as @Krzysztof Krasoń mentioned) and with it (as the asker stated). The problem for us was, that the Eclipse->Maven->User Settings->[x] Update Settings was obviously not working at all and to test certain things Eclipse->Maven->[x] Download repository index updates on startup must be checked (e.g. Maven Repositories View->Global Repositories->central->Update Index). And most of all:

Eclipse must be restarted after every settings.xml update! :-/

I guess it's a bug or reload/caching issue. We successfully tested it with

  • Kepler (4.3) and Neon (4.6)
  • and their embedded Maven versions (3.2.1 / 3.3.9) as well as an external 3.3.3
  • with http:// and https:// URLs

Solution 3

My tests with Eclipse Maven show that the protocol in settings.xml is referring to the protocol of the proxy server, not the protocol of the URL request. It also shows that Maven only uses the first active proxy server listed, and ignores the rest.

Here's my evidence:

1. The documentation says that

active: true if this proxy is active. This is useful for declaring a set of proxies, but only one may be active at a time.

protocol, host, port: The protocol://host:port of the proxy, separated into discrete elements."

2. The source code is even clearer:

    /**
     * Get the protocol of the proxy server.
     * @return the protocol of the proxy server
     */
    public String getProtocol()
    {
        return protocol;
    }

3. Real world tests (using Eclipse Maven):

a. 1st proxy is a bogus ftp, 2nd is real http, 3rd is real https. Result: FAIL.

If the protocol were for the URL request, then Maven would've looked up the real http/https proxies and worked perfectly fine. Instead, it used the 1st proxy even though it was "ftp", and failed.

    <proxies>
        <proxy>
            <id>bogus_ftp</id>
            <active>true</active>
            <protocol>ftp</protocol>
            <port>123</port>
            <host>bogus.proxy.com</host>
        </proxy>
        <proxy>
            <id>real_http</id>
            <active>true</active>
            <protocol>http</protocol>
            <port>123</port>
            <host>real.proxy.com</host>
        </proxy>
        <proxy>
            <id>real_https</id>
            <active>true</active>
            <protocol>https</protocol>
            <port>123</port>
            <host>real.proxy.com</host>
        </proxy>
    </proxies>

b. 1st proxy is real http, 2nd is bogus https. Result: SUCCESS.

This shows that it only used the 1st proxy. Otherwise, it would have used the 2nd proxy for https requests, hit the bogus proxy server, and failed.

    <proxies>
        <proxy>
            <id>real_http</id>
            <active>true</active>
            <protocol>http</protocol>
            <port>123</port>
            <host>real.proxy.com</host>
        </proxy>
        <proxy>
            <id>bogus_https</id>
            <active>true</active>
            <protocol>https</protocol>
            <port>123</port>
            <host>bogus.proxy.com</host>
        </proxy>
    </proxies>

c. Both are http, but 1st proxy is bogus, 2nd is real. Result: FAIL.

This shows that maven doesn't use multiple proxies, even for the same protocol. Otherwise, it would have tried the 2nd real proxy and succeeded.

    <proxies>
        <proxy>
            <id>bogus_http</id>
            <active>true</active>
            <protocol>http</protocol>
            <port>123</port>
            <host>bogus.proxy.com</host>
        </proxy>
        <proxy>
            <id>real_http</id>
            <active>true</active>
            <protocol>http</protocol>
            <port>123</port>
            <host>real.proxy.com</host>
        </proxy>
    </proxies>

Solution 4

I solved the problem with updating the maven version, or in other words not using the embedded eclipse maven version, but external version 3.3.9.

Share:
40,147
Stephen Hartley
Author by

Stephen Hartley

Updated on February 11, 2022

Comments

  • Stephen Hartley
    Stephen Hartley about 2 years

    I'm using Maven 3.1.1 behind a proxy server. The same proxy handles both HTTP and HTTPS traffic.

    I can't seem to tell maven using settings.xml to use both protocols. It seems to me that it is only possible to have one active proxy, as whichever active proxy is defined first is used, and subsequent 'active' proxy definitions are ignored. This is my settings.xml:

    <proxies>
        <proxy>
            <id>myhttpproxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>192.168.1.2</host>
            <port>3128</port>
            <nonProxyHosts>localhost</nonProxyHosts>
        </proxy>
        <proxy>
            <id>myhttpsproxy</id>
            <active>true</active>
            <protocol>https</protocol>
            <host>192.168.1.2</host>
            <port>3128</port>
            <nonProxyHosts>localhost</nonProxyHosts>
        </proxy>
    </proxies>
    

    Is it possible to configure a proxy for both HTTP and HTTPS in maven's settings.xml? I'm aware that I could workaround this by passing Java system properties to the maven invocation such as:

    -Dhttps.proxyHost=192.168.1.2 -Dhttps.proxyPort=3128
    

    but surely this must be possible from within settings.xml?

    Maven bugs raised such as MNG-2305 and MNG-4394 suggest this issue is resolved, but I am not convinced.

    Alternatively, is there a "proxy proxy" I could run locally that I could point maven to? The "proxy proxy" would route http/https accordingly. Even so, I would still need to define two active proxy definitions in settings.xml for Maven to direct both types of traffic.

  • Silver Quettier
    Silver Quettier almost 8 years
    Very useful information, a shame you were not credited more for it. +1, worked for me.
  • Stephen Hartley
    Stephen Hartley almost 8 years
    @krzyk maven's settings documentation would seem to agree with you: protocol, host, port: The protocol://host:port of the proxy, separated into discrete elements. I will attempt to re-test this - it has been some time now...!
  • Sorin Postelnicu
    Sorin Postelnicu over 7 years
    I know this looks like the protocol refers to the proxy's protocol, but for me it did not work with only one proxy. When I have added two proxies, like the OP suggested, then for me it worked (with Maven 3.3.9). At first it didn't work because I copy/pasted the proxy server in settings.xml and I forgot to put a different id for the second proxy server. After I put a different id, it worked.
  • Krzysztof Krasoń
    Krzysztof Krasoń about 7 years
    @StephenHartley Have you had a chance to test it out?
  • Max Robbertze
    Max Robbertze about 7 years
    Does not work. You need to proxy entries, one for HTTP, one for HTTPS, but you must use the ID tags, with different IDs, else the one will be ignored
  • wisbucky
    wisbucky over 5 years
    I posted my answer of real world tests using Eclipse Maven that showed that protocol is indeed for the proxy server and not the url request, and that maven only uses the first active proxy and ignores any others. stackoverflow.com/a/52508940 . @SorinPostelnicu @StephenHartley @MaxRobbertze
  • Sorin Postelnicu
    Sorin Postelnicu over 5 years
    Dear @wisbucky, in all your 3 tests you forgot to specify different ids for the proxy declarations. If you specify different ids then it works. Probably the problem is that Maven generates a default id when missing, and it generates the same id for all the proxy declarations. Could you please try again all your examples but with valid distinct ids, and tell us if it's indeed working with more than one active proxy?
  • wisbucky
    wisbucky over 5 years
    @SorinPostelnicu, That's a good point. I re-tested all 3 examples with distinct <id>s (I edited the post to reflect this). Exact same results as before. You can probably take advantage of the ids with command line arguments, but Eclipse Maven does not by default.
  • Sorin Postelnicu
    Sorin Postelnicu over 5 years
    It works when running mvn from the command line, but indeed I did not test with Eclipse Maven. If it does not work, then it should be filed as a bug for Elipse Maven.