簡介:為了在專案變更版本時檢視是否變更成功 & 檢視專案編譯時間
1、在pom檔案中新增:
<properties>
<!--maven.build.timestamp儲存了maven編譯時間戳-->
<timestamp>${maven.build.timestamp}</timestamp>
<!--指定時間格式-->
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>
<build>中新增以下內容:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
2、application.properties中新增:【注意:是@@,不是${}】
app.version = @project.version@
app.build.time = @timestamp@
3、新增一個配置類
【注意:我們所處的時區為東八區,預設列印時間為東零區,需要加8個小時】
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 專案啟動日誌:列印專案版本和構建時間
*/
@Configuration
@Slf4j
public class BeanPostConfig implements BeanPostProcessor {
// 當前版本
@Value("${app.version}")
private String serviceVersion;
// 打包時間
@Value("${app.build.time}")
private String serviceBuildDate;
@PostConstruct
public void projectInfo(){
StringBuffer projectInfo = new StringBuffer();
projectInfo.append("\n=================project=================\n");
projectInfo.append(String.format("\nservice version:%s\n",serviceVersion));
String timestamps = formatTimeHours(serviceBuildDate, 8);
projectInfo.append(String.format("\nservice build date:%s\n",timestamps));
projectInfo.append("\n=================project=================\n");
log.info(projectInfo.toString());
}
/**
* 將時間轉換為東八區時區,預設為東0區
*
* @param time
* @param hours
* @return
*/
public String formatTimeHours(String time, int hours) {
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sd.parse(time);
long rightTime = (long) (d.getTime() + hours * 60 * 60 * 1000);
String newtime = sd.format(rightTime);
return newtime;
} catch (Exception e) {
log.warn("-------系統時間轉化出現異常--------");
return null;
}
}
}
輸出日誌為: