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> 
 

第二種,分別給每個項目配置不同的中央庫

直接在項目的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>



作者:xiaolyuh
鏈接:https://www.jianshu.com/p/80384612ee1d
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM