場(chǎng)景: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>
??不過(guò)這種方式獲取到的時(shí)間有時(shí)區(qū)問(wèn)題,拿到的是 UTC 時(shí)間,無(wú)法指定時(shí)區(qū),因此比中國(guó)晚 8 小時(shí);為解決時(shí)區(qū)問(wèn)題,可使用插件 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 包名稱格式中,如果存在冒號(hào):時(shí),雖然可以在 IDEA 中點(diǎn)擊 Run 按鈕啟動(dòng),但是部署時(shí)使用 java -jar ***.jar 無(wú)法啟動(dòng)并報(bào)錯(cuò):
找不到或無(wú)法加載主類 org.springframework.boot.loader.JarLauncher
??上面三種方式通過(guò) maven 打包之后在運(yùn)行,都可以在 yml 配置中獲取到編譯的時(shí)間值;但是如果沒(méi)打包,直接點(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@
^