简体   繁体   English

"如何在 Maven 的 settings.xml 中为 HTTP 和 HTTPS 配置代理服务器?"

[英]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.我在代理服务器后面使用 Maven 3.1.1。 The same proxy handles both HTTP and HTTPS traffic.同一个代理处理HTTPHTTPS流量。

I can't seem to tell maven using settings.xml to use both protocols.我似乎无法告诉 maven 使用settings.xml来使用这两种协议。 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 :这是我的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 ?是否可以在 Maven 的settings.xml中同时为HTTPHTTPS配置代理? I'm aware that I could workaround this by passing Java system properties to the maven invocation such as:我知道我可以通过将 Java 系统属性传递给 maven 调用来解决这个问题,例如:

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

but surely this must be possible from within settings.xml ?但这肯定可以从settings.xml实现吗?

Maven bugs raised such as MNG-2305 and MNG-4394 suggest this issue is resolved, but I am not convinced.提出的 Maven 错误(例如MNG-2305MNG-4394 )表明此问题已解决,但我不相信。

Alternatively, is there a "proxy proxy" I could run locally that I could point maven to?或者,是否有一个“代理代理”我可以在本地运行,我可以指向 maven? The "proxy proxy" would route http/https accordingly. “代理代理”将相应地路由 http/https。 Even so, I would still need to define two active proxy definitions in settings.xml for Maven to direct both types of traffic.即便如此,我仍然需要在settings.xml中定义两个活动代理定义,以便 Maven 引导这两种类型的流量。

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: 来自settings.xml的Maven代理用于http和https,因此您只需要定义一个代理服务器并将其用于两者,您只需要留下一个proxy标记,如下所示:

<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). 没有额外的...<id>httpsproxy</id>...条目 (如@KrzysztofKrasoń所提到的)和它(如提问者所述)。 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 ). 对我们来说问题是, Eclipse->Maven->User Settings->[x] Update Settings显然根本不起作用并测试某些东西Eclipse->Maven->[x] Download repository index updates on startup必须检查(例如Maven Repositories View->Global Repositories->central->Update Index )。 And most of all: 最重要的是:

Eclipse must be restarted after every settings.xml update! 每次settings.xml更新后都必须重新启动Eclipse! :-/ : - /

I guess it's a bug or reload/caching issue. 我想这是一个bug或重载/缓存问题。 We successfully tested it with 我们成功地测试了它

  • Kepler (4.3) and Neon (4.6) 开普勒(4.3)和霓虹灯(4.6)
  • and their embedded Maven versions (3.2.1 / 3.3.9) as well as an external 3.3.3 及其嵌入式Maven版本(3.2.1 / 3.3.9)以及外部3.3.3
  • with http:// and https:// URLs 使用http://https:// URL

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 . 我使用Eclipse Maven进行的测试表明, settings.xml中的protocol是指代理服务器的协议,而不是URL请求的协议。 It also shows that Maven only uses the first active proxy server listed, and ignores the rest. 它还显示Maven仅使用列出的第一个活动代理服务器,并忽略其余部分。

Here's my evidence: 这是我的证据:

1. The documentation says that 文件说明了这一点

active : true if this proxy is active. active :如果此代理处于活动状态,则为true。 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." protocol ,host,port: protocol:// host:代理的端口 ,分隔成离散元素。“

2. The source code is even clearer: 2. 源代码更清晰:

    /**
     * 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): 3.真实世界测试(使用Eclipse Maven):

a. 一个。 1st proxy is a bogus ftp, 2nd is real http, 3rd is real https. 第一个代理是一个虚假的ftp,第二个是真正的http,第三个是真正的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. 如果协议是用于URL请求,那么Maven将查找真正的http / https代理并且工作得非常好。 Instead, it used the 1st proxy even though it was "ftp", and failed. 相反,它使用第一个代理,即使它是“ftp”,并且失败了。

    <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. 第一个代理是真正的http,第二个是伪造的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. 否则,它会使用第二个代理进行https请求,点击伪代理服务器,然后失败。

    <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. C。 Both are http, but 1st proxy is bogus, 2nd is real. 两者都是http,但第一个代理是伪造的,第二个是真实的。 Result: FAIL. 结果:失败。

This shows that maven doesn't use multiple proxies, even for the same protocol. 这表明maven不使用多个代理,即使对于相同的协议也是如此。 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 ):对于带有M2E 1.16.1Eclipse 4.17 (2020-09)以及它应该特别适用于那里的重点! )和Maven 3.3.3(外部)或 3.6.3(嵌入式) (以及可能类似的组合/版本),它适用于混合http://...https://...具有以下设置的 repo URL过去除外):

eg to make it work for settings.xml or pom.xml :例如使其适用于settings.xmlpom.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)您的<usr-home>/.m2/settings.xml中的两个条目,如果某些存储库具有http://和一些https:// URL(不幸的是,不像文档或旧 Maven 或 m2e 版本/组合中所述)

     <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另外 Eclipse M2E 插件存在根本不同步的问题,如果
    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) " ) Window -> Preferences -> Maven -> [x] 启动时下载存储库索引更新未启用(=> 因此可以将其视为:“ [x] 启用存储库索引更新(另外在启动时)

  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:此外,一些 repos ,例如已知的 Sonatype 的Nexus Repository Manager v3(至少 3.2),不支持索引(另见功能请求 NEXUS-17279 ),因此至少有两个选项:

  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为了确保不会遇到缓存或崩溃问题,您应该重新启动 Eclipse(例如,使用-clean选项) ,以防在上述设置后某些东西无法立即工作

Of course without indexing one would loose the ability to search within repo artifacts but the direct download should always work nevertheless.当然,如果没有索引,将失去在 repo 工件中搜索的能力,但直接下载应该始终有效

  • eg via pom.xml -> right click -> Maven -> Add Dependency -> Enter groupId, artifactId ...例如通过pom.xml -> 右键单击​​ -> Maven -> 添加依赖项 -> 输入 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如果你输入groovy应该有相当多的建议如果它有效,否则可能会显示一些artifactId cannot be empty 警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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