2

I am working in a micro services based architecture where each service has dependency of base jar.

So I want to print BUILD info(project version and build time) at the Spring Boot application startup.

Please suggest a way to make the changes only in base jar .

for individual service My code goes like this:

public class PaymentApplication extends SpringBootServletInitializer {

    @Autowired
    Optional<BuildProperties> 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());
        }
    }

plugin in pom.xml

    <!-- Build Info plug-in -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>build-info</id>
                            <goals>
                                <goal>build-info</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
Kuldeep Singh
  • 1,214
  • 4
  • 18
  • 45
  • 1
    Hey. have a look to this : https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-properties-and-configuration and this : https://stackoverflow.com/questions/38983934/cannot-get-maven-project-version-property-in-a-spring-application-with-value. Might help you. – RUARO Thibault Feb 05 '20 at 07:39
  • I don't want any properties to be added in application.properties. One of the Solution I got add a class in base jar like below: `@EventListener public void logBuildInfo(ContextRefreshedEvent event) { if(buildProperties.isPresent()) LOGGER.info("### BUILD_INFO=[version={}, buildTime={}]",buildProperties.get().getVersion(),buildProperties.get().getTime()); }` ContextRefreshedEvent will be executed at the time of ctx initialization or ctx refreshed. – Rajesh Lohar Feb 05 '20 at 12:26

0 Answers0