1、Maven的基本概念
Maven(翻譯爲"專家","內行")是跨平臺的項目管理工具。主要服務於基於Java平臺的項目構建,依賴管理和項目信息管理。
1.1、項目構建
項目構建過程包括【清理項目】→【編譯項目】→【測試項目】→【生成測試報告】→【打包項目】→【部署項目】這幾個步驟,這六個步驟就是一個項目的完整構建過程。
理想的項目構建是高度自動化,跨平臺,可重用的組件,標準化的,使用maven就可以幫我們完成上述所說的項目構建過程。
1.2、依賴管理
1.3、使用Maven的好處
Maven中使用約定,約定java源代碼代碼必須放在哪個目錄下,編譯好的java代碼又必須放到哪個目錄下,這些目錄都有明確的約定。
Maven的每一個動作都擁有一個生命週期,例如執行mvn install 就可以自動執行編譯,測試,打包等構建過程
只需要定義一個pom.xml,然後把源碼放到默認的目錄,Maven幫我們處理其他事情
使用Maven可以進行項目高度自動化構建,依賴管理(這是使用Maven最大的好處),倉庫管理。
2、Maven的簡單使用
2.1、Maven項目的目錄約定
|----src
| |----main
| | |----java ——
| | |----resources ——存放項目資源存放項目的.java文件文件,如spring, hibernate配置文件
| |----test
| | |----java ——存放所有測試.java文件,如JUnit測試類
| | |----resources ——存放項目資源文件,如spring, hibernate配置文件
|----target ——項目輸出位置
|----pom.xml ----用於標識該項目是一個Maven項目
2.2、手動創建Maven項目,使用Maven
Pom.xml配置
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--所有的Maven項目都必須配置這四個配置項-->
<modelVersion>4.0.0</modelVersion>
<!--groupId指的是項目名的項目組,默認就是包名-->
<groupId>com.dongnao.jack</groupId>
<!--artifactId指的是項目中的某一個模塊,默認命名方式是"項目名-模塊名"-->
<artifactId>mavenProjeck</artifactId>
<!--version指的是版本,這裏使用的是Maven的快照版本-->
<version>SNAPSHOT-0.0.1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile/test/provided/runtime</scope>
</dependency>
</dependencies>
</project>
Mvn compile
Mvn test
Mvn package
一條龍服務,包括編譯、測試、打包。但是並沒有把包同步到倉庫
Mvn install
一條龍服務,包括編譯、測試、打包。並且把包同步到倉庫
Mvn clean
3、Mvn概念模型
Maven倉庫佈局
根據maven座標定義每個構建在倉庫中唯一存儲路徑
路徑爲:groupId/ artifactId/version/ artifactId-version.packaging
http://search.maven.org 從中央倉庫中搜索jar包
創庫分類
本地倉庫
/.m2/repository/
每個用戶只有一個本地倉庫
遠程倉庫
中央倉庫:http://repo1.maven.org/maven2 central
私服:是一種特殊的遠程倉庫,架設在局域網內的倉庫
鏡像:用來替代中央倉庫,速度一般都比中央倉庫快。 Mirror,一般都是國內
Maven三大生命週期
生命週期Maven有三套相互獨立的生命週期,請注意這裏說的是“三套”,而且“相互獨立”,這三套生命週期分別是:
Clean在進行真正的構建之前進行一些清理工作。
Default構建的核心部分,編譯,測試,打包,部署等等。
Site生成項目報告,站點,發佈站點。
Maven座標:
Maven座標主要組成: grouopid/artifactid/version/ artifactId-version.packaging
項目名/模塊名稱/版本號/……
groupId:定義maven的項目,可以用包名
artifactId:定義項目中的一個模塊,一般都是工程名
version:版本
packaging:定義打包方式
座標的作用:
用來唯一標識一個構件,在依賴裏面根據座標來找需要的構件。
Maven依賴:
根據maven座標找依賴的構件
dependency依賴中scope範圍
以下的意思就是:maven中jar包的分類:
1.compile: 默認編譯依賴範圍。對於編譯,測試,運行三種classpath都有效。
2.test:測試依賴範圍。只對於測試classpath有效,只對測試代碼有效
3.provided:已提供依賴範圍。對於編譯,測試的classpath都有效,但對於運行無效。因爲由容器已經提供,例如servlet-api,打包的時候這個包不會打進去
4.runtime:運行時提供。例如:jdbc驅動
Compile、runtime打包會打進去,test、provided打包不會打進去
傳遞性依賴原則:
依賴衝突解決:
1.路徑最近者優先 CàBàA,C依賴B,B依賴A,會先用B的jar包
2.路徑相同,第一聲明者優先:CàB,CàA,必須看B,A在C中哪一個先聲明
Maven聚合和繼承
主要是可以起到公共配置文件的抽取作用,jar包的統一管理
注意:
1.dependencyManagement中定義的依賴子module不會共享
2.dependencies中定義的依賴子module可以共享
3、jar包的統一管理,把jar抽取到parent中
繼承:
<parent>
<groupId>com.dongnao.jack</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
聚合:
<modules>
<module>hello1Maven</module>
<module>maven1</module>
</modules>
Maven web項目
自動部署到tomcat下面並運行
<build>
<finalName>web</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.3</version>
<configuration>
<container>
<containerId>tomcat5x</containerId>
<home>D:/Program Files/Apache Software Foundation/Tomcat 5.0</home>
</container>
<configuration>
<type>existing</type>
<home>D:/Program Files/Apache Software Foundation/Tomcat 5.0</home>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-run</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4、Nexus[ˈneksəs]私服
下載地址:http://www.sonatype.org/nexus/,下載開源版本
爲什麼要使用Nexus
爲什麼要搭建nexus私服,原因很簡單,有些公司都不提供外網給項目組人員,因此就不能使用maven訪問遠程的倉庫地址,所以很有必要在局域網裏找一臺有外網權限的機器,搭建nexus私服,然後開發人員連到這臺私服上,這樣的話就可以通過這臺搭建了nexus私服的電腦訪問maven的遠程倉庫。
具體的Nexus可以在這裏學習:
http://www.cnblogs.com/quanyongan/archive/2013/04/24/3037589.html
Nexus 是Maven倉庫管理器,如果你使用Maven,你可以從Maven中央倉庫 下載所需要的構件(artifact),但這通常不是一個好的做法,你應該在本地架設一個Maven倉庫服務器,在代理遠程倉庫的同時維護本地倉庫,以節省帶寬和時間,Nexus就可以滿足這樣的需要。此外,他還提供了強大的倉庫管理功能,構件搜索功能,它基於REST,友好的UI是一個extjs的REST客戶端,它佔用較少的內存,基於簡單文件系統而非數據庫。這些優點使其日趨成爲最流行的Maven倉庫管理器。
Nexus內置倉庫說明:
(1)Maven Central:該倉庫代理Maven中央倉庫,其策略爲Release,因此只會下載和緩存中央倉庫中的發佈版本構件。
(2)Releases:這是一種策略爲Release的宿主類型倉庫,用來部署組織內部的發佈版本構件。
(3)Snapshots:這是一個策略爲Snapshot的宿主類型倉庫,用來部署組織內部的快照版本構件。
(4)3rd party:這是一個策略爲Release的宿主類型倉庫,用來部署無法從公共倉庫獲得的第三方發佈版本構件。
(5)Public Repositories:該倉庫組將上述所有策略爲Release的倉庫聚合並通過一致的地址提供服務。
配置從私服下載
配置文件和順序:MAVEN_HOME/conf/setting.xml-à~/.m2/setting.xml-àproject
配置所有構件均從私服下載,在~/.m2/setting.xml中配置如下:
<settings>
<mirrors>
<mirror>
<!--此處配置所有的構建均從私有倉庫中下載 *代表所有,也可以寫central -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.1.100:8000/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!—所有請求均通過鏡像 -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
Pom.xml中的配置
<repositories>
<repository>
<id>central</id>
<url>http://localhost:8000/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://localhost:8000/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
配置上傳到私服:
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://localhost:8000/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://localhost:8000/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
Nexus的訪問權限控制,在~/m2/setting.xml中配置如下:
<!-- 設置發佈時的用戶名 -->
<servers>
<server>
<id> releases </id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id> snapshots </id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
更新遇到錯誤:
was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced
去自己的.m2 文件夾下把 xxx.lastUpdated文件全部刪掉,重新運行maven,ok!