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 就該出場了,在你的父 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven 集成 groovy 腳本插件gmavenplus-plugin詳解
文章介紹了在Maven構(gòu)建過程中使用Groovy腳本進(jìn)行動(dòng)態(tài)配置的示例,重點(diǎn)解析了如何在`<build>`配置片段中綁定腳本到`initialize`生命周期階段,并詳細(xì)說明了腳本執(zhí)行流程、常見使用場景以及潛在問題和優(yōu)化建議,感興趣的朋友跟隨小編一起看看吧2025-12-12
解決idea中javaweb的mysql8.0.15配置問題
這篇文章主要介紹了idea中javaweb的mysql8.0.15配置問題 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
java調(diào)用webservice的.asmx接口的使用步驟
這篇文章主要介紹了java調(diào)用webservice的.asmx接口的使用步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問限流與黑名單攔截
本文主要介紹了Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問限流與黑名單攔截,包含針對用戶IP限流,整個(gè)接口的訪問限流,以及對某個(gè)參數(shù)字段的限流,并且支持請求限流后處理回調(diào),感興趣的可以了解一下2024-02-02
Caffeine中本地緩存的最佳實(shí)踐與性能優(yōu)化指南
作為 Java 生態(tài)中性能優(yōu)異的本地緩存實(shí)現(xiàn),Caffeine 提供了更高的吞吐量和更低的延遲,本文將深入剖析 Caffeine 的核心原理、源碼實(shí)現(xiàn)和實(shí)際應(yīng)用,希望對大家有所幫助2025-09-09
java?工作流引擎設(shè)計(jì)實(shí)現(xiàn)解析流程定義文件
這篇文章主要為大家介紹了java?工作流引擎設(shè)計(jì)與實(shí)現(xiàn)及流程定義文件解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

