Search code examples
javaspring-bootlogback

How to access Spring Boot`s build properties as normal Spring properties?


I would like to use the build properties in /META-INF/build-info.properties as normal Spring properties, so that I am able to use them in via spring-property in my Logback configuration logback-spring.xml.

I was already able to use them, but something changed...

  • The properties file /META-INF/build-info.properties is there.
  • I am able get the bean BuildProperties as bean with the correct values.
  • The build properties are not registred in the application context as property sources?

Does someone knows, how to use the build properties like normal Spring properties in my logback-spring.xml?

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

  <springProperty scope="context" name="application" source="build.name"/>
  <springProperty scope="context" name="version" source="build.version"/>

Solution

  • In your Spring Boot application, make sure that the build-info.properties file is being loaded as a property source. This can be done by adding the following entry to your application.properties

    spring.config.name=application,build-info
    spring.config.location=classpath:/,classpath:/META-INF/
    

    Once the build properties are loaded as normal Spring properties, you can use them in your logback-spring.xml configuration file by referencing them using the ${propertyKey} syntax.

    like example-:

    <configuration>
       <property resource="META-INF/build-info.properties" />
    
       <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
           <!-- Use the build property in the appender configuration -->
           <encoder pattern="${build.version} - %msg%n" />
       </appender>
    
       <root level="INFO">
           <appender-ref ref="CONSOLE" />
       </root>
    </configuration>
    

    In this example, the ${build.version} property is accessed from the build-info.properties file and used within the configuration.