原文地址:https://www.cnblogs.com/Naylor/p/18024689
简介git-commit-id-maven-plugin 是一个maven 插件,用来在打包的时候将 git-commit 信息打进 Jar 中。
这样做的好处是可以将发布的某版本和对应的代码关联起来,方便查阅和线上项目的维护。至于它的作用,用官方说法,这个功能对于大型分布式项目来说是无价的。
功能你是否经常遇到这样的问题:
测试提交了一个bug,开发人员无法确认是哪个版本有这个问题?当前测试环境部署的是某个版本吗?生产环境会不会也有这个问题?公司内部的项目,总共几十、几百个服务,每天都有服务的生产环境部署,一个服务甚至一天上线好几次,对于项目管理来说无法清晰了解某一时刻某个服务的版本如何验证我的代码是否已经上线?。。。。。。以上种种,都有一个共同的诉求,就是我希望在打包的时候将最后一次 git commit id 和当前 jar 关联起来并可试试查询 jar 对应的 git commit id 。
实践引入插件本例 Spring Boot 版本为 2.7.6,java 版本为 11
此插件已经上传到中央仓库(https://central.sonatype.com/artifact/io.github.git-commit-id/git-commit-id-maven-plugin?smo=true)
在项目pom.xml 中引入如下插件
...... io.github.git-commit-id git-commit-id-maven-plugin 5.0.0 get-the-git-infos revision initialize true ${project.build.outputDirectory}/git. ^git.build.(time|version)$ ^git.commit.id.(abbrev|full)$ txt full generateGitPropertiesFilename:用于指定生成的 gitCommitInfo 存放到哪个位置,后缀可以任意指定,如果不指定将使用 format 的值format:指定文件后缀,一般为 properties、jsoncommitIdGenerationMode:记录完整信息,若 format 为 json,此值必须为 full此外为了能成功打出 jar 包,还需要如下插件的配合:
org.springframework.boot spring-boot-maven-plugin ${spring-boot.version} true org.projectlombok lombok repackage repackage使用 maven 执行 clean and package ,将在 targetclasses 下生成 git.json 文件,内容如下:
#Generated by Git-Commit-Id-Plugingit.build.time=2024-02-21T10:41:24+0800git.build.version=0.0.1-SNAPSHOTgit.commit.id.abbrev=3fc9c80git.commit.id.full=3fc9c8009a48e22ef171c98a97398005e9f30a4a同时,如果我们反编译生成的 jar 包,将在 BOOT-INF/classes 下看到 git.json 文件
GitCommitIdMavenPlugin 插件有丰富的配置选项,