These are the default life cycle phases in maven
- validate - validate the project is correct and all necessary information is available
- compile - compile the source code of the project
- test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package - take the compiled code and package it in its distributable format, such as a JAR.
- verify - run any checks on results of integration tests to ensure quality criteria are met
- install - install the package into the local repository, for use as a dependency in other projects locally
-
deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
生命周期階段與插件的綁定關(guān)系.png
一個maven項目完整的生命周期路徑
validate >> compile >> test (optional) >> package >> verify >> install >> deploy
- maven package的生命周期路徑
validate >> compile >> test (optional) >> package
- maven install的生命周期路徑
validate >> compile >> test (optional) >> package >> verify >> install
- maven clean
移除之前版本編譯的文件,也就是target目錄下的所有文件。
Maven: Failed to read artifact descriptor
項目中遇到一個問題咆蒿,子項目使用mvn compile的時候出現(xiàn)Failed to read artifact descriptor胡控,使用-e參數(shù)查看詳細日志拟杉,看到是父項目沒有安裝脑又。切換到父項目目錄庆聘,使用mvn clean install即可貌虾。
Maven的打包類型
參考文章:https://www.baeldung.com/maven-packaging-types
最近在處理項目的時候遇到了兩個問題吞加,一個是在執(zhí)行main方法的時候找不到resource文件,經(jīng)過查看編譯的target的目錄中也沒有包含resource相關(guān)文件尽狠,后經(jīng)排查是把項目的打包類型聲明成了pom衔憨,而pom主要用來聚合項目的依賴,資源文件并不會編譯打包(參考:https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003420219-Why-can-not-build-resources-)袄膏,后來把項目改成jar即可践图。第二個問題是把一個項目的api拆分出來的時候(把相關(guān)的服務(wù)接口通過feign的方式整合到api項目中,這樣其他服務(wù)只需要引入api這個包即可沉馆,不需要引入其實現(xiàn)服務(wù))把項目錯誤的聲明成了pom码党,導(dǎo)致把api服務(wù)集成到其他服務(wù)的時候報錯。兩個問題都是沒有正確的理解pom和jar之間的區(qū)別斥黑。
- pom是最簡單的一種打包方式揖盘,主要用來做依賴聚合,父項目提供依賴傳遞锌奴。一個父項目允許你在不同的pom項目之間定義繼承關(guān)系扣讼。因為它沒有resource需要處理也不需要編譯代碼,所以它不會生成任何可執(zhí)行文件缨叫。它的聲明周期只有install->deploy
- jar是最流行的打包方式椭符,也是默認的打包方式,它在打包的時候會執(zhí)行完整的聲明周期耻姥,包含java code, resource, metadata files
- 其他打包方式類似jar,比如war...
Maven常用運維指令
參考文章:https://www.cnblogs.com/hiver/p/7850954.html销钝,https://blog.51cto.com/u_330478/3625582
反應(yīng)堆(Reactor)是指所有模塊組成的一個構(gòu)建結(jié)構(gòu)
- am(--also-make):安裝其需要的依賴
- amd(-also-make-dependents):同時構(gòu)建依賴與所列模塊的模塊
- pl(--projects):構(gòu)建指定的模塊,模塊間用逗號分隔
- rf(-resume-from):從指定的模塊構(gòu)建反應(yīng)堆
比如現(xiàn)在有個項目envctl-exporter
依賴通用模塊common-db
琐簇,父模塊為demo
# 輸出調(diào)試日志
mvn install -e -X
# 語法格式
mvn clean package -Dmaven.test.skip=true -pl ${group_id}:${task_name} -am
# demo,common-db,envctl-exporter都會構(gòu)建
mvn clean package -Dmaven.test.skip=true -pl envctl-exporter -am
# common-db,envctl-exporter都會構(gòu)建
mvn clean package -Dmaven.test.skip=true -pl common-db -amd
# 只會構(gòu)建envctl-exporter
mvn clean package -Dmaven.test.skip=true -pl common-db -amd -rf envctl-exporter
Maven fails to find local artifact
Maven插件
- Maven插件幫助指令
# 輸出插件maven-source-plugin的幫助文檔
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-source-plugin:2.1.1 -Ddetail
# 輸出插件maven-help-plugin的幫助文檔
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin
- 配置插件執(zhí)行目標(biāo)蒸健,創(chuàng)建項目的源碼jar包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<!--配置插件執(zhí)行目標(biāo),創(chuàng)建項目的源碼jar包-->
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
指定jar編譯版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
spring boot maven插件
通過maven啟動spring boot項目:mvn spring-boot:run -Dapp.profiles=test
<!--https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Maven私服
# 使用docker安裝
# 創(chuàng)建宿主機數(shù)據(jù)目錄
mkdir /usr/local/nexus/data
# 賦予數(shù)據(jù)目錄全部權(quán)限
chmod 777 /usr/local/nexus/data
# docker安裝
docker run -d --name nexus3 -p 8081:8081 --restart always -v /usr/local/nexus/data:/nexus-data sonatype/nexus3
maven倉庫類型
- group:多個倉庫的集合婉商,比如
maven-public
集成了maven-releases,maven-snapshots,maven-central
;通常在配置鏡像時指定該私服鏈接;
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>雙胞胎Nexus私服</name>
<url>http://172.19.7.199:8081/repository/maven-public/</url>
</mirror>
-
proxy:代理倉庫丈秩,依賴請求轉(zhuǎn)發(fā)到代理服務(wù)器下載盯捌,比如把中央倉庫代理到阿里云
中央倉庫代理.png - hosted: 宿主倉庫饺著,也就是本地倉庫箫攀,存儲本地依賴(包含本地配置推送的和第三方庫)幼衰,比如:
maven-releases(發(fā)布版本),maven-snapshots(快照版本)
<!--分發(fā)到私服配置,release版本分發(fā)到maven-releases倉庫渡嚣;snapshot版本分發(fā)到maven-snapshots倉庫-->
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://172.19.7.199:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repository</name>
<url>http://172.19.7.199:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>