maven替换中央仓库- 阿里云

在国内访问Maven仓库,连接速度太慢。下面是将中央仓库替换成阿里云的中央仓库的方法。

第一种,统一修改仓库地址

可以直接修改Mavenconf文件夹中的setting.xml文件,或者在.m2文件夹下建立一个setting·xml文件。

setting.xml里面有个mirrors节点,用来配置镜像URL。mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性。

  • id是唯一标识一个mirror
  • name貌似没多大用,相当于描述
  • url是官方的库地址
  • mirrorOf代表了一个镜像的替代位置,例如central就表示代替官方的中央库。

mirror也不是按settings.xml中写的那样的顺序来查询的。所谓的第一个并不一定是最上面的那个。

当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询。

在setting·xml中添加如下代码:

...
<mirrors>  
    ...   
    <mirror>  
      <id>alimaven</id>  
      <name>aliyun maven</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>          
    </mirror>
</mirrors>

《maven替换中央仓库- 阿里云》 image

第二种,分别给每个项目配置不同的中央库

直接在项目的pom.xml中修改中央库的地址。如下:

<repositories>
    <repository>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

完整的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xiaolyuh</groupId>
    <artifactId>spring-boot-student</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>spring-boot-student</name>

    <!-- 添加Spring Boot的父类依赖,这样当前项目就是Spring Boot项目了。 spring-boot-starter-parent是一个特殊的starter,他用来 
        提供相关的maven默认依赖, 使用它之后,常用的依赖可以省去version标签 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <!-- 或者在maven的setting文件中加入 -->
    <!--<mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>-->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>spring-boot-student-banner</module>
    </modules>

</project>

《maven替换中央仓库- 阿里云》 扫码有红包哦

    原文作者:xiaolyuh
    原文地址: https://www.jianshu.com/p/80384612ee1d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞