在测试一个 java demo 项目时,需要用到 maven 来编译,因为公司网络是需要走代理访问外网的,在我不知道 maven 需要单独配置代理的情况下,一直使用 mvn package
进行打包编译,一直报错,如下:
[ERROR] The project com.ly.simple:ly-simple-tomcat:0.0.1-SNAPSHOT (/opt/tomcat-java-demo/pom.xml) has 1 error
[ERROR] Non-resolvable parent POM: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from/to central (https://repo.maven.apache.org/maven2): Connection to https://repo.maven.apache.org refused and 'parent.relativePath' points at wrong local POM @ line 8, column 14: Connection timed out (Connection timed out) -> [Help 2]
org.apache.maven.model.resolution.UnresolvableModelException: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from/to central (https://repo.maven.apache.org/maven2): Connection to https://repo.maven.apache.org refused
关键错误是 Connection to https://repo.maven.apache.org refused
,其实本地网络是可以连接到的,只是 mvn 连接不到,所以才想到了代理的问题,查询了添加代理的方法,maven 安装目录下有默认的配置文件 conf/settings.xml
,建议将代理配置放到用户目录下
$ vim $HOME/.m2/settings.xml
<settings>
<proxies>
<proxy>
<id>httpproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>xxx.xxx.com</host>
<port>3128</port>
</proxy>
<proxy>
<id>httpsproxy</id>
<active>true</active>
<protocol>https</protocol>
<host>xxx.xxx.com</host>
<port>3128</port>
</proxy>
</proxies>
</settings>
<protocol>http/https</protocol>
,该位置的配置是导致我长时间无法解决问题的原因,因为本身代理的 URL 为 http://xxx.xxx.com:3128
,网上资料没有任何说明 protocol
用来标识的是代理使用的协议,还是使用代理时走的协议。这句话可能不太好理解。
在 Linux 系统中,http_proxy 和 https_proxy,是两个默认的变量,当访问 http 的URL时,使用 http_proxy;访问 https 的URL时,使用 https_proxy,这里的 protocol 也是和这个同理。一开始我并未设置 https,所以配置了代理还是一直无法访问。
还有一点是 host 无需标识使用的是 http://
还是 https://
,直接写域名或者ip就好了。