前言
當(dāng)我們想知道部署項目的哪個版本有問題?當(dāng)我們想知道線上運行的版本是否是我們預(yù)期的版本脐瑰?當(dāng)我們想把部署的版本與代碼進行關(guān)聯(lián)?如果是你用git來做版本管理,那就可以使用git-commit-id-maven-plugin插件來實現(xiàn)上述功能丸相。
git-commit-id-maven-plugin插件,會根據(jù)當(dāng)前分支的版本號生成一個git.properties文件朋鞍。git.properties內(nèi)容形如下
git.branch=master
git.build.host=xxx
git.build.time=2022-03-01T20\:33\:43+0800
git.build.user.email=aaa@qq.com
git.build.user.name=aaa
git.build.version=1.0-SNAPSHOT
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=6dab4430864e3e4a9fc1ba6f6b93f278100d4e2e
git.commit.id.abbrev=6dab443
git.commit.id.describe=6dab443-dirty
git.commit.id.describe-short=6dab443-dirty
git.commit.message.full=Add README.md
git.commit.message.short=Add README.md
git.commit.time=2022-03-01T16\:24\:48+0800
git.commit.user.email=aa@example
git.commit.user.name=aa
git.dirty=true
git.remote.origin.url=http://hello
git.tags=
git.total.commit.count=1
如何使用
本文以springboot項目為例已添,springboot項目的parent pom里面已經(jīng)內(nèi)嵌git-commit-id-maven-plugin插件管理依賴妥箕。如下
<pluginManagement>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
</configuration>
</plugin>
</plugins>
</pluginManagement>
因此我們的項目中可以做如下配置
1、在我們的項目中顯式引入git-commit-id-plugin插件
<build>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
</plugins>
</build>
2更舞、通過和actuator集成畦幢,顯示git信息
a、在項目中引入actuator GAV
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
b缆蝉、瀏覽器訪問http://ip : port/actuator/info
如果覺得上面的信息不夠多宇葱,我們可以通過自定義端點或者自己寫一個controller把信息展示出來
詳細的信息可以通過org.springframework.boot.info.GitProperties展示
本示例以自定義端點為例。示例如下
@Endpoint(id = "git")
@Component
public class GitEndpoint {
@Autowired(required = false)
private GitProperties gitProperties;
@ReadOperation
public Object info() throws IOException {
if(ObjectUtils.isEmpty(gitProperties)){
return new HashMap<>();
}
return gitProperties;
}
}
在application.yml中開放自定義端點
management:
endpoints:
web:
exposure:
include: 'git'
瀏覽器訪問http://ip : port/actuator/git
如果仍然覺得上述的信息還是不夠詳細刊头,那可以通過解析git.properties文件顯示黍瞧。示例
@Endpoint(id = "gitDetail")
@Slf4j
@Component
public class GitDetailEndPoint {
@ReadOperation
public Object detail() throws IOException {
Properties props = null;
try {
props = PropertiesLoaderUtils.loadAllProperties("git.properties");
return props;
} catch (IOException e) {
log.error("git.properties not found");
} finally {
}
return new HashMap<>();
}
}
在application.yml中開放自定義端點
management:
endpoints:
web:
exposure:
include: 'gitDetail'
瀏覽器訪問http://ip:port/actuator/gitDetail
總結(jié)
git-commit-id-maven-plugin在分布式或者微服務(wù)項目中,用來驗證項目版本還是挺有用的原杂,推薦大家有機會用一下
demo鏈接
https://github.com/lyb-geek/springboot-learning/tree/master/springboot-git-commit