您现在的位置是:首页 > 电脑学习教程 > 其他平台 > 文章正文 其他平台

git验证线上的版本是否符合预期

2022-07-23 10:54:34 其他平台

简介git-commit-id-maven-plugin插件,会根据当前分支的版本号生成一个git.properties文件。git.properties内容形如下git.branch=mastergit.bui...

git-commit-id-maven-plugin插件,会根据当前分支的版本号生成一个git.properties文件。git.properties内容形如下

  1. git.branch=master 
  2. git.build.host=xxx 
  3. git.build.time=2022-03-01T20\:33\:43+0800 
  4. git.build.user.email=aaa@qq.com 
  5. git.build.user.name=aaa 
  6. git.build.version=1.0-SNAPSHOT 
  7. git.closest.tag.commit.count= 
  8. git.closest.tag.name= 
  9. git.commit.id=6dab4430864e3e4a9fc1ba6f6b93f278100d4e2e 
  10. git.commit.id.abbrev=6dab443 
  11. git.commit.id.describe=6dab443-dirty 
  12. git.commit.id.describe-short=6dab443-dirty 
  13. git.commit.message.full=Add README.md 
  14. git.commit.message.short=Add README.md 
  15. git.commit.time=2022-03-01T16\:24\:48+0800 
  16. git.commit.user.email=aa@example 
  17. git.commit.user.name=aa 
  18. git.dirty=true 
  19. git.remote.origin.url=http://hello 
  20. git.tags= 
  21. git.total.commit.count=1 

如何使用

本文以springboot项目为例,springboot项目的parent pom里面已经内嵌git-commit-id-maven-plugin插件管理依赖。如下

  1. <pluginManagement> 
  2.  <plugins> 
  3.     <plugin> 
  4.           <groupId>pl.project13.maven</groupId> 
  5.           <artifactId>git-commit-id-plugin</artifactId> 
  6.           <executions> 
  7.             <execution> 
  8.               <goals> 
  9.                 <goal>revision</goal> 
  10.               </goals> 
  11.             </execution> 
  12.           </executions> 
  13.           <configuration> 
  14.             <verbose>true</verbose> 
  15.             <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat> 
  16.             <generateGitPropertiesFile>true</generateGitPropertiesFile> 
  17.             <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename> 
  18.           </configuration> 
  19.         </plugin> 
  20.    </plugins> 
  21. </pluginManagement> 

项目中做如下配置

1、在我们的项目中显式引入git-commit-id-plugin插件

  1. <build> 
  2.         <plugins> 
  3.             <plugin> 
  4.                 <groupId>pl.project13.maven</groupId> 
  5.                 <artifactId>git-commit-id-plugin</artifactId> 
  6.             </plugin> 
  7.         </plugins> 
  8.  </build> 

2、通过和actuator集成,显示git信息

a、在项目中引入actuator GAV

  1. <dependency> 
  2.             <groupId>org.springframework.boot</groupId> 
  3.             <artifactId>spring-boot-starter-actuator</artifactId> 
  4.         </dependency> 

b、浏览器访问http://ip : port/actuator/info

如果觉得上面的信息不够多,我们可以通过自定义端点或者自己写一个controller把信息展示出来

详细的信息可以通过org.springframework.boot.info.GitProperties展示

本示例以自定义端点为例。示例如下

  1. @Endpoint(id = "git"
  2. @Component 
  3. public class GitEndpoint { 
  4.     @Autowired(required = false
  5.     private GitProperties gitProperties; 
  6.     @ReadOperation 
  7.     public Object info() throws IOException { 
  8.         if(ObjectUtils.isEmpty(gitProperties)){ 
  9.             return new HashMap<>(); 
  10.         } 
  11.         return gitProperties; 
  12.     } 

在application.yml中开放自定义端点

  1. management: 
  2.   endpoints: 
  3.     web: 
  4.       exposure: 
  5.         include: 'git' 

浏览器访问http://ip : port/actuator/git

如果仍然觉得上述的信息还是不够详细,那可以通过解析git.properties文件显示。示例

  1. @Endpoint(id = "gitDetail"
  2. @Slf4j 
  3. @Component 
  4. public class GitDetailEndPoint { 
  5.     @ReadOperation 
  6.     public Object detail() throws IOException { 
  7.         Properties props = null
  8.         try { 
  9.             props = PropertiesLoaderUtils.loadAllProperties("git.properties"); 
  10.             return props; 
  11.         } catch (IOException e) { 
  12.             log.error("git.properties not found"); 
  13.         } finally { 
  14.         } 
  15.         return new HashMap<>(); 
  16.     } 

在application.yml中开放自定义端点

  1. management: 
  2.   endpoints: 
  3.     web: 
  4.       exposure: 
  5.         include: 'gitDetail' 

浏览器访问http://ip:port/actuator/gitDetail

总结

git-commit-id-maven-plugin在分布式或者微服务项目中,用来验证项目版本还是挺有用的,推荐大家有机会用一下

demo链接

相关文章

站点信息