解决Maven使用代理连接问题

前言

  在国内使用Maven下载远程仓库jar包,速度真是不太理想,慢的真心无法忍受。所以,最好的解决办法是通过Maven设置代理(使用XX-Net翻墙)或者Maven使用代理

Maven设置代理

  1. 编辑 ${user.home}/.m2/settings.xml 文件,如果该目录下没有该文件,复制 $MAVEN_HOME/conf/setting.xml
  2. 找到<proxies>节点,设置代理信息

    1
    2
    3
    4
    5
    6
    7
    <proxy>
    <id>xx-net</id>
    <active>true</active>
    <protocol>http</protocol>
    <host>127.0.0.1</host>
    <port>8087</port>
    </proxy>
  3. 完成上述配置,仍然还是会有问题

    1
    [ERROR] Failed to execute goal on project xiaov: Could not resolve dependencies for project org.b3log:xiaov:war:2.2.0: Failed to collect dependencies at org.b3log:latke:jar:2.3.5 -> com.alibaba:druid:jar:1.0.16: Failed to read artifact descriptor for com.alibaba:druid:jar:1.0.16: Could not transfer artifact com.alibaba:druid:pom:1.0.16 from/to central (https://repo.maven.apache.org/maven2): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1]

    原因就是Maven走的是https,使用代理连接才会出现上述SSL认证问题。
    更多解决办法:点击查看推荐使用下面的解决办法

  4. 最简洁的Maven配置
    请将下述配置信息覆盖settings.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    <proxy>
    <id>xx-net</id>
    <active>true</active>
    <protocol>http</protocol>
    <host>127.0.0.1</host>
    <port>8087</port>
    </proxy>
    </proxies>
    <profiles>
    <profile>
    <id>securecentral</id>
    <repositories>
    <repository>
    <id>central</id>
    <url>http://repo1.maven.org/maven2</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
    <id>central</id>
    <url>http://repo1.maven.org/maven2</url>
    <releases>
    <enabled>true</enabled>
    </releases>
    </pluginRepository>
    </pluginRepositories>
    </profile>
    </profiles>
    <activeProfiles>
    <activeProfile>securecentral</activeProfile>
    </activeProfiles>
    </settings>

Maven使用镜像

这里推荐使用阿里Maven仓库镜像(Nexus仓库),希望能稳定存活。
修改Maven的settings.xml文件,配置mirrors的子节点,添加如下mirror:

1
2
3
4
5
6
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

评论