SpringBoot 打包文件名增加編譯時(shí)間
場景:
Spring Boot 打 jar 包時(shí),文件命名增加編譯時(shí)間;以及在 application.yml
配置文件中拿到編譯時(shí)間;
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>
不過這種方式獲取到的時(shí)間有時(shí)區(qū)問題,拿到的是 UTC 時(shí)間,無法指定時(shí)區(qū),因此比中國晚 8 小時(shí);為解決時(shí)區(qū)問題,可使用插件 buildnumber-maven-plugin
或 build-helper-maven-plugin
獲取編譯時(shí)間
二、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 包名稱格式中,如果存在冒號:
時(shí),雖然可以在 IDEA 中點(diǎn)擊 Run
按鈕啟動(dòng),但是部署時(shí)使用 java -jar ***.jar
無法啟動(dòng)并報(bào)錯(cuò):
找不到或無法加載主類 org.springframework.boot.loader.JarLauncher
上面三種方式通過 maven 打包之后在運(yùn)行,都可以在 yml 配置中獲取到編譯的時(shí)間值;但是如果沒打包,直接點(diǎn)擊 Run
按鈕啟動(dòng) Spring Boot 程序;maven.build.timestamp
可以正常獲取到時(shí)間,buildnumber-maven-plugin
的 timestamp 為空;而 build-helper-maven-plugin
直接報(bào)錯(cuò):
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 打包編譯時(shí)間實(shí)現(xiàn)過程詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 打包編譯時(shí)間的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android 谷歌推薦的VR實(shí)現(xiàn)方式(分享)
下面小編就為大家分享一篇Android 谷歌推薦的VR實(shí)現(xiàn)方式。具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Flutter學(xué)習(xí)之矢量圖SVG的區(qū)域填色示例詳解
這篇文章主要為大家介紹了Flutter學(xué)習(xí)之矢量圖SVG的區(qū)域填色示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Flutter插件開發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter插件開發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android中AsyncTask與handler用法實(shí)例分析
這篇文章主要介紹了Android中AsyncTask與handler用法,以實(shí)例形式較為詳細(xì)的分析了Android中AsyncTask與handler的功能、用法與相關(guān)注意事項(xiàng),并附帶完整實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-10-10詳解Android Handler機(jī)制和Looper Handler Message關(guān)系
Handler是Android線程之間的消息機(jī)制,主要的作用是將一個(gè)任務(wù)切換到指定的線程中去執(zhí)行,準(zhǔn)確的說是切換到構(gòu)成Handler的looper所在的線程中去出處理。本文將詳細(xì)介紹Android Handler機(jī)制和Looper Handler Message關(guān)系。2021-06-06Activity配置、啟動(dòng)和關(guān)閉activity實(shí)例詳解
這篇文章主要介紹了Activity配置、啟動(dòng)和關(guān)閉activity實(shí)例詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09簡單掌握Android開發(fā)中彩信的發(fā)送接收及其附件的處理
這篇文章主要介紹了簡單掌握Android開發(fā)中彩信的發(fā)送接收及其附件的處理,由于微信的流行,使用彩信的用戶已經(jīng)很少了,簡單了解即可,需要的朋友可以參考下2016-02-02Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)實(shí)例詳解
系統(tǒng)的啟動(dòng)過程非常復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于Android?app啟動(dòng)節(jié)點(diǎn)與上報(bào)啟動(dòng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04