Spring to pick up git commit via Maven plugin or CI/CD variables (gitlab)
If you need a maven plugin that creates a properties file with the information taken from the .git directory, then use the git-commit-id maven plugin available from maven central.
Here is how I configured. At the initialise phase it reads the .git from the parent directory (we use a monorepo), then it generates a git.properties (that gets added into the classpath at target/classes/git.properties) with a specific list of git properties.
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
</generateGitPropertiesFilename>
<commitIdGenerationMode>full</commitIdGenerationMode>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.commit.id.full</includeOnlyProperty>
<includeOnlyProperty>git.commit.message.short</includeOnlyProperty>
<includeOnlyProperty>git.commit.time</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
I could then pick up the property file easily from Spring, as the file is in the classpath from the start.
package com.boardintelligence.writerapi.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Data
@Configuration
@PropertySource("classpath:git.properties")
public class GitCommitProperties {
@Value("${git.commit.id.abbrev:na}")
private String abbrev;
@Value("${git.commit.id.full:na}")
private String full;
@Value("${git.commit.message.short:na}")
private String messageShort;
@Value("${git.commit.time:na}")
private String time;
}
If the .git directory is not accessible from the CI/CD pipeline
If your pipeline builds the application without the .git directory (e.g. inside the docker container), you can add a src/main/resources/git.properties file with default values (and also more easily test its usage) and override it from the shell.
On Gitlab I’ve solved by adding some “before_script” commands to override that properties file with the available runner variables, just before the src are copied inside the container and the build starts
#.gitlab-ci.ymlyour-task:
before_script:
- echo -n "git.commit.id.abbrev=${CI_COMMIT_SHORT_SHA}\ngit.commit.id.full=${CI_COMMIT_SHA}\ngit.commit.message.short=${CI_COMMIT_MESSAGE}\ngit.commit.time=${CI_COMMIT_TIMESTAMP}" > src/main/resources/git.properties
(I didn’t have time to find out how do it in one line, the “\n” was not recognised as a new line)