Search code examples
gitspring-bootmavenlogginglogback

I cannot get logback to reference a generated git.properties file after I've built and deployed my service, what am I doing wrong?


I have the following plugin setup in maven:

   <plugins>
            <!-- This will add a git.properties file in the resources folder with commit information we can reference-->
            <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>
                    </execution>
                </executions>
                <configuration>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                </configuration>
            </plugin>

My logback-spring.xml looks like this:

<configuration>
    <springProperty scope="context" name="applicationVersion" source="project.version" defaultValue="unknown" />
    <springProfile name="!dev">
        <springProperty scope="context" name="gitCommitId" source="git.commit.id.abbrev" defaultValue="unknown" />
    </springProfile>

    <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>

The applicationVersion appears in the logs as expected, but this is referenced from the application.properties file. The git.properties file is generated and placed in the following locations:

target/my-service-7.0-SNAPSHOT/WEB-INF/classes/git.properties

target/classes/git.properties

As expected, gitCommitId doesn't appear when I'm running the dev profile, but when I deploy it, it appears in the logs with the default value "unknown".

What am I doing wrong?


Solution

  • I changed my strategy for this, instead of trying to access git.properties directly, I now import it into application.properties instead, then it is accessible to logback as the other properties are.
    See here for the solution: https://stackoverflow.com/a/76686438/571875