Fork me on GitHub
Suzf  Blog

How-to configure a proxy for maven

License: Attribution-NonCommercial-ShareAlike 4.0 International

本文出自 Suzf Blog。 如未注明,均为 SUZF.NET 原创。

转载请注明:http://suzf.net/post/1268

前言

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

Maven设置代理

  1. 编辑 ${user.home}/.m2/settings.xml 文件,如果该目录下没有该文件,复制 $MAVEN_HOME/conf/setting.xml
  2. 找到<proxies>节点,设置代理信息
     <proxy>
       <id>xx-net</id>
       <active>true</active>
       <protocol>http</protocol>
       <host>127.0.0.1</host>
       <port>8087</port>
     </proxy>
  3. 完成上述配置,仍然还是会有问题
    [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
    <?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:

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
上文来源 Abinnz's blog

以下是我个人 Jenkins Maven proxy 设置

:/usr/local/apache-maven-3.3.9# diff  -ruN conf/settings.xml.old  conf/settings.xml 
--- conf/settings.xml.old	2017-02-21 16:59:07.996000000 +0800
+++ conf/settings.xml	2017-02-21 17:01:15.496000000 +0800
@@ -88,6 +88,10 @@
    | specification in this list marked as active will be used.
    |-->
   <proxies>
+    <proxy>
+      <host>172.16.9.11</host>
+      <port>3128</port>
+    </proxy>
     <!-- proxy
      | Specification for one proxy, to be used in connecting to the network.
      |

[0] http://maven.apache.org/guides/mini/guide-proxies.html

「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」

Suzf Blog

(๑>ڡ<)☆ 谢谢 ~

使用微信扫描二维码完成支付