How to make maven use system proxy settings

点点圈 提交于 2019-12-04 15:16:23

问题


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

The question is: how I can run maven in command line and make it use this proxy?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/3601474/how-to-make-maven-use-system-proxy-settings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!