Spring boot Project Log print project version and build time

Keywords: Spring Maven Lombok


When the spring boot project is started, the basic information of the project will be printed: service name, project version in pom, and the time when the jar package is built. It is convenient to check whether the service is updated to the correct version, and also to provide interface access

Implementation steps (steps 2)

step 1 add pre execution code for service startup

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;

 * Execute the pre code of service startup before initializing the bean
public class BeanPostConfig implements BeanPostProcessor {
    private String serviceVersion;
    private String serviceBuildDate;
    private String serviceName;

    private Environment environment;

    public void  projectInfo(){
        StringBuffer projectInfo = new StringBuffer();
        projectInfo.append(String.format("\nservice name:%s\n",serviceName));
        projectInfo.append(String.format("\nservice version:%s\n",serviceVersion));
        projectInfo.append(String.format("\nservice build date:%s\n",serviceBuildDate));



step 2 application.yml configuration variable

  version: @project.version@
  builddate: @maven.build.timestamp@

Note that the variable to get maven uses @ @, and using @ @ in @ Value is invalid

Posted by ajcrm125 on Sun, 10 Nov 2019 12:13:12 -0800