需求:有个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时间。