优秀的编程知识分享平台

网站首页 > 技术文章 正文

java项目必备maven插件之git-commit-id-plugin

nanyue 2024-08-19 19:00:54 技术文章 6 ℃
原创不易,请多多支持!对Java技术感兴趣的童鞋请关注我,后续技术分享更精彩。

背景

后台上线应用是否启动成功,我们可以用healthcheck请求检查。但上线的代码是否生效,我们该如何确认呢?有人可能会说查看应用日志。是的,这是一种方法,却不是一种好的方式。这种方式需保证:每次上线的代码都要打印不同的日志,以示差异。而且查日志过程很繁琐。这里推荐一款git-commit-id-plugin maven插件,很好的解决了该场景问题。

实现思路

git-commit-id-plugin以maven插件方式,集成到项目pom文件中。在项目打包时,通过git指令,获取当前打包项目分支git元数据,并保存这些信息到打包target目录下的git.properties文件中。再配合spring-boot-starter-actuator。应用启动完成,通过http://{domain}/actuator/info接口即可查看最新代码git信息,从而校验新版本是否上线成功。

集成

添加maven插件

pom.xml文件中添加以下插件信息

<plugin>
 <!--https://github.com/git-commit-id/maven-git-commit-id-plugin-->
 <groupId>pl.project13.maven</groupId>
 <artifactId>git-commit-id-plugin</artifactId>
 <version>3.0.1</version>
 <executions>
 <execution>
 <id>get-the-git-infos</id>
 <goals>
 <goal>revision</goal>
 </goals>
 <phase>initialize</phase>
 </execution>
 </executions>
 <configuration>
 <generateGitPropertiesFile>true</generateGitPropertiesFile>
 <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
 <includeOnlyProperties>
 <!--设置git.properties文件保存的git元数据属性, 支持正则表达式-->
 <includeOnlyProperty>^git.commit.*lt;/includeOnlyProperty>
 <includeOnlyProperty>^git.build.*lt;/includeOnlyProperty>
 <includeOnlyProperty>^git.branchlt;/includeOnlyProperty>
 <includeOnlyProperty>^git.tagslt;/includeOnlyProperty>
 </includeOnlyProperties>
 <commitIdGenerationMode>full</commitIdGenerationMode>
 </configuration>
 </plugin>

添加spring-boot-starter-actuator依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

springboot yml配置添加以下属性

#spring actuator setting
#http://{domain}/actuator/info
management:
 info: # 打开info actuator/info请求访问权限
 defaults:
 enabled: true
 git: # https://github.com/git-commit-id/maven-git-commit-id-plugin
 mode: full

maven项目打包后,target生成git.properties文件,文件记录git元数据信息如下。

验证

启动项目,浏览器访问http://{ip:port}/actuator/info查看版本信息。

小结

本文通过以下步骤,介绍了git-commit-id-plugin插件从了解到使用的过程。简单集成,立马见效。程序之美无过如此。

  • git-commit-id-plugin maven插件解决的问题场景。
  • git-commit-id-plugin的实现思路。
  • 项目集成示例。
最近发表
标签列表