背景:
有时候公司不允许直接访问外部网络,就需要经过代理验证(比如在安全限制上设置了白名单网站),这时,maven需要通过配置http代理,才能正常访问外部仓库(比如库的链接 repo1.maven.org)。
步骤:
1、确认是否可以直接访问公共的maven中央仓库
直接ping repo1.maven.org:
如果不能ping通,说明无法访问,执行如下一步
如果可以ping通,则无需做任何配置可以直接访问maven中央仓库
2、比如一个代理服务ip是218.14.227.198,端口号为3122,执行telnet 218.14.227.198 3122
如果连接成功,则输入ctrl+],然后q回车退出,执行如下一步
如果出错,先获取正确代理服务信息在处理
3、连接成功的情况下,编辑所用目录下的.m2/settings.xml文件(如果没有该文件,复制$M2_HOME/conf/settings.xml,且.m2目录是隐藏目录,需要设置可见),添加代理配置:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- proxies | This is a list of proxies which can be used on this machine
to connect to the network. | Unless otherwise specified (by system property
or command-line switch), the first proxy | specification in this list marked
as active will be used. | -->
<proxies>
<proxy>
<id>my-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>218.14.227.198</host>
<port>3122</port>
<!--
<username>admin</username>
<password>admin</password>
<nonProxyHosts>repository.mycom.com|*.google.com</nonProxyHosts>
-->
</proxy>
</proxies>
</settings>
proxies下面可以有多个proxy元素,如果声明了多个proxy元素,则默认情况下第一个被激活的proxy会生效。
id:可选,可以配置多个代理节点(proxy)
active:true/false,true的话说明该代理配置激活使用
protocol:http,代理的协议,这里就是http
username:用户名,代理需要账号校验时填写,否则注释掉
password:密码,代理需要账号校验时填写,否则注释掉
host:代理ip
port:代理端口
nonProxyHosts:无需经过代理访问的网站,多个的话通过|隔开
当公司网络需要通过代理访问外部时,Maven需要配置HTTP代理才能正常获取库资源。本文介绍了如何检查网络连接并配置settings.xml文件,包括代理的IP、端口、用户名和密码,以及设置无需代理的域名。
1039

被折叠的 条评论
为什么被折叠?



