Java 如何在 Maven 的 settings.xml 中为 HTTP 和 HTTPS 配置代理服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31032174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure a proxy server for both HTTP and HTTPS in Maven's settings.xml?
提问by Stephen Hartley
I'm using Maven 3.1.1 behind a proxy server. The same proxy handles both HTTP
and HTTPS
traffic.
我在代理服务器后面使用 Maven 3.1.1。同一个代理同时处理HTTP
和HTTPS
流量。
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
:
我似乎无法告诉 mavensettings.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
? I'm aware that I could workaround this by passing Java system properties to the maven invocation such as:
是否可以为MavenHTTP
和HTTPS
Maven配置代理settings.xml
?我知道我可以通过将 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-2305and MNG-4394suggest this issue is resolved, but I am not convinced.
诸如MNG-2305和MNG-4394 之类的 Maven 错误表明此问题已解决,但我不相信。
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 ?“代理代理”将相应地路由 http/https。即便如此,我仍然需要settings.xml
为 Maven定义两个活动代理定义来引导这两种类型的流量。
回答by Krzysztof Krasoń
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.
上面的协议是代理服务器的协议,不是代理的请求。
回答by AnselmM
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.
我通过更新 maven 版本解决了这个问题,或者换句话说,不使用嵌入式 eclipse maven 版本,而是使用外部版本 3.3.9。
回答by Andreas Dietrich
It works withoutthe extra ...<id>httpsproxy</id>...
entry(as @Krzysztof Krasoń mentioned) and with it (as the asker stated).
The problem for us was, that the was obviously not working at all and to test certain things Eclipse->Maven->User Settings->[x] Update Settings
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:
它不需要额外的...<id>httpsproxy</id>...
条目(如@Krzysztof Krasoń 提到的)和它(如提问者所说)。我们的问题是,这显然根本不起作用,Eclipse->Maven->User Settings->[x] Update Settings
Eclipse->Maven->[x] Download repository index updates on startup
必须检查某些事情(例如Maven Repositories View->Global Repositories->central->Update Index
)。最重要的是:
Eclipse must be restarted after every settings.xml
update! :-/
每次settings.xml
更新后都必须重新启动 Eclipse !:-/
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://
andhttps://
URLs
- 开普勒 (4.3) 和霓虹灯 (4.6)
- 及其嵌入式 Maven 版本 (3.2.1 / 3.3.9) 以及外部 3.3.3
- 与
http://
和https://
网址
回答by wisbucky
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.
我对 Eclipse Maven 的测试表明protocol
insettings.xml
指的是代理服务器的协议,而不是URL 请求的协议。它还表明 Maven 只使用列出的第一个活动代理服务器,而忽略其余的。
Here's my evidence:
这是我的证据:
1. The documentationsays that
1.文档说
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."
active: 如果此代理处于活动状态,则为 true。这对于声明一组代理很有用,但一次只能有一个处于活动状态。
协议,主机,端口:协议://主机:代理的端口,分为离散元素。”
2. The source codeis 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. Result: FAIL.
一种。第一个代理是伪造的 ftp,第二个是真正的 http,第三个是真正的 https。结果:失败。
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.
如果协议是针对 URL 请求的,那么 Maven 会查找真正的 http/https 代理并且工作得很好。相反,它使用了第一个代理,即使它是“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. Result: SUCCESS.
湾 第一个代理是真正的 http,第二个是伪造的 https。结果:成功。
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. Both are http, but 1st proxy is bogus, 2nd is real. Result: FAIL.
C。两者都是http,但第一个代理是虚假的,第二个是真实的。结果:失败。
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.
这表明 maven 不使用多个代理,即使对于相同的协议。否则,它会尝试第二个真正的代理并成功。
<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>