Maven packing timestamp problem

Time:2020-9-20

When Maven is packaged, the timestamp attribute can be generated to assemble the package name.


<version>5.0.${build.time}B</version>
<properties>
 <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>

Turn on the filtering function for the resource file, and the POM variable in the resource file will be automatically replaced during the packaging process. You can get the version number by accessing the properties resource file in Java code.


<resource>
 <directory>src/main/resources</directory>
 <filtering>true</filtering>
</resource>

However, there is a problem with this timestamp attribute, the generated time${ maven.build.timestamp }Time zone cannot be modified because it is UTC time. Find a way to solve this problem with plug-ins on the Internet.


<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>build-helper-maven-plugin</artifactId>
 <version>1.8</version>
 <executions>
  <execution>
   <id>timestamp-property</id>
   <goals>
    <goal>timestamp-property</goal>
   </goals>
   <configuration>
    <name>build.time</name>
    <pattern>yyyyMMdd'.'HHmm</pattern>
    <timeZone>GMT+8</timeZone>
   </configuration>
  </execution>
 </executions>
</plugin>

Properties generated by the plug-in${ build.time }It is already the local time zone.

However, in idea, build projects, build artifacts${ project.version }It was not replaced.

After groping for a while, we found that idea build projects does not call Maven life cycle, resulting in plugin not being executed. Although Maven’s own variables can be replaced, custom variables are not.

Using the Maven install package of the tool button, the variables in the resource can be replaced normally.

There is also a method to bind the build of idea with Maven goal:

Figure 1 binding Maven goal

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.