通过插件生成编译时间代码,然后先compile一次生成代码,Java代码就可以引用到该时间,后面compile或者package的时候就会刷新这个时间,日志就可以打印每次编译的时间。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>timestamp-property</id>
<phase>initialize</phase>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>build.timestamp</name>
<pattern>yyyy-MM-dd HH:mm:ss</pattern>
<timeZone>Asia/Shanghai</timeZone>
</configuration>
</execution>
<execution>
<id>add-generated-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- 生成包含时间戳的 Java 类 -->
<echo file="${project.build.directory}/generated-sources/src/main/java/com/chrollo/common/BuildTime.java">
package com.chrollo.common;
public class BuildTime {
public static final String TIMESTAMP = "${build.timestamp}";
}
</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>