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 標簽配置的 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)過程詳解的詳細內容,更多關于SpringBoot 打包編譯時間的資料請關注腳本之家其它相關文章!
相關文章
Flutter插件開發(fā)之HmsScanKit實現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter插件開發(fā)之HmsScanKit實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android中AsyncTask與handler用法實例分析
這篇文章主要介紹了Android中AsyncTask與handler用法,以實例形式較為詳細的分析了Android中AsyncTask與handler的功能、用法與相關注意事項,并附帶完整實例源碼供讀者下載,需要的朋友可以參考下2015-10-10
詳解Android Handler機制和Looper Handler Message關系
Handler是Android線程之間的消息機制,主要的作用是將一個任務切換到指定的線程中去執(zhí)行,準確的說是切換到構成Handler的looper所在的線程中去出處理。本文將詳細介紹Android Handler機制和Looper Handler Message關系。2021-06-06
簡單掌握Android開發(fā)中彩信的發(fā)送接收及其附件的處理
這篇文章主要介紹了簡單掌握Android開發(fā)中彩信的發(fā)送接收及其附件的處理,由于微信的流行,使用彩信的用戶已經很少了,簡單了解即可,需要的朋友可以參考下2016-02-02

