1.为什么要设置代理服务器?
开发中经常遇到国外的maven服务器无法访问,还有就是服务器没有外网地址,这将依赖包无法下载,解决这个问题很是简单,只需要设置代理服务器就可以了,让maven使用代理服务器去访问库服务。
开发中经常遇到国外的maven服务器无法访问,还有就是服务器没有外网地址,这将依赖包无法下载,解决这个问题很是简单,只需要设置代理服务器就可以了,让maven使用代理服务器去访问库服务。
有时候你所在的公司基于安全因素考虑,要求你使用通过安全认证的代理访问因特网。这种情况下,就需要为Maven配置HTTP代理,才能让它正常访问外部仓库,以下载所需要的资源。
为了使它工作,你必须声明在 Maven 的配置文件中设置代理服务器:settings.xml
2:Maven配置文件settings.xml
找到文件 {M2_HOME}/conf/settings.xml, 并把你的代理服务器信息配置写入。注:{M2_HOME} 是你的文件路径:D:\software\Maven\apache-maven-3.3.9
<!-- 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
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
3:修改配置文件
将注释去掉,修改<username>、<password>、<host>、<port>标签的配置。
<!-- 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>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>maven</username>
<password>password</password>
<host>127.0.0.1</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
<nonProxyHosts>标签是指定那些主机不需要代理,可以使用“|”符号来分隔多个主机名。此外,该配置也支持通配符,如:*.google.com表示所有以google.com结尾的域名访问都不要通过代理。
完成后,Apache Maven 应该是能够通过代理服务器立即连接到Internet。