SpringBoot 打包文件名增加編譯時間
場景:
Spring Boot 打 jar 包時,文件命名增加編譯時間;以及在 application.yml 配置文件中拿到編譯時間;
Apache Maven 3.8.2
Spring Boot 2.4.1
一、maven.build.timestamp
1、pom.xml
<properties>
<buildTime>${maven.build.timestamp}</buildTime>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>2、application.yml
app: version: @project.version@ buildTime: @buildTime@
要在 application.yml 配置中獲取到 pom.xml 中的屬性,需要添加如下配置
<build>
<!-- jar 包命名格式 -->
<finalName>${project.artifactId}-${version}-${buildTime}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>不過這種方式獲取到的時間有時區(qū)問題,拿到的是 UTC 時間,無法指定時區(qū),因此比中國晚 8 小時;為解決時區(qū)問題,可使用插件 buildnumber-maven-plugin 或 build-helper-maven-plugin 獲取編譯時間
二、buildnumber-maven-plugin
1、pom.xml
<build>
<finalName>${project.artifactId}-${version}-${timestamp}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat>
</configuration>
<executions>
<execution>
<goals>
<goal>create-timestamp</goal>
</goals>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>2、application.yml
app: version: @project.version@ buildTime: @timestamp@
三、build-helper-maven-plugin
1、pom.xml
<build>
<finalName>${project.artifactId}-${version}-${buildTime}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>buildTime</name>
<pattern>yyyyMMddHHmmss</pattern>
<locale>zh_CN</locale>
<timeZone>GMT+8</timeZone>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>2、application.yml
app: version: @project.version@ buildTime: @buildTime@
??注意:finalName 標(biāo)簽配置的 jar 包名稱格式中,如果存在冒號:時,雖然可以在 IDEA 中點擊 Run 按鈕啟動,但是部署時使用 java -jar ***.jar 無法啟動并報錯:
找不到或無法加載主類 org.springframework.boot.loader.JarLauncher
上面三種方式通過 maven 打包之后在運行,都可以在 yml 配置中獲取到編譯的時間值;但是如果沒打包,直接點擊 Run 按鈕啟動 Spring Boot 程序;maven.build.timestamp 可以正常獲取到時間,buildnumber-maven-plugin 的 timestamp 為空;而 build-helper-maven-plugin 直接報錯:
17:55:22.077 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
in 'reader', line 23, column 14:
buildTime: @buildTime@
^
以上就是SpringBoot 打包編譯時間實現(xiàn)過程詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 打包編譯時間的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android解析json數(shù)據(jù)示例代碼(三種方式)
本篇文章主要介紹了Android解析json數(shù)據(jù)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
WebView的介紹與簡單實現(xiàn)Android和H5互調(diào)的方法
這篇文章主要給大家介紹了關(guān)于WebView與簡單實現(xiàn)Android和H5互調(diào)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Retrofit2.0添加Header的方法總結(jié)(推薦)
這篇文章主要介紹了Retrofit2.0添加Header的方法總結(jié)(推薦),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
Android 添加系統(tǒng)設(shè)置屬性的實現(xiàn)及步驟
這篇文章主要介紹了Android 添加系統(tǒng)設(shè)置屬性的實現(xiàn)及步驟的相關(guān)資料,需要的朋友可以參考下2017-07-07

