Spring集成获取git版本号 ~~
Posted NewWorldForU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring集成获取git版本号 ~~相关的知识,希望对你有一定的参考价值。
目录
需求
需求描述:需要加入版本控制,所以需要获取本次提交 git 的commit id
方案
1、配置文件中写静态的哈希值,每次发布都修改。
优势: 开发周期短。
劣势: 会死在上边。。。。。
2、动态获取git提交的commit id,会在编译的时候在classes下生成一个git.properties,在前端访问的时候直接读取配置文件。
优势:大大减少错误的概率。(默认但凡需要人每次操作的部分都会存在错误的可能性)
技术实现
需要先集成一个工具 [git-commit-id-plugin]
<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>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<!-- 是否生 git.properties 属性文件 -->
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<!-- 生成版本信息文件名 -->
<generateGitPropertiesFilename>$project.build.outputDirectory/git.properties</generateGitPropertiesFilename>
<format>json</format>
<!-- git描述配置,可选;由JGit提供实现 -->
<gitDescribe>
<!-- 是否生成描述属性 -->
<skip>false</skip>
<!-- 提交操作未发现tag时,仅打印提交操作ID -->
<always>false</always>
<!-- 提交操作ID显式字符长度,最大值为:40;默认值:7; 0代表特殊意义;后面有解释; -->
<abbrev>7</abbrev>
<!-- 构建触发时,代码有修改时(即"dirty state"),添加指定后缀;默认值:""; -->
<dirty>-dirty</dirty>
</gitDescribe>
</configuration>
</plugin>
实现类
package com.yunhuxi.jwgateway.service.version.impl;
import com.alibaba.fastjson.JSONObject;
import com.yunhuxi.jwgateway.service.version.VersionService;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* @Author: jiahao.zhao
* @Date: 2021/2/25 16:55
*/
@Service
public class VersionServiceImpl implements VersionService
@Override
public String getCommitId()
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("git.properties");
try
String gitProperties = readFromInputStream(inputStream);
JSONObject jsonObject = JSONObject.parseObject(gitProperties);
String commitId = (String) jsonObject.get("git.commit.id.abbrev");
return commitId;
catch (IOException e)
e.printStackTrace();
return "Version information could not be retrieved.";
private String readFromInputStream(InputStream inputStream)
throws IOException
StringBuilder result = new StringBuilder();
try (
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8.name()))
)
String line;
while ((line = br.readLine()) != null)
result.append(line).append(System.lineSeparator());
return result.toString();
效果
"git.branch" : "origin/feature/V3.0.0/20210222_dev",
"git.build.host" : "LAPTOP-EE92AUMN",
"git.build.time" : "2021-03-09T17:49:26+0800",
"git.build.user.email" : "xxx@163.com",
"git.build.user.name" : "zhaojiahao",
"git.build.version" : "0.0.1-SNAPSHOT",
"git.closest.tag.commit.count" : "5",
"git.closest.tag.name" : "V100.0.2",
"git.commit.id" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"git.commit.id.abbrev" : "xxxxxxx",
"git.commit.id.describe" : "V100.0.2-5-g7cd16cc-dirty",
"git.commit.id.describe-short" : "V100.0.2-5-dirty",
"git.commit.message.full" : "fix: 提交信息\\n\\nReviewers: xxx\\n\\nReviewed By: xxx\\n\\n",
"git.commit.message.short" : "fix: 提交信息",
"git.commit.time" : "2021-03-09T16:24:14+0800",
"git.commit.user.email" : "xxx@163.com",
"git.commit.user.name" : "zhaojiahao",
"git.dirty" : "true",
"git.remote.origin.url" : "git地址",
"git.tags" : "V100.0.2"
———— What is worth doing is worth doing well.
以上是关于Spring集成获取git版本号 ~~的主要内容,如果未能解决你的问题,请参考以下文章