1. 程式人生 > >在國內訪問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>

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>