Maven 版本管理與 flatten-maven-plugin 插件的使用解析
1. flatten-maven-plugin 介紹
1.1 環(huán)境
- IntelliJ IDEA 2021.3
- JDK 1.8.0_301
- Apache Maven 3.8.1
- org.codehaus.mojo:versions-maven-plugin 1.2.7
- https://www.mojohaus.org/flatten-maven-plugin/
1.2 版本占位符
自 Maven 3.5.0-beta-1 開始,可以使用 ${revision}, ${sha1} and/or ${changelist} 這樣的變量作為版本占位符。
像這樣:
<project> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.maven.ci</groupId> <artifactId>ci-parent</artifactId> <name>First CI Friendly</name> <version>${revision}</version> <properties> <revision>1.0</revision> </properties> ... </project>
或者像這樣:
<project> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.maven.ci</groupId> <artifactId>ci-parent</artifactId> <name>First CI Friendly</name> <version>${revision}${sha1}${changelist}</version> ... <properties> <revision>1.0</revision> <changelist>-SNAPSHOT</changelist> <sha1/> </properties> </project>
可以使用這樣的命令:
mvn -Drevision=2.7.8 -Dchangelist=-RELEASE -Dsha1=ssbd clean package
缺點(diǎn):
Install / Deploy 時(shí),版本占位符將不能被替換。這將導(dǎo)致 Install / Deploy 后, maven 不能識(shí)別。
使用 flatten-maven-plugin 解決這個(gè)問題。
flatten-maven-plugin:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>${flatten-maven-plugin.version}</version> <configuration> <updatePomFile>true</updatePomFile> <flattenMode>resolveCiFriendliesOnly</flattenMode> </configuration> <executions> <execution> <id>flatten</id> <phase>process-resources</phase> <goals> <goal>flatten</goal> </goals> </execution> <execution> <id>flatten-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2. 實(shí)例分析
2.1 先看一下自己構(gòu)建的項(xiàng)目
基于 COLA 4.X 構(gòu)建一個(gè)項(xiàng)目,本人目前正在寫支付中臺(tái),所以就以此為例構(gòu)建 “pointer-pay” 項(xiàng)目:
mvn archetype:generate \ -DgroupId=com.pointer.pay \ -DartifactId=pointer-pay \ -Dversion=1.0.0-SNAPSHOT \ -Dpackage=com.pointer.pay \ -DarchetypeArtifactId=cola-framework-archetype-web \ -DarchetypeGroupId=com.alibaba.cola \ -DarchetypeVersion=4.3.1
然后看一下其初始項(xiàng)目的版本管理方式:
parent:
module:
可以看到這里的父工程和modules都寫死了版本。當(dāng)然,像支付中臺(tái)或者其他不會(huì)變更需求的項(xiàng)目,這個(gè)寫也沒什么毛病。
But,在大多數(shù)互聯(lián)網(wǎng)公司中,幾乎每個(gè)項(xiàng)目都處在版本快速迭代中,甚至一兩周更新一個(gè)小版本,一個(gè)月更新一個(gè)大版本。如果還是這樣直接寫死版本的話,通常做法就是全局搜索替換版本號(hào),這樣就顯得很撈,也不太科學(xué)。然后就有了revision 的占位符統(tǒng)一管理。
2.2 再看一下開源項(xiàng)目是怎么進(jìn)行版本管理的
我們可以在 spring-boot 和 spring-cloud-alibaba 的開源項(xiàng)目中看到,其就是利用 revision 占位符來進(jìn)行統(tǒng)一版本管理的。
https://github.com/spring-projects/spring-boot/blob/2.2.x/pom.xml
https://github.com/alibaba/spring-cloud-alibaba/blob/2021.x/pom.xml
2.3 改造 pointer-pay 先看一下原來的項(xiàng)目結(jié)構(gòu):
然后利用 revision 占位符來統(tǒng)一管理版本:
父工程pom:
子工程pom:
修改完以后編譯運(yùn)行都沒問題。然后 install、deploy 的時(shí)候就出現(xiàn)問題了:打出來的jar包的pom文件里還是原來的revision變量,下面一起到maven倉庫中查看一下:
可見這里識(shí)別不出版本號(hào),也就會(huì)導(dǎo)致引用方不能識(shí)別你的 pom/jar 包。這時(shí) flatten-maven-plugin
就該出場(chǎng)了,在你的父 pom 引入相關(guān)插件:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>1.2.7</version> <configuration> <updatePomFile>true</updatePomFile> <flattenMode>resolveCiFriendliesOnly</flattenMode> </configuration> <executions> <execution> <id>flatten</id> <phase>process-resources</phase> <goals> <goal>flatten</goal> </goals> </execution> <execution> <id>flatten-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
然后重新 clean、install 一下,你會(huì)發(fā)現(xiàn)每個(gè)模塊根目錄下多了一個(gè) .flattened-pom.xml
文件,那么這個(gè)玩意是怎么生成的呢?下面一起看一下 updatePomFile
標(biāo)簽,官方文檔是這個(gè)描述的:
The flag to indicate if the generated flattened POM shall be set as POM file to the current project. By default this is only done for projects with packaging other than pom. You may want to also do this for pom packages projects by setting this parameter to true or you can use false in order to only generate the flattened POM but never set it as POM file. If flattenMode is set to bom the default value will be true.
大概意思是:updatePomFile 屬性表示是否將生成的 .flattened-pom.xml 作為當(dāng)前項(xiàng)目的 pom 文件。默認(rèn)只有打包的時(shí)候(package、install、deploy)會(huì)將 .flattened-pom.xml 做為當(dāng)前項(xiàng)目的 pom 文件,但是打包類型 pom 的 pom.xml 中的占位符是不會(huì)被替換的。如果想要都被替換,那就將 updatePomFile 的屬性設(shè)置為 true 吧。如果 flattenMode 被設(shè)置為 bom,updatePomFile 默認(rèn)屬性值為 true。
再一起看一下引入 flatten-maven-plugin
之后編譯過的 pom 文件:
到此這篇關(guān)于Maven 版本管理與 flatten-maven-plugin 插件的使用及分析的文章就介紹到這了,更多相關(guān)Maven 版本管理與 flatten-maven-plugin 插件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)展示
本文主要介紹了Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)展示2024-06-06java static塊和構(gòu)造函數(shù)的實(shí)例詳解
這篇文章主要介紹了java static塊和構(gòu)造函數(shù)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握J(rèn)ava static關(guān)鍵字的函數(shù)方法,需要的朋友可以參考下2017-09-09elasticsearch索引index數(shù)據(jù)功能源碼示例
這篇文章主要為大家介紹了elasticsearch索引index功能源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04關(guān)于Mybatis-Plus?Update更新策略問題
這篇文章主要介紹了關(guān)于Mybatis-Plus?Update更新策略問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java實(shí)現(xiàn)九宮格的簡(jiǎn)單實(shí)例
這篇文章主要介紹了 Java實(shí)現(xiàn)九宮格的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06Java?SpringBoot項(xiàng)目如何優(yōu)雅的實(shí)現(xiàn)操作日志記錄
這篇文章主要介紹了Java?SpringBoot項(xiàng)目如何優(yōu)雅的實(shí)現(xiàn)操作日志記錄,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能示例
這篇文章主要介紹了Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能,簡(jiǎn)單分析了計(jì)算機(jī)數(shù)值進(jìn)制轉(zhuǎn)換與通過位移進(jìn)行乘積計(jì)算的原理,并結(jié)合具體實(shí)例給出了java模擬計(jì)算機(jī)成績(jī)運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09