当前位置: 首页 > ds >正文

springboot项目启动时打印maven打包时间

背景

生产环境部署的时候,开发给的包部署后没有生效;运维说开发给的包不对,开发说运维没有部署正确;于是想着能有一种方法来减少这种错误,启动的时候把打包的时间打印出来;

pom文件配置

在pom文件中新增properties,maven打包的时候会读取该配置:

<properties><maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ssXXX</maven.build.timestamp.format>
</properties>
新增一个properties文件

在资源目录新增一个properties文件,注意这里一定是一个properties文件,不能是yaml
内容如下即可:

# maven 打包时间
build.time= @maven.build.timestamp@

用两个@符号将maven打包可识别的变量包住

配置pom文件,让打包的时候能让maven编译时替换properties文件内容
<build><resource><directory>src/main/resources</directory><includes><include>**/**</include></includes><filtering>true</filtering></resource></resources>
</build>

关键配置:filtering 为true

新增一个配置类映射properties文件内字段
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Objects;/*** maven打包相关配置类*/
@Component
@PropertySource(value = {"classpath:xxx.properties"}, encoding = "utf-8")
@ConfigurationProperties(prefix = "build")
@Getter
@Setter
@Slf4j
public class MavenBuildConfig implements InitializingBean {/*** 打包时间*/private String time;@Overridepublic void afterPropertiesSet() throws Exception {// utc时间String buildTime = this.getTime();if (Objects.nonNull(buildTime) && buildTime.contains("T") && buildTime.endsWith("Z")) {try {// 自定义格式(UTC 时间 → 本地时间字符串)Instant instant = Instant.parse(buildTime);DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Utils.YYYY_MM_DD_HH_MM_SS).withZone(ZoneId.systemDefault());buildTime = formatter.format(instant);} catch (Exception e) {log.error("afterPropertiesSet Error: {}", e.getMessage(), e);}}log.info("========================== Maven-Build-Time: {} ==========================", buildTime);}
}
启动查看效果

在这里插入图片描述

大功告成。over~~

http://www.xdnf.cn/news/19573.html

相关文章:

  • [Mysql数据库] 知识点总结8
  • 计算机网络:(十六)TCP 的运输连接管理
  • Ring Buffer解析
  • 仓颉语言Web框架中的路由分组
  • linux系统学习(6.软件包管理)
  • 十分钟快速掌握 YML YAML 文件
  • 07.《交换机三层功能、单臂路由与端口安全基础知识》
  • 在Linux环境安装Maven(保姆级别)
  • leetcode 面试题 01.01.判定字符是否唯一
  • 【高级】系统架构师 | 信息系统基础
  • 基于Seurat的空转单样本数据分析流程学习(一)
  • JavaScript中的XMLHttpRequest对象分析
  • 基于单片机智能保温杯/智能水杯
  • Java基础第7天总结(代码块、内部类、函数式编程)
  • 【多模态】使用LLM生成html图表
  • 打开多个Excel文件后快速关闭所有的文档,并且退出Excel应用
  • s[:] = reversed(s) 和 s = reversed(s)的区别
  • 【Proteus仿真】点亮小灯系列仿真——小灯闪烁/流水灯/交通灯
  • R3:适用于 .NET 的新一代响应式扩展库,事件订阅流
  • TFS-2002《Fuzzy Clustering With Viewpoints》
  • 嵌入式ARM程序高级调试技能:19.qumu arm elf无法生成coredump
  • 接口测试:如何定位BUG的产生原因
  • nginx-增加VTS模块
  • 数据结构八股
  • 数据结构(C语言篇):(八)栈
  • vscode+EIDE+Clangd环境导入keil C51以及MDK工程
  • shell脚本第六阶段---三剑客之sed
  • C++日志系统:高效异步日志实现解析
  • LeetCode 36. 有效的数独 - 解题思路与实现详解
  • ans.1中的对象标识符OBJECT_IDENTIFIER----OID