原文: https://howtodoinjava.com/maven/maven-proxy-settings/

学习配置 maven https 代理设置。 默认情况下,maven 在本地系统上运行时使用默认的网络连接。 但是有时我们在工作场所或公司中运行应用。 这些网络通过代理服务器或防火墙来屏蔽互联网使用,因此从我们的系统进行的所有互联网查询都通过此代理服务器进行。

Maven 默认情况下不会检测网络代理配置,并且要在这些受限区域中使用 maven,我们必须为 maven 配置网络代理设置。

阅读更多: Maven 安装

1. 如何配置 Maven 代理设置

要设置 maven 代理设置,请执行以下步骤:

  • 导航到路径 – {M2_HOME} conf/settings.xml
  • 在任何文本编辑器中以编辑模式打开文件settings.xml
  • 打开并更新<proxy>标签。

该标签如下所示:

  1. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  4. <!-- proxies
  5. | This is a list of proxies which can be used on this machine to connect to the network.
  6. | Unless otherwise specified (by system property or command-line switch), the first proxy
  7. | specification in this list marked as active will be used.
  8. |-->
  9. <proxies>
  10. <!-- proxy
  11. | Specification for one proxy, to be used in connecting to the network.
  12. |
  13. <proxy>
  14. <id>optional</id>
  15. <active>true</active>
  16. <protocol>http</protocol>
  17. <username>proxyuser</</username>
  18. <password>proxypass</password>
  19. <host>proxy.host.net</host>
  20. <port>80</port>
  21. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  22. </proxy>
  23. -->
  24. </proxies>
  25. </settings>

使用您的网络特定凭据更新上述代理服务器字段。 您可以在浏览器的连接设置中找到网络代理的详细信息。 例如,可以在以下位置找到代理设置:

  1. Internet Explorer >> tools >> internet options >> Connections >> LAN Settings
  2. Firefox >> tools >> options >> ADVANCED TAB >> settings

2. Maven 代理设置示例

给出了示例网络代理设置条目。

  1. <!-- proxies
  2. | This is a list of proxies which can be used on this machine to connect to the network.
  3. | Unless otherwise specified (by system property or command-line switch), the first proxy
  4. | specification in this list marked as active will be used.
  5. |-->
  6. <proxies>
  7. <!-- proxy
  8. | Specification for one proxy, to be used in connecting to the network.
  9. |
  10. <proxy>
  11. <id>optional</id>
  12. <active>true</active>
  13. <protocol>https</protocol>
  14. <username>lokesh</</username>
  15. <password>abc123</password>
  16. <host>webproxy.company.com</host>
  17. <port>8081</port>
  18. <!-- <nonProxyHosts>local.net|some.host.com</nonProxyHosts> -->
  19. </proxy>
  20. -->
  21. </proxies>

完成上述步骤后,您还应该可以在代理服务器后面使用 maven。

3. Eclipse 中的 Maven 代理设置

要在使用 Maven 时在 eclipse 中使用代理设置,请按照下列步骤操作:

  • 打开 Eclipse,然后转到窗口 -> 首选项。

  • 点击User SettingsBrowse按钮,并选择settings.xml
    Maven 代理设置 – Eclipse,命令行和全局设置 - 图1
    Eclipse 中的 Maven 代理

  • 单击"Update Settings"按钮更新设置。 如果出现任何确认对话框,只需单击“是”。

4. 通过命令行进行 Maven 代理设置

为了节省时间,我们可以在 maven 命令的末尾附加代理详细信息。

  1. $ mvn clean install -DproxySet=true -DproxyHost=ur.proxy.server -DproxyPort=port

尽管可以从命令行设置 Maven 代理,但仍建议在settings.xml文件中使用全局代理设置

学习愉快!

参考:

配置代理