配置Maven環境變量
配置Maven環境變量
一、下載解壓
Maven官網下載
下載zip格式,方便安裝多個版本跟配置
二、配置settings.xml文件
1.進入解壓路徑,新建文件夾 localRepository ,找到 conf 文件夾,進入找到 settings.xml 文件。
2.打開 settings.xml 文件,配置本地倉庫,找到
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
- 1
- 2
- 3
- 4
- 5
- 6
在注釋下面加一行代碼,表示本地倉庫路徑,用剛才新建的文件夾路徑。
<localRepository>D:\Devtool\Maven\apache-maven-3.8.1\localRepository</localRepository>
- 1
3.打開 settings.xml 文件,配置國內下載鏡像,找到
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
</mirrors>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
找到后,把紅框注釋掉,並添加國內鏡像代碼。
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
- 1
- 2
- 3
- 4
- 5
- 6
最新的地址
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里雲公共倉庫</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
- 1
- 2
- 3
- 4
- 5
- 6
4.可能會遇到,大部分jar包都可以在阿里鏡像中找到,部分jar包在阿里鏡像中沒有,需要單獨配置鏡像,自行參考下面鏈接。
<mirrors>
<!-- 阿里雲倉庫 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!-- 中央倉庫1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央倉庫2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
在maven的配置文件setting.xml大里面有個mirrors節點,用來配置鏡像URL。mirrors可以配置多個mirror,每個mirror有id,name,url,mirrorOf屬性,
id是唯一標識一個mirror,name節點名,url是官方的庫地址,mirrorOf代表了一個鏡像的替代位置,例如central就表示代替官方的中央庫
雖然mirrors可以配置多個子節點,但是它只會使用其中的一個節點,即默認情況下配置多個mirror的情況下,只有第一個生效,只有當前一個mirror
無法連接的時候,才會去找后一個;而我們想要的效果是:當a.jar在第一個mirror中不存在的時候,maven會去第二個mirror中查詢下載,但是maven不會這樣做!
注意:
配置多個mirror時,mirrorOf不能配置" * “,” * " 的意思就是(根據mirrorOf和repository的id)匹配所有的倉庫(repository),
這樣就是說如果你需要某個jar,他會從鏡像地址去下載這個jar。不管你配置了多少個庫,即使這些庫的地址不一樣,仍然會從鏡像地址訪問。
二、配置環境變量
win + R
黑窗口輸入 mvn -v 查詢版本號
三、不聯網使用本地倉庫
1.三種倉庫的區別
本地倉庫:本地的一個文件夾,用來存放所有的jar包,由自己維護
遠程倉庫(或私服):由公司或單位創建的一個倉庫,由公司維護
中央倉庫:互聯網上的倉庫,由Maven團隊維護
2.配置無網狀態鏡像
倉庫位置用上面配置的本地倉庫位置
<mirror>
<id>central</id>
<name>central</name>
<url>file://D:\Devtool\Maven\apache-maven-3.8.1\localRepository</url>
<mirrorOf>*</mirrorOf>
</mirror>
- 1
- 2
- 3
- 4
- 5
- 6
3.idea設置修改