Explain in detail that spring boot obtains the version number and compile time stamp in Maven POM


This article first introduces how to obtain the compilation time and version number of a project in springboot, and provides an interface to the outside world. Then it introduces the solution to the time zone error of the new version of Maven. Finally, it introduces two methods of adding the timestamp to the package name.

The springboot project obtains the compilation time stamp and version number, and then provides the interface, which is roughly divided into the following steps:

  • Get the compile time stamp of Maven in POM file
  • Configure POM and render resource files with attributes in Maven application.yml
  • Provide interface

Get compile time in maven

Add two properties to the POM file properties

 <!-- maven.build.timestamp Saved Maven compile timestamp -- >
 <! -- specify time format -- > 
 <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>

Render with Maven properties application.yml

Configuration in POM file build


stay application.yml Medium configuration

Cannot use ${}

 version: @[email protected]
 time: @[email protected]

Provide interface

Create AppController and provide interface get / APP

package cn.yshow.modules.sys.controller;

import cn.yshow.common.utils.restResult.RestResult;
import cn.yshow.common.utils.restResult.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

 * @author zhaod [email protected]
 * @version v1.0
 * @since 2018/12/16 16:19
public class AppController {
  *Current version
 private String version;
  *Packing time
 private String buildTime;

 public RestResult uploadImg() {
  Map<String,String> ret = new HashMap<>();
  //Resultresult is the return object I encapsulate
  return ResultGenerator.genSuccessResult(ret);


Yapi was used to test as follows

maven.build.timestamp Time zone error resolution

In Maven 3.2.2 +, maven.build.timestamp It has been redefined to show that the time in UTC is 8 hours slower than that in China. You can use the plug-in build helper Maven plugin to get the time in this time zone

Add a plug-in in to the plugins block. This configuration is different from the official website. An error will be reported according to the configuration mode of the official website

  <! -- specifies the package name -- >
     <pattern>yyyy-MM-dd HH:mm</pattern>

After the above processing, the${ build.time }The time already represents the GMT-8 time zone

application.yml The configuration is as follows

 version: @[email protected]
 time: @[email protected]

The idea compilation project does not call the Maven lifecycle, resulting in the installed plugin not being executed. Although Maven’s own variables can be replaced, custom variables are not.

You can bind the build of idea with Maven goal, and execute Maven goal before build. On the right side of idea, Maven projects, lifecycle, compile check execute after build and execute after rebuild, as shown in the following figure

Add timestamp to package name

Do not repeat the two methods, otherwise

Method 1: add the timestamp to the version number


Method 2: add the timestamp to the package name directly


reference material

Maven packing timestamp problem

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.