1、前提
安裝mavne首先需要安裝JDK,並配置環境變量。這部分比較簡單,就不再做演示了。2、安裝
直接解壓文章末尾提供的apache-maven-3.1.1-bin.zip文件到D:\maven\apache-maven-3.1.1(可自定義文件位置)即可完成安裝。3、配置
1)、環境變量右鍵 我的電腦->屬性->高級系統設置->環境變量,進行如下設置。
a、在用戶變量中添加MAVEN_HOME=D:\maven\apache-maven-3.1.1
b、修改PATH,在配置中添加%MAVEN_HOME%\bin;c、調出命令行窗口(win + r 之後輸入cmd)
d、輸入mvn –version,出現界面如下,則安裝Maven配置成功
2)、settings.xml文件配置
Maven中最重要的配置就是settings.xml文件的配置,其默認位置爲%MAVEN_HOME%\conf\settings.xml,相關具體配置如下:
a、全局settings.xml位於%MAVEN_HOME%\conf\settings.xml,在該文件中配置的任何選項對於使用maven的所有應用程序均會產生影響,且影響力最大。(該文件如果不做任何修改,在第一次啓動maven之時會在當前用戶的文件夾下建立一個.m2的文件夾,其中存放了maven本地的所有jar文件)
b、用戶settings.xml,拷貝%MAVEN_HOME%\conf\settings.xml文件到當前系統用戶文件下的.m2下的settings.xml文件,並在其中修改爲自定義的maven本地倉庫存放位置(用戶settings.xml並非一開始就有,它的意義在於不修改maven全局配置的情況下,更加合理對的配置用戶自己的maven配置文件)。
<!--localRepository
| The path to the local repository mavenwill use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!-- 自定義本地倉庫存放位置 -->
<localRepository>D:/maven/repository</localRepository>
c、自定義settings.xml文件,即拷貝%MAVEN_HOME%\conf\settings.xml文件到當前自定義用戶自定義的maven本地倉庫存放位置的同級目錄下(自定義settings.xml同用戶settings.xml一樣,它的出現也是爲了更加合理的使用maven的配置文件。)
修改settings.xml內容,具體如下:
<?xmlversion="1.0" encoding="UTF-8"?>
<settingsxmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 設置本地倉庫路徑 -->
<localRepository>D:/maven/repository</localRepository>
<!-- 設置發佈 jar 包時的用戶名及密碼 -->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!-- 設置 maven 的遠程倉庫爲 nexus-->
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>LocalRepository</name>
<url>http://192.168.24.252:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<!-- 設置 central 的路徑等 -->
<profiles>
<profile>
<id>central</id>
<repositories>
<repository>
<id>central</id>
<name>Central</name>
<!-- 該 url 沒有意義,可以隨便寫,但必須有。 -->
<url>http://localhost:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>localprivate nexus</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活 central -->
<activeProfiles>
<activeProfile>central</activeProfile>
</activeProfiles>
<!-- 配置eclipse插件 -->
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>
</settings>
到此爲止,對於Maven的配置我們就講解完畢了。這裏重點給大家強調一點的是,maven只是一個管理型的構建工具,它的功能很多,也有很多豐富的插件,很多人錯誤的認爲maven只是一個構建工具,這一點是非常值得大家借鑑的。
持續更新中,敬請期待!
apache-maven-3.1.1-bin.zip下載地址如下:
http://mirrors.hust.edu.cn/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.zip