個(gè)人簡介:Java領(lǐng)域新星創(chuàng)作者;阿里云技術(shù)博主、星級博主、專家博主;正在Java學(xué)習(xí)的路上摸爬滾打,記錄學(xué)習(xí)的過程~
個(gè)人主頁:.29.的博客
學(xué)習(xí)社區(qū):進(jìn)去逛一逛~
一、下載Maven核心程序
- 通過官方渠道,下載Maven壓縮包,官網(wǎng)??:maven.apache.org
…
- 進(jìn)入官網(wǎng)后,選擇Download(下載)選項(xiàng),安裝最新版本的壓縮包
…
- 將壓縮包放置到自己喜歡的目錄下,解壓:
注意
:文件夾目錄要求 非中文、無空格。
??
…
- 加壓后,文件的內(nèi)容目錄如下:
其中,Maven核心的配置文件是conf
目錄下的settings.xml
文件
二、設(shè)置本地倉庫
?為什么
- Maven本地倉庫是有默認(rèn)值的,我們可以從
conf\settings.xml
文件下找到關(guān)于默認(rèn)本地倉庫的描述:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
我們從配置文件的這一段注釋中,了解到默認(rèn)倉庫的存放目錄是:${user.home}/.m2/repository
,也就是系統(tǒng)的家目錄中,家目錄是存放在C盤(系統(tǒng)盤)當(dāng)中的。
C盤 – 用戶 – 用戶名 – .m2 – repository
…
當(dāng)我們累計(jì)使用的 jar包越來越多,Maven倉庫的體積也將越來越大,內(nèi)存過大會(huì)拖慢所在C盤的運(yùn)行速度,影響系統(tǒng)性能。為了避免這樣的結(jié)果,我們才需要設(shè)置本地倉庫的路徑,將Maven本地倉庫放置在別的盤當(dāng)中。
?怎么做
我們需要在配置文件中加入一行代碼,來配置本地倉庫:
<localRepository>d:\maven-repository</localRepository>
localRepository
標(biāo)簽中的內(nèi)容就填寫我們自己配置的本地倉庫路徑,我們只需要手動(dòng)創(chuàng)建一個(gè)空文件夾,將此文件夾的路徑復(fù)制到標(biāo)簽中即可;
當(dāng)然不創(chuàng)建也沒問題,在標(biāo)簽中設(shè)置好路徑后,當(dāng)我們使用本地倉庫時(shí),Maven會(huì)幫我們創(chuàng)建的~
需要注意的是:本地倉庫的目錄也要求不包含中文
、空格
。
三、配置阿里云鏡像倉庫
?為什么
Maven在下載jar包時(shí),默認(rèn)會(huì)訪問境外的中央倉庫去進(jìn)行下載,但是訪問國外網(wǎng)站的速度較慢。為了提高訪問速度從而提升效率,我們需要將Maven下載jar包時(shí)訪問的倉庫設(shè)置為國內(nèi)阿里云提供的鏡像倉庫。
- 默認(rèn)的中央倉庫 - 訪問國外網(wǎng)站 - 速度慢
- 阿里云鏡像倉庫 - 訪問國內(nèi)網(wǎng)站 - 速度快
?怎么做
依舊是打開Maven目錄下,conf
文件夾中的settings.xml
文件,對settings.xml
文件中<mirrors></mirrors>
標(biāo)簽內(nèi)的內(nèi)容進(jìn)行修改:
- 默認(rèn)情況下的
mirrors
標(biāo)簽內(nèi)容:
<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>
——————————
- 設(shè)置完阿里云鏡像倉庫后的
mirrors
標(biāo)簽內(nèi)容:
改動(dòng):
- 將原本給定的例子注釋掉
- 加入我們配置的鏡像倉庫內(nèi)容(可直接復(fù)制)
<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>
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
四、配置Maven的JDK版本
?為什么
Maven工程默認(rèn)使用JDK 1.5的版本,而實(shí)際上常用的是 JDK 1.8 及以上版本。
?怎樣做
打開Maven目錄下,conf
文件夾中的settings.xml
文件,對settings.xml
文件中<profiles></profiles>
標(biāo)簽內(nèi)的內(nèi)容進(jìn)行修改:
- 默認(rèn)情況下的
profiles
標(biāo)簽內(nèi)容:
全都是注釋
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles>
————————————
- 配置后的
profiles
標(biāo)簽內(nèi)容:
改動(dòng):
- 添加了設(shè)置JDK版本的相關(guān)配置(配置在下述代碼尾部,可直接復(fù)制)
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
五、配置環(huán)境變量
——————————————
?配置Java環(huán)境變量
Maven 是一個(gè)用 Java 語言開發(fā)的程序,它必須基于 JDK 來運(yùn)行,學(xué)習(xí)過Java的同學(xué)應(yīng)該都配置過Java環(huán)境變量,可以直接跳過這一步。
如果還未下載JDK,配置Java環(huán)境變量,可以參考這篇文章:JDK安裝+配置環(huán)境變量
檢查
:
Win+R 輸入 cmd 進(jìn)入命令指示符界面,使用以下指令檢查:
java -version
echo %JAVA_HOME%
?配置Maven環(huán)境變量
- 打開之間下載解壓好的Maven文件目錄,復(fù)制路徑進(jìn)行備用:
??
- 打開計(jì)算機(jī)高級系統(tǒng)設(shè)置,選擇環(huán)境變量,新建MAVEN_HOME:
AND
??
- 系統(tǒng)變量選擇Path進(jìn)行編輯,在Path環(huán)境當(dāng)中增加一個(gè)MAVEN_HOME的bin目錄:
%MAVEN_HOME%\bin
??檢查
:
Win+R 輸入 cmd 進(jìn)入命令指示符界面,使用以下指令檢查:
mvn -v
————
如果出現(xiàn)以下提示:
說明沒有讀取到環(huán)境變量
??
- 在用戶變量的Path環(huán)境變量也添加MAVEN_HOME,之后再檢查一次:
%MAVEN_HOME%\bin
文章來源:http://www.zghlxwxcb.cn/news/detail-439819.html
到了這里,我們就成功從0完成了Maven的下載,安裝以及配置啦~
恭喜!
文章來源地址http://www.zghlxwxcb.cn/news/detail-439819.html
到了這里,關(guān)于②【Maven】從0上手Maven的安裝與配置 - 最全教程 (下載 + 配置 + 環(huán)境變量 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!