在國內訪問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> </project>
這個就是原生的Sonattype Nexus.... 其實阿里很多做過類似的事情,阿里的開源軟件的Mirrors,阿里的Ruby語言的gem和NodeJs的npm倉庫。作為一個Linux用戶,我一直用的阿里的mirror,速度很快。現在阿里開放了 maven倉庫,是件好事,造福很多Java程序員。
現在很多大佬都在稍稍的布局,阿里的maven更是如此,因為有了阿里的maven,我才知道阿里的git,有了阿里的maven,我才知道阿里的持續交付,有了阿里的maven,我才向老板推薦了阿里雲服務器,現在公司第年在阿里雲上大概消耗3000元的樣子。
https://www.zhihu.com/question/49097325
archetypeCatalog用來指定maven-archetype-plugin讀取archetype-catalog.xml文件的位置:
internal——maven-archetype-plugin內置的
local——本地的,位置為~/.m2/archetype-catalog.xml
remote——指向Maven中央倉庫的Catalog
修改阿里雲鏡像后,若不能使用命令’mvn archetype:generate -DarchetypeCatalog=local’
$ cp ~/.m2/archetype-catalog.xml ~/.m2/repository/
https://blog.csdn.net/tengxing007/article/details/72588242
Webmin管理工具
目前功能最強大的基於Web的Linux系統管理工具,Webmin讓您能夠遠程通過Web界面管理您的主機
https://market.aliyun.com/spec/panel?utm_content=se_1000089576
https://blog.csdn.net/tengxing007/article/details/72588242
internal repository是指在局域網內部搭建的repository,它跟central repository, jboss repository等的區別僅僅在於其URL是一個內部網址
mirror則相當於一個代理,它會攔截去指定的遠程repository下載構件的請求,然后從自己這里找出構件回送給客戶端。
配置mirror的目的一般是出於網速考慮。
可以看出,internal repository和mirror是兩碼事。
前者本身是一個repository,可以和其它repository一起提供服務,比如它可以用來提供公司內部的maven構件;
而后者本身並不是repository,它只是遠程repository的網絡加速器。
不過,很多internal repository搭建工具往往也提供mirror服務,比如Nexus就可以讓同一個URL,既用作internal repository,又使它成為所有repository的mirror。
如果倉庫X可以提供倉庫Y存儲的所有內容,那么就可以認為X是Y的一個鏡像。換句話說,任何一個可以從倉庫Y獲得的構件,都胡夠從它的鏡像中獲取。
舉個例子,http://maven.net.cn/content/groups/public/ 是中央倉庫http://repo1.maven.org/maven2/ 在中國的鏡像,由於地理位置的因素,該鏡像往往能夠提供比中央倉庫更快的服務。
因此,可以配置Maven使用該鏡像來替代中央倉庫。
settings.xml
<settings> ... <mirrors> <mirror> <id>maven.net.cn</id> <name>one of the central mirrors in china</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> ... </settings>
https://blog.csdn.net/caomiao2006/article/details/40401517
一些其它的maven中央倉庫
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> <mirror> <id>ibiblio</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror> <!-- 中央倉庫在中國的鏡像 --> <mirror> <id>maven.net.cn</id> <name>oneof the central mirrors in china</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>