maven配置文件pom增加變量取版本號(hào)方式
maven配置文件pom增加變量取版本號(hào)
1.定義版本號(hào)
<properties> <spring.version>3.2.2.RELEASE</spring.version> </properties>
2.取版本號(hào),方便以后架包版本升級(jí)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency>
maven讀取程序版本號(hào)的3種方法
方法1
在應(yīng)用項(xiàng)目中,如果應(yīng)用程序需要獲取當(dāng)前程序的版本號(hào),可以讀取”/META-INF/maven/${groupId}/${artifactId}/pom.properties“,獲取maven生成的版本信息。
當(dāng)然前提用應(yīng)用程序在運(yùn)行時(shí)得知道自己的groupId和artifactId,否則無(wú)法定位路徑。
pom.properties內(nèi)容示例
#Created by Apache Maven .5.0 version=1.0.4-SNAPSHOT groupId=com.gitee.l0km artifactId=facelog-service
這種方法很簡(jiǎn)單,但也有缺點(diǎn):
貌似這種方法只能獲取maven默認(rèn)定義${project.version},無(wú)法加入自定義的信息。
方法2
還有一個(gè)方案就是直接將版本信息寫入MANIFEST.MF。通過(guò)java.util.jar.Manifest來(lái)讀取解析MANIFEST.MF來(lái)獲取版本號(hào)。
如下增加buildnumber-maven-plugin插件,并給maven-jar-plugin插件指定寫入MANIFEST.MF的參數(shù)。
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <goals> <goal>create</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifestEntries> <!-- 項(xiàng)目版本號(hào) --> <Project-Version>${project.version}</Project-Version> <!-- buildnumber插件提供的 SCM(git/svn等)版本號(hào) --> <Scm-Version>${buildNumber}</Scm-Version> <!-- 時(shí)間戳 --> <Timestamp>${maven.build.timestamp}</Timestamp> </manifestEntries> </archive> </configuration> </plugin>
方法3
前面兩種方案,都需要將應(yīng)用程序打成jar包才能讀取版本信息。
那么程序在開發(fā)調(diào)試的時(shí)候,并沒有生成pom.properties,和MANIFEST.MF,也就無(wú)法讀取版本信息了。
所以另一種思路就是用 template-maven-plugin插件讓maven自動(dòng)生成一個(gè)包含版本信息的代碼如Version.java。這樣任何時(shí)候,程序都能很方便的知道自己的版本號(hào)了。
模板
首先需要一個(gè)代碼模板Version.java,示例如下:
package net.gdface.facelog.service; public final class Version { /** project version */ public static final String VERSION = "${project.version}"; /** SCM(git) revision */ public static final String SCM_REVISION= "${buildNumber}"; /** SCM branch */ public static final String SCM_BRANCH = "${scmBranch}"; /** build timestamp */ public static final String TIMESTAMP ="${buildtimestamp}"; }
模板放在/src/main/java/java-templates/${package_of_template}/下
原本在模板文件中用maven內(nèi)置變量${maven.build.timestamp}做時(shí)間戳,實(shí)際運(yùn)行并沒有被正確替換,不知道原因。所以改為使用buildnumber-maven-plugin插件(goal create-timestamp)生成的時(shí)間戳${buildtimestamp}
插件
然后修改pom.xml增加 template-maven-plugin插件和buildnumber-maven-plugin插件
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <id>bn1</id> <goals> <!-- 創(chuàng)建${buildNumber} --> <goal>create</goal> </goals> </execution> <execution> <id>bn2</id> <goals> <!-- 創(chuàng)建時(shí)間戳${buildtimestamp} --> <goal>create-timestamp</goal> </goals> <configuration> <!-- 指定時(shí)間戳變量名 --> <timestampPropertyName>buildtimestamp</timestampPropertyName> <!-- 指定日期格式 --> <timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>templating-maven-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <id>filter-src</id> <goals> <goal>filter-sources</goal> </goals> </execution> </executions> </plugin>
template-maven-plugin插件會(huì)將/src/main/java/java-templates/文件夾下的所有模板中的${xxx}占位符都用maven中同名的變量替換一遍,
生成的Version.java在${project.build.directory}/generated-sources/${package_of_template}下,并且該文件夾會(huì)自動(dòng)成為源碼文件夾加入編譯過(guò)程。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java多線程編程同步器Future和FutureTask解析及代碼示例
這篇文章主要介紹了java多線程編程同步器Future和FutureTask解析及代碼示例,對(duì)二者進(jìn)行了詳細(xì)介紹,分析了future的源碼,最后展示了相關(guān)實(shí)例代碼,具有一定參考價(jià)值 ,需要的朋友可以了解下。2017-11-11Feign遠(yuǎn)程調(diào)用參數(shù)里面內(nèi)容丟失的解決方案
這篇文章主要介紹了Feign遠(yuǎn)程調(diào)用參數(shù)里面內(nèi)容丟失的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03使用java實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲
這篇文章主要介紹了使用java實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07ExecutorService實(shí)現(xiàn)獲取線程返回值
這篇文章主要介紹了ExecutorService實(shí)現(xiàn)獲取線程返回值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08基于Java實(shí)現(xiàn)中文分詞系統(tǒng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)易的中文分詞系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-07-07Mybatis給數(shù)據(jù)庫(kù)敏感字段加解密詳解
這篇文章主要介紹了Mybatis給數(shù)據(jù)庫(kù)敏感字段加解密詳解,為了保護(hù)數(shù)據(jù)庫(kù)敏感字段數(shù)據(jù)安全,有時(shí)候我們需要將敏感數(shù)據(jù)加密入庫(kù),查詢時(shí)再解密成明文,我們可以利用Mybatis自定義TypeHandler來(lái)處理,需要的朋友可以參考下2023-11-11