spring-bootlogbackspring-logbacklogback-classiclogstash-logback-encoder

Spring Boot service is not picking up the git.properties file generated by the gi-commit-id maven plugin


I have configured the git-commit-id plugin as follows:

    <plugin>
        <groupId>io.github.git-commit-id</groupId>
        <artifactId>git-commit-id-maven-plugin</artifactId>
        <version>6.0.0</version>
        <executions>
            <execution>
                <id>get-the-git-infos</id>
                <goals>
                    <goal>revision</goal>
                </goals>
                <phase>initialize</phase>
            </execution>
        </executions>
        <configuration>
            <generateGitPropertiesFile>true</generateGitPropertiesFile>
            <includeOnlyProperties>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                <includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
                <includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
                <includeOnlyProperty>^git.dirty$</includeOnlyProperty>
            </includeOnlyProperties>
            <commitIdGenerationMode>full</commitIdGenerationMode>
        </configuration>
    </plugin>

It works as expected and generates the following git.properties file:

#Generated by Git-Commit-Id-Plugin
git.build.time=2023-07-13T14\:04\:21+0200
git.build.version=5.0-SNAPSHOT
git.commit.id.abbrev=753455c
git.commit.id.full=753455c7ae51f231cefda280e0508feeb3655f59
git.dirty=true

All good!
That file is saved in target/classes/git.properties.

I have created the following PropertySourceConfig.java file to import the properties from git.properites:

package com.my.app.metrics;

import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

/**
 * Generates beans with access to properties required for metrics
 */
@Component
public class PropertySourceConfig {


    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propsConfig
                = new PropertySourcesPlaceholderConfigurer();
        propsConfig.setLocation(new ClassPathResource("git.properties"));
        propsConfig.setIgnoreResourceNotFound(true);
        propsConfig.setIgnoreUnresolvablePlaceholders(true);
        return propsConfig;
    }
}

This works, I have added fields annotated with @Value() pointing at the properties in git.properties to one of my controllers, and can see the correct values in those fields when I run the debugger.

I have also added the following logback-spring.xml file:

<configuration>
    <springProperty scope="context" name="applicationName" source="spring.application.name" defaultValue="unknown" />
    <springProperty scope="context" name="applicationVersion" source="project.version" defaultValue="unknown" />
    <springProperty scope="context" name="commitId" source="git.commit.id.abbrev" defaultValue="unknown" />

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

Here's where my problems start.

spring.application.name is picked up as expected.
project.version (in application.properties) is picked up too.
git.commit.id.abbrev (in git.properties) is not picked up, and so it shows up as "unknown" in the log output.

What is the issue here?

I want to say the logger is configured before the PropertySourceConfig object is created, and so there is no reference to it when the logger starts.

If so, how can I fix that?

Maybe I can't. Perhaps the strategy should be to add is info as a separate metric and push it directly to CloudWatch instead of including it in the logs at all?


Solution

  • The git.properties is read only by the info endpoint through the auto configuration. It isn't part of the environment and as such not available for property substitution.

    To do this you would need to make sure it is loaded. You can use one of 2 approaches for this.

    1. add spring.config.import=classpath:/git.properties to your application.properties. This should load it next to the application.properties.

    2. You can write an EnvironmentPostProcessor to do this.

    public class GitInfoAdder implements EnvironmentPostProcessor {
    
      public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        var gitResource = new ClasspathResource("git.properties");
        environment.getPropertySources().addLast(new ResourcePropertySource(gitResource));
      }
    }
    

    Next you need to add a spring.factories to your src/main/resources/META-INF directory to load this EnvironmentPostProcessor.

    org.springframework.boot.env.EnvironmentPostProcessor=your.package. GitInfoAdder
    

    This will add the properties as well. But I would suggest option 1 that is probably the easiest (and the one I keep forgetting).