SpringBoot使用git-commit-id-maven-plugin打包

打印 上一主题 下一主题

主题 863|帖子 863|积分 2589

简介

git-commit-id-maven-plugin 是一个maven 插件,用来在打包的时候将git-commit 信息打进jar中。
这样做的好处是可以将发布的某版本和对应的代码关联起来,方便查阅和线上项目的维护。至于它的作用,用官方说法,这个功能对于大型分布式项目来说是无价的。
功能

你是否经常遇到这样的问题:

  • 测试提交了一个bug,开发人员无法确认是哪个版本有这个问题,当前测试环境部署的是某个版本吗?生产环境会不会也有这个问题?
  • 公司内部的项目,总共几十、几百个服务,每天都有服务的生产环境部署,一个服务甚至一天上线好几次,对于项目管理来说无法清晰了解某一时刻某个服务的版本
  • 如何验证我的代码是否已经上线?
  • 。。。。。。
以上种种,都有一个共同的诉求,就是我希望在打包的时候将最后一次 git commit id 和当前 jar 关联起来并可试试查询jar对应的git commit id 。
实践

引入插件

本例SpringBoot版本为 2.7.6,java版本为11
此插件已经上传到中央仓库(https://central.sonatype.com/artifact/io.github.git-commit-id/git-commit-id-maven-plugin?smo=true)
在项目pom.xml 中引入如下插件
  1. <project>
  2.     ......
  3.     <build>
  4.         <plugins>
  5.            
  6.             <plugin>
  7.                 <groupId>io.github.git-commit-id</groupId>
  8.                 <artifactId>git-commit-id-maven-plugin</artifactId>
  9.                 <version>5.0.0</version>
  10.                 <executions>
  11.                     <execution>
  12.                         <id>get-the-git-infos</id>
  13.                         <goals>
  14.                             <goal>revision</goal>
  15.                         </goals>
  16.                         <phase>initialize</phase>
  17.                     </execution>
  18.                 </executions>
  19.                 <configuration>
  20.                     <generateGitPropertiesFile>true</generateGitPropertiesFile>
  21.                     <generateGitPropertiesFilename>${project.build.outputDirectory}/git.</generateGitPropertiesFilename>
  22.                     <includeOnlyProperties>
  23.                         <includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
  24.                         <includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
  25.                     </includeOnlyProperties>
  26.                     <format>txt</format>
  27.                     <commitIdGenerationMode>full</commitIdGenerationMode>
  28.                 </configuration>
  29.             </plugin>
  30.         </plugins>
  31.     </build>
  32. </project>
复制代码

  • generateGitPropertiesFilename:用于指定生成的gitCommitInfo存放到哪个位置,后缀可以任意指定,如果不指定将使用format的值
  • format:指定文件后缀,一般为 properties , json
  • commitIdGenerationMode:记录完整信息,若format为json,此值必须为full
此外为了能成功打出jar包,还需要如下插件的配合:
  1. <plugin>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-maven-plugin</artifactId>
  4.     <version>${spring-boot.version}</version>
  5.     <configuration>
  6.         <includeSystemScope>true</includeSystemScope>
  7.         <excludes>
  8.             <exclude>
  9.                 <groupId>org.projectlombok</groupId>
  10.                 <artifactId>lombok</artifactId>
  11.             </exclude>
  12.         </excludes>
  13.     </configuration>
  14.     <executions>
  15.         <execution>
  16.             <id>repackage</id>
  17.             <goals>
  18.                 <goal>repackage</goal>
  19.             </goals>
  20.         </execution>
  21.     </executions>
  22. </plugin>
复制代码
使用maven执行 clean and  package  ,将在target\classes下生成 git.json文件,内容如下:
  1. #Generated by Git-Commit-Id-Plugin
  2. git.build.time=2024-02-21T10\:41\:24+0800
  3. git.build.version=0.0.1-SNAPSHOT
  4. git.commit.id.abbrev=3fc9c80
  5. git.commit.id.full=3fc9c8009a48e22ef171c98a97398005e9f30a4a
复制代码
同时,如果我们反编译生成的jar包,将在BOOT-INF/classes下看到git.json 文件
GitCommitIdMavenPlugin插件有丰富的配置选项,更多配置参考:
  1. <configuration>
  2.    
  3.     <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
  4.    
  5.     <prefix>git</prefix>
  6.    
  7.    
  8.     <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
  9.    
  10.    
  11.     <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
  12.    
  13.     <verbose>false</verbose>
  14.    
  15.     <generateGitPropertiesFile>true</generateGitPropertiesFile>
  16.    
  17.     <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
  18.    
  19.     <format>properties</format>
  20.    
  21.     <skipPoms>true</skipPoms>
  22.    
  23.    
  24.     <injectAllReactorProjects>false</injectAllReactorProjects>
  25.    
  26.    
  27.     <failOnNoGitDirectory>true</failOnNoGitDirectory>
  28.    
  29.    
  30.     <failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
  31.    
  32.    
  33.     <skip>false</skip>
  34.    
  35.    
  36.     <offline>false</offline>
  37.    
  38.    
  39.     <runOnlyOnce>false</runOnlyOnce>
  40.    
  41.    
  42.     <excludeProperties>
  43.        
  44.     </excludeProperties>
  45.    
  46.    
  47.     <includeOnlyProperties>
  48.        
  49.     </includeOnlyProperties>
  50.    
  51.    
  52.     <replacementProperties>
  53.        
  54.     </replacementProperties>
  55.    
  56.    
  57.     <useNativeGit>false</useNativeGit>
  58.    
  59.    
  60.     <nativeGitTimeoutInMs>30000</nativeGitTimeoutInMs>
  61.    
  62.    
  63.     <abbrevLength>7</abbrevLength>
  64.    
  65.    
  66.     <commitIdGenerationMode>flat</commitIdGenerationMode>
  67.    
  68.    
  69.     <gitDescribe>
  70.        
  71.         <skip>false</skip>
  72.        
  73.         <always>true</always>
  74.        
  75.         <abbrev>7</abbrev>
  76.        
  77.         <dirty>-dirty</dirty>
  78.        
  79.         <match>*</match>
  80.        
  81.         <tags>false</tags>
  82.        
  83.         <forceLongFormat>false</forceLongFormat>
  84.     </gitDescribe>
  85.    
  86.    
  87.     <validationProperties>
  88.         <validationProperty>
  89.            
  90.             <name>validating project version</name>
  91.            
  92.             <value>${project.version}</value>
  93.            
  94.             <shouldMatchTo><![CDATA[^.*(?<!-SNAPSHOT)$]]></shouldMatchTo>
  95.         </validationProperty>
  96.        
  97.     </validationProperties>
  98.    
  99.    
  100.     <validationShouldFailIfNoMatch>true</validationShouldFailIfNoMatch>
  101.    
  102.    
  103.     <evaluateOnCommit>HEAD</evaluateOnCommit>
  104.    
  105.    
  106.            useBranchNameFromBuildEnvironment>true</useBranchNameFromBuildEnvironment>
  107. <injectIntoSysProperties>true</injectIntoSysProperties>
  108. </configuration>
复制代码
查询gitCommitInfo

通过编写一个接口,用来查询生成的GitCommitInfo,核心代码如下:
  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.util.ResourceUtils;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import java.io.*;
  7. @Slf4j
  8. @RestController
  9. @RequestMapping("/version")
  10. public class VersionController {
  11.     /**
  12.      * 获取 git.json 中的内容
  13.      * @return
  14.      * @throws IOException
  15.      */
  16.     @GetMapping("/gitCommitId")
  17.     public String getGitCommitId() throws IOException {
  18.         //git.json  or  git.properties
  19.         File file = ResourceUtils.getFile("classpath:git.json");
  20.         if (file.exists()) {
  21.             String s = "";
  22.             InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
  23.             BufferedReader br = new BufferedReader(in);
  24.             StringBuffer content = new StringBuffer();
  25.             while ((s = br.readLine()) != null) {
  26.                 content = content.append(s);
  27.             }
  28.             return content.toString();
  29.         } else {
  30.             return "";
  31.         }
  32.     }
  33. }
复制代码
兼容性

与java的兼容性


  • java8:插件版本=4.x
  • java11:插件版本>=5
与maven的兼容性


  • maven3:插件版本=4.x
  • maven3.2.x:插件版本>=7
引用

技术交流QQ群:1158377441 欢迎关注我的微信公众号【TechnologyRamble】,后续博文将在公众号首发:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

八卦阵

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表