maven项目编译时间如何记载->timestamp

本文探讨了在项目中记录编译时间的重要性,特别是在频繁打包和部署的场景下,它能帮助识别最新部署的版本。文章介绍了通过在pom.xml中定义变量asdr,并利用maven的build-helper-maven-plugin插件格式化时间戳,然后在application.properties中引用,最终在代码中读取的方法来实现编译时间的记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven项目编译时间如何记载->timestamp

1、为什么需要记载项目代码编译时间?

从业务角度,项目代码都带有版本信息(如v12.7.701)。但这种粒度仅体现了业务功能层面。
从服务部署角度,对应需要频繁打包测试或发布的版本,记录编译时间,更有助于帮助技术人员识别当前部署的项目,是否是最新打包项目。通过内置的时间戳工具,在服务启动时,直接输出。不再需要去跑到服务器上看项目后缀以及修改时间。

2、怎么实现?

//pom配置
//-------
    <properties>
        <!--maven.build.timestamp保存了maven编译时间戳-->
        <asdr>${maven.build.timestamp}</asdr>
    </properties>
//-------


//-------
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>

                <executions>
                    <execution>
                        <id>timestamp-property</id>
                        <goals>
                            <goal>timestamp-property</goal>
                        </goals>
                        <configuration>
                            <name>asdr</name>
                            <pattern>yyyy-MM-dd HH:mm:ss:SSS</pattern>
                            <timeZone>GMT+8</timeZone>
                            <locale>zh_CN</locale>
                        </configuration>
                    </execution>
                </executions>


            </plugin>
//-------
//application.properties配置
app.time= @asdr@


//TaskLoader类
@Component
public class TaskLoader implements ApplicationRunner {
    @Value("${app.time}")
    private String buildTimeStamp;
    @Override
    public void run(ApplicationArguments args){
       logger.warn("程序编译时间(GMT+8,按北京时间格式化处理)->" + buildTimeStamp);
    }
}

代码说明

1、代码中使用 asdr,这种没有业务含义的字符串进行命名,是有意为之,故意不用timestamp、build.time等这种名称。主要是为了区分概念。

2、asdr是pom中自己定义的变量,它的名称是 asdr,值是 ${maven.build.timestamp}

3、build-helper-maven-plugin通过配置,对 asdr的值进行格式化。(其中的configuration的name属性(asdr),就是我们在前面自定义的变量asdr。
)

                         <configuration>
                            <name>asdr</name>
                            <pattern>yyyy-MM-dd HH:mm:ss:SSS</pattern>
                            <timeZone>GMT+8</timeZone>
                            <locale>zh_CN</locale>
                        </configuration>

4、在application.properties中进行引用(app.time= @asdr@)
5、在代码中从application.properties中获取此变量

    @Value("${app.time}")
    private String buildTimeStamp;
    
    //.......
    logger.warn("程序编译时间(GMT+8,按北京时间格式化处理)->" + buildTimeStamp);
    //.......

好,分享结束。

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值