Maven是Apache下的一個項目管理工具。它可以構建項目和管理依賴(管理jar包,防止jar之間的依賴和衝突,管理jdk、tomcat版本等環境變量)。
項目構建是一個項目從編寫源代碼到清理、編譯、測試、運行、打包、部署、運行的過程。
一、maven安裝
步驟一、解壓壓縮包
步驟二、配置環境變量
步驟三、建一個Repositories文件夾
步驟四、自定義倉庫配置(settings.xml)
步驟五、eclipse配置
二、maven常用的命令
pom文件介紹
編譯:cmd進入命令模式,執行 mvn compile 便可以將src/main/java下的文件編譯爲class文件輸出到target目錄下。
測試:cmd進入命令模式,執行 mvn test。
清理:cmd進入命令模式,執行 mvn clean,便可以刪除target目錄的內容。
打包:cmd進入命令模式,執行 mvn package 便可以將java工程打成jar包,web工程打成war包。
安裝:cmd進入命令模式,執行 mvn install 便可以將war包或者jar包,發佈到本地倉庫。
三、依賴範圍
A依賴B,需要在A的pom.xml文件中添加B的座標,添加座標時,需要指定依賴範圍,依賴範圍包括:
當項目引入多個依賴時,每個依賴之前也存在依賴,比如A依賴C-1.2.1,B也依賴C-1.2.4,只是C的版本不同,同時引入A,B,這是會發生依賴衝突,解決方法有:
1、按照maven以下原則調解:
(1)第一聲明者優先原則:A和B誰在前面,就依賴誰的C版本
(2)路徑進者優先原則:A在前,B在後,若想使用B依賴的C版本,可以在單獨配一下想依賴的C版本
2、排除依賴
3、鎖定版本號(最常用):使用dependencyManagement
四、搭建私服
私服:獨立於中央倉庫之外,把自己的東西分享給公司的人,所以需要把jar包放在私服裏。
maven本地倉庫與中央倉庫、私服之間的關係:如果本地倉庫沒有,則向中央倉庫下載,若是公司有私服,則先通過私服下載,私服沒有之後,再向中央倉庫下載。
步驟一:下載nexus
步驟二:使用管理員身份運行cmd,執行nexus install命令
步驟三:啓動私服服務, 訪問私服:http://localhost:8081/nexus/
步驟四:發佈項目到私服
在maven的settings.xml配置文件中,加上以下配置:
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
releases爲連接發佈版本倉庫;snapshots爲測試版本倉庫
在發佈項目代碼的pom.xml上配置添加以下配置:
<!--發佈配置-->
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
裏面的url爲私服對應的url
通過maven運行package命令將項目打成war包
運行deplog命令,將war包發佈到私服上。
步驟六:從私服下載jar包到項目裏
在maven的settings.xml配置文件中,添加以下配置:
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>dev</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
在項目的pom.xml裏添加以下配置:
<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>