开发者

How to make maven use system proxy settings

I'm using Ubuntu with Gnome where I can set network proxy settings (with authentication).

The question is: how I can开发者_JS百科 run maven in command line and make it use this proxy?


Did you take a look at http://maven.apache.org/guides/mini/guide-proxies.html?

In your settings.xml:

<settings>
  .
  .
  <proxies>
   <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
    </proxy>
  </proxies>
  .
  .
</settings>

NOTE: This doesn't affect Java code! These proxy settings are Maven repository proxy settings.


There is a java.net.useSystemProxies system property that can be set to true (on Windows and Linux platforms) to tell the JVM to use the system proxy settings. From the Java Networking and Proxies guide:

Before we see in details how to write such a ProxySelector, let's talk about the default one. J2SE 5.0 provides a default implementation which enforces backward compatibility. In other terms, the default ProxySelector will check the system properties described earlier to determine which proxy to use. However, there is a new, optional feature: On recent Windows systems and on Gnome 2.x platforms it is possible to tell the default ProxySelector to use the system proxy settings (both recent versions of Windows and Gnome 2.x let you set proxies globally through their user interface). If the system property java.net.useSystemProxies is set to true (by default it is set to false for compatibility sake), then the default ProxySelector will try to use these settings. You can set that system property on the command line, or you can edit the JRE installation file lib/net.properties, that way you have to change it only once on a given system.

But this will only work for the java.net.* classes, not for commons-httpclient, jsch, etc. So this doesn't solve the whole issue and Maven doesn't really support it (this is logged as MNG-728).

In other words, I'm afraid you'll have to configure the proxy settings in your ~/.m2/settings.xml.


Those who need to set these settings through the command line can use the following:

MAVEN_CLI_OPTS: "-DproxySet=true -Dhttp.proxyHost=yourProxyHost -Dhttp.proxyPort=9999 -Dhttp.nonProxyHosts=mvnrepository.com"

Usage:

mvn $MAVEN_CLI_OPTS test

Warning: nonProxyHosts property did not work correctly for me when using it from a CI environment. I suspect it might be because the pipe characters are not being interpreted correctly in Gitlab's YAML syntax but I didn't find a way to fix that.

Source: https://confluence.atlassian.com/jirakb/java-option-http-nonproxyhosts-does-not-work-214863640.html


In Netbeans youhave to remove the -Djava.net.useSystemProxies=true from Tools-> option->java->Maven. Once you remove it, Maven reads the proxy settings from settings.xml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜