如何獲取Maven項(xiàng)目的版本號(hào)
目前大多數(shù)Spring Boot項(xiàng)目都會(huì)打成Jar包,所以什么War包、Ear包的就先不摸索了。
Jar包的秘密
我們先解壓一個(gè)Spring Boot應(yīng)用Jar包看看里面能不能找到一些蛛絲馬跡。在META-INF文件夾中找到了兩個(gè)相關(guān)的東西,一個(gè)是MANIFEST.MF:
Manifest-Version: 1.0 Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx Implementation-Title: spring-boot-version Implementation-Version: 1.0.23 Spring-Boot-Layers-Index: BOOT-INF/layers.idx Start-Class: cn.felord.SpringBootVersionApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Build-Jdk-Spec: 1.8 Spring-Boot-Version: 2.4.5 Created-By: Maven Jar Plugin 3.2.0 Main-Class: org.springframework.boot.loader.JarLauncher
里面包含了我定義的版本號(hào)1.0.23,Implementation-Version這個(gè)值好像通過(guò)代碼能夠獲得:
String version = this.getClass().getPackage().getImplementationVersion()
但是用IDE啟動(dòng)發(fā)現(xiàn)version=null,不過(guò)用java -jar運(yùn)行時(shí)version = 1.0.23。可能與IDE運(yùn)行并不是通過(guò)jar的方式有關(guān)。
另一個(gè)是pom.properties,Maven項(xiàng)目編譯會(huì)生成一個(gè)Properties配置文件:
artifactId=spring-boot-version groupId=cn.felord version=1.0.23
這豈不是讀取Properties文件就可以了?
String path = "META-INF/maven/cn.felord/spring-boot-version/pom.properties";
ClassPathResource resource = new ClassPathResource(path);
InputStream inputStream = resource.getInputStream();
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
try (BufferedReader bufferedReader = new BufferedReader(reader)) {
bufferedReader.lines()
.forEach(System.out::println);
}
} catch (Exception ignored) {
}
這豈不是讀取Properties文件就可以了?
String path = "META-INF/maven/cn.felord/spring-boot-version/pom.properties";
ClassPathResource resource = new ClassPathResource(path);
InputStream inputStream = resource.getInputStream();
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
try (BufferedReader bufferedReader = new BufferedReader(reader)) {
bufferedReader.lines()
.forEach(System.out::println);
}
} catch (Exception ignored) {
}
依然只能從jar讀取,而且比較麻煩。這兩種方式都要依賴jar包,有木有不單純依賴jar包的呢?
Maven資源插件過(guò)濾
Maven在構(gòu)建項(xiàng)目時(shí)可以通過(guò)資源插件將構(gòu)建屬性即pom.xml中的屬性注入到指定的資源文件中,具體操作為:
<build>
...
<resources>
<!-- include main.properties -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>main.properties</include>
</includes>
</resource>
</resources>
...
</build>
恰好spring-boot-starter-parent中已經(jīng)設(shè)置了這種方式。
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
如果你是application.properties,你可以通過(guò)下面的方式來(lái)接收版本號(hào):
application.version = ${project.version}
如果是application.yaml,你可以通過(guò)下面的方式來(lái)接收版本號(hào):
application: version: '@project.version@'
然后如何取值就不用多說(shuō)了吧。這種方式不依賴jar包,使用起來(lái)也很簡(jiǎn)單。
Spring Boot提供
Spring Boot其實(shí)已經(jīng)內(nèi)置了獲取項(xiàng)目構(gòu)建信息的自動(dòng)配置ProjectInfoAutoConfiguration,它包含一個(gè)條件BeanBuildProperties:
@ConditionalOnResource(
resources = {"${spring.info.build.location:classpath:META-INF/build-info.properties}"}
)
@ConditionalOnMissingBean
@Bean
public BuildProperties buildProperties() throws Exception {
return new BuildProperties(this.loadFrom(this.properties
.getBuild()
.getLocation(), "build", this.properties
.getBuild().getEncoding()));
}
這個(gè)BuildProperties提供了不少構(gòu)建信息:
public class BuildProperties extends InfoProperties {
public BuildProperties(Properties entries) {
super(processEntries(entries));
}
public String getGroup() {
return this.get("group");
}
public String getArtifact() {
return this.get("artifact");
}
public String getName() {
return this.get("name");
}
public String getVersion() {
return this.get("version");
}
public Instant getTime() {
return this.getInstant("time");
}
}
其中的條件build-info.properties可以通過(guò)Spring Boot插件spring-boot-maven-plugin執(zhí)行下面的命令生成:
mvn spring-boot:build-info
我們只需要配置插件為:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>
build-info
</goal>
</goals>
</execution>
</executions>
</plugin>
就能使得BuildProperties生效,我們可以輕松取得相關(guān)的信息:
{
"version" : "1.0.23",
"group" : "cn.felord",
"artifact" : "spring-boot-version",
"name" : "spring-boot-version",
"time" : {
"epochSecond" : 1620664643,
"nano" : 591000000
}
}
總結(jié)
今天介紹了幾種從通過(guò)API獲取項(xiàng)目構(gòu)建版本信息的方法,有什么用呢?主要用于項(xiàng)目監(jiān)控,發(fā)版審計(jì),DevOps等領(lǐng)域,包括Spring Boot的自定義banner。如果你的項(xiàng)目涉及到CI/CD,很可能要涉及到這一方面。 如果你知道更好用的獲取方法歡迎留言討論,我來(lái)拋磚引玉。
以上就是如何獲取Maven項(xiàng)目的版本號(hào)的詳細(xì)內(nèi)容,更多關(guān)于獲取Maven項(xiàng)目的版本號(hào)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring調(diào)度注解@Scheduled方式(含分布式)
文章介紹了Java中任務(wù)調(diào)度的幾種常見(jiàn)方法,包括JDK原生的Timer、ScheduledThreadPoolExecutor和Spring的@Scheduled注解,文章還討論了如何在分布式環(huán)境中實(shí)現(xiàn)任務(wù)調(diào)度,并介紹了一些開(kāi)源的分布式任務(wù)調(diào)度解決方案,如Quartz和XXL-JOB2024-11-11
SpringBoot DBUnit 單元測(cè)試(小結(jié))
這篇文章主要介紹了SpringBoot DBUnit 單元測(cè)試(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
java模擬發(fā)送form-data的請(qǐng)求方式
這篇文章主要介紹了java模擬發(fā)送form-data的請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
java樂(lè)觀鎖原理與實(shí)現(xiàn)案例分析
這篇文章主要介紹了java樂(lè)觀鎖原理與實(shí)現(xiàn),結(jié)合具體案例形式分析了樂(lè)觀鎖的原理及java使用樂(lè)觀鎖實(shí)現(xiàn)自動(dòng)派單功能的相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
我總結(jié)的幾種@Transactional失效原因說(shuō)明
這篇文章主要是我總結(jié)的幾種@Transactional失效原因說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Spring Cloud Stream微服務(wù)消息框架原理及實(shí)例解析
這篇文章主要介紹了Spring Cloud Stream微服務(wù)消息框架原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Idea安裝Eslint插件提示:Plugin NativeScript was not installed的問(wèn)題
這篇文章主要介紹了Idea安裝Eslint插件提示:Plugin NativeScript was not installed的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
SpringBoot API接口超時(shí)時(shí)間的五種配置方式詳解
在開(kāi)發(fā)API接口時(shí),配置API接口的超時(shí)時(shí)間是一項(xiàng)非常重要的任務(wù),SpringBoot中有多種方式可以配置API接口的超時(shí)時(shí)間,下面小編就為大家介紹一下吧2025-03-03

