简体   繁体   中英

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

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.

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.

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 (eg 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

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>

我解决了更新maven版本的问题,换句话说,不是使用嵌入式eclipse maven版本,而是使用外部版本3.3.9。

For Eclipse 4.17 (2020-09) with M2E 1.16.1 ( and the focus it should especially work there! ) and Maven 3.3.3 (external) or 3.6.3 (embedded) (and likely similar combinations/versions) it works for mixed http://... and https://... repo URLs with the following setup (other than in the past ):

eg to make it work for settings.xml or pom.xml :

  • <url> https://repo.maven.apache.org/maven2 </url>
  • <url> http://my-local-repo/repository/rep1 </url>
  • <url> http://my-local-sonatype-nexus-repo-manager-oss-v3/repository/rep2 </url>

you need:

  1. two entries in your <usr-home>/.m2/settings.xml if some repos have http:// and some https:// URLs (unfortunately not like maybe stated in docs or in older Maven or m2e versions/combinations)

     <proxy> <id>http</id> <active>true</active> <protocol>http</protocol> <port>8080</port> <host>some-proxy.com</host> </proxy> <proxy> <id>https</id> <!-- important!: different id than above! --> <active>true</active> <protocol>https</protocol> <port>8080</port> <host>some-proxy.com</host> </proxy>
  2. additionally the Eclipse M2E plugin has the problem to not sync at all if the
    Window -> Preferences -> Maven -> [x] Download repository index updates on startup is not enabled (=> thus one could think of it as: " [x] Enable repository index updates (additionally on startup) " )

  3. furthermore some repos , eg the known Sonatype's Nexus Repository Manager v3 (at least 3.2), do not support indexing (see also feature request NEXUS-17279 ) and thus one has at least two options:

  4. to be sure to not run into caching or crash problems you should restart Eclipse (eg with -clean option) in case something does not work right away after the above setup

Of course without indexing one would loose the ability to search within repo artifacts but the direct download should always work nevertheless.

  • eg via pom.xml -> right click -> Maven -> Add Dependency -> Enter groupId, artifactId ...
    • if you enter groovy there should be quite some suggestions if it works, otherwise some artifactId cannot be empty warning may be displayed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM