Spring Boot项目获取Git版本信息

背景:生产环境中为了验证服务更新是否成功,需要知道当前对应的git仓库信息。

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.3.RELEASE</version>
	<relativePath /> <!-- lookup parent from repository -->
</parent>

spring boot父项目的PluginManagement中已经包含git-commit-id-plugin这个插件,这个插件的作用是在指定阶段(goal)读取git仓库信息并在对应目录生成git.properties文件。

               <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>

由于该插件是非强制继承的,我们需要在自己项目pom中显式地继承此插件,但不需要再做其他版本之类的配置。

    <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>pl.project13.maven</groupId>
				<artifactId>git-commit-id-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

该插件生成的git.properties文件内容如下:

git.build.user.email=xxx@xxx.com
git.build.host=xxx.local
git.dirty=true
git.remote.origin.url=git@xxxxxx
git.closest.tag.name=v3.17.0
git.commit.id.describe-short=v3.17.0-44-dirty
git.commit.user.email=xxx@xxx.com
git.commit.time=2019-04-08T10\:23\:34+0800
git.commit.message.full=xxx
git.build.version=3.16.2
git.commit.message.short=一次提交
git.commit.id.abbrev=3570d29
git.branch=master
git.build.user.name=king
git.closest.tag.commit.count=44
git.commit.id.describe=v3.17.0-44-g3570d29-dirty
git.commit.id=3570d291b18e8d10b833fbbe0c6cc1c32c771b24
git.tags=
git.build.time=2019-04-08T10\:30\:15+0800
git.commit.user.name=king

Properties文件生成了,想怎么读取都可以自由选择,之前作者的想法是使用这个插件把git.properties生成到resources目录下,然后通过@ConfigurationProperties和@PropertySource将配置文件映射为java对象,后来发现原来SpringBoot自己已经为我们提供了一个GitProperties类,并且已经在启动时生成并加入到容器管理中,我们只需使用即可。

通过Spring Boot Admin的Beans管理来观察下这个GitProperties。>如何搭建

在Spring Boot应用中,只需要通过注解注入就可以直接使用了。 

@Autowired
private GitProperties git;

该类仅提供了4个属性如下,不过也够用了。 

{
    branch: "master",
    commitId: "00df6fad7ef69954b35037c549c4b93956fda4e8",
    shortCommitId: "00df6fa",
    commitTime: "2019-04-09T03:04:24Z"
}

呃...上面写这么多,其实自己可以不写一行代码,spring boot自身已经把此功能埋进去了,我们只需要把maven插件引用进来,就可以通过actuator(/actuator/info接口)来暴露当前git信息,结构如下:

{
    git: {
        commit: {
            time: "2019-04-09T09:15:29Z",
            id: "41d7943"
        },
        branch: "origin/tags/v0.0.1"
    }
}

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值