我想在Spring Boot启动时在日志中打印构建信息,例如项目版本和构建时间。

7 浏览
0 Comments

我想在Spring Boot启动时在日志中打印构建信息,例如项目版本和构建时间。

我在一个基于微服务架构的项目中工作,每个服务都依赖于基础jar包。

因此,我希望在Spring Boot应用程序启动时打印构建信息(项目版本和构建时间)。

请建议一种只在基础jar包中进行更改的方法。

对于每个服务,我的代码如下所示:

public class PaymentApplication extends SpringBootServletInitializer {
    @Autowired
    Optional buildProperties;
    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentApplication.class);
    
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext appContext = SpringApplication.run(PaymentApplication.class, args);
        appContext.registerShutdownHook();
    }
    
    @PostConstruct
    private void logVersion() {
        if(buildProperties.isPresent()) {
            LOGGER.info("### BUILD_INFO=[version={}, buildTime={}]",buildProperties.get().getVersion(),buildProperties.get().getTime());
        }
    }
}

pom.xml中的插件配置如下:



    org.springframework.boot
    spring-boot-maven-plugin
    
        
            build-info
            
                build-info
            
        
    

0