Git commit id plugin - apinazo/booter GitHub Wiki
The Maven git commit id plugin generates a file with all the info from the git repository status when the project is packaged. This way you can always know which exact commit the packages was generated from.
<build>
<plugins>
....
<!-- Generates git commit info. -->
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.4</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<prefix>git</prefix>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<format>json</format>
<gitDescribe>
<skip>false</skip>
<always>false</always>
<dirty>-dirty</dirty>
</gitDescribe>
</configuration>
</plugin>
</plugins>
</build>
Every time the project is build - like in mvn package
- a git.properties
file will be created in the target/classes
directory, containing all the info configured before. In this example it will have JSON format.
Consecuently, that file will be also packaged and available to the application.
Any class from the application could read the git.properties
JSON formatted file. An example use is to serve the info in a REST service.
@RestController
@RequestMapping("/git")
public class GitInfoController {
@GetMapping("/info")
public String versionInformation() {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("git.properties").getFile());
String info;
try {
info = FileUtils.readFileToString(file, "UTF-8");
} catch (IOException e) {
info = "No info could be retrieved";
}
return info;
}
}
Now the info is available at the endpoint GET /git/info
. An example response would be this:
{
"git.branch": "develop",
"git.build.host": "Angels-MacBook-Pro.local",
"git.build.time": "2018-07-11T22:18:31+0200",
"git.build.user.email": "[email protected]",
"git.build.user.name": "angel",
"git.build.version": "0.0.1-SNAPSHOT",
"git.closest.tag.commit.count": "",
"git.closest.tag.name": "",
"git.commit.id": "c6dfa673125da31f61b93da11629632184e06a1d",
"git.commit.id.abbrev": "c6dfa67",
"git.commit.id.describe": "c6dfa67-dirty",
"git.commit.id.describe-short": "c6dfa67-dirty",
"git.commit.message.full": "Add custom implementation of /health endpoint and a demo endpoint.",
"git.commit.message.short": "Add custom implementation of /health endpoint and a demo endpoint.",
"git.commit.time": "2018-07-09T23:31:39+0200",
"git.commit.user.email": "[email protected]",
"git.commit.user.name": "angel",
"git.dirty": "true",
"git.remote.origin.url": "https://github.com/apinazo/booter.git",
"git.tags": ""
}
The info comes from the last commit in the repository.