maven打包java程序记录编译时间

需求:有个java程序,使用maven打包成一个可以执行的jar,想在这个jar里面记录一下编译时间。

解决方案有很多种,我使用这样的方案:

1、利用maven的maven-jar-plugin插件,生成可执行jar时,会有个MANIFEST.MF文件内嵌到jar中,而这个插件可以往这个MF文件写编译时间

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Build-Time>${maven.build.timestamp}</Build-Time>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

2、现在只要把这个时间读取出来就好了

  try {
     Manifest manifest = new Manifest(this.getClass().getResourceAsStream("/META-INF/MANIFEST.MF"));
     Attributes attributes = manifest.getMainAttributes();
     attributes.getValue("Build-Time");
  } catch (IOException ex) {
     LOGGER.debug("Error reading manifest file information", ex);
  }

读取出来的是UTC时间,可以根据需求转成GMT时间。

参考资料:https://www.codenong.com/2469922/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值