Maven是基于項目對象模型(POM)水评,可以通過一小段描述信息來管理項目的構(gòu)建、報告和文檔的軟件項目管理工具媚送。
安裝
http://maven.apache.org/download.cgi
image.png
groupId的值就是項目的包名
artifactId是模塊/項目名
mvn常用命令
mvn compile
對項目進(jìn)行編譯
mvn test
測試
mvn package
打jar包
mvn clean
刪除target
mvn install
安裝jar包到本地倉庫
archetype插件用于創(chuàng)建符合maven規(guī)定的目錄骨架
mvn archetype:generate
按照提示進(jìn)行選擇
mvn archetype:genetate -DgroupId=...
maven中的坐標(biāo)和倉庫
groupId + artifactId + version 組成坐標(biāo)
倉庫:
- 本地倉庫
- 遠(yuǎn)程倉庫
鏡像倉庫
配置地址:apache-maven-3.6.0/conf/settings.xml
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<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>
-->
</mirrors>
更改本地倉庫位置
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
maven的生命周期和插件
- clean 清理項目
- pre-clean 執(zhí)行清理前的工作
- clean 清理上一次構(gòu)建生成的所有文件
- post-clean 執(zhí)行清理后的文件
- default 構(gòu)建項目(最核心)
- compile
- test
- package
- install
- site 生成項目站點
- pre-site 在生成項目站點前要完成的工作
- site 生成項目的站點文檔
- post-site 在生成項目站點后要完成的工作
- site-deploy 發(fā)布生成的站點到服務(wù)器上
clean中燥、compile、test塘偎、package(complile test)疗涉、install
完整的項目構(gòu)建過程包括:
清理、編譯式塌、測試博敬、打包、集成測試峰尝、驗證偏窝、部署
pom的常用元素
project
根元素,包含一些約束信息
modelVersion
固定的版本 例如4.0.0
groupId + artifactId + version + packaging(不指定時默認(rèn)為jar)
項目坐標(biāo)
name
項目描述名
describtion
項目描述
url
項目地址
optional 設(shè)置依賴是否可選
- false 子項目默認(rèn)為繼承
- true 子項目必須顯示引用該依賴
exclusions
排除依賴傳遞列表
dependencyManagement
主要定義在父模塊中武学,子模塊可以直接繼承
插件列表
<plugins>
<plugin>
<groupId>...</groupId>
<artifactId>...</artifactId>
</plugin>
</plugins>
</build>
依賴沖突
- 短路優(yōu)先
- 先聲明先優(yōu)先