你的浏览器不支持canvas

牛马的一生不该是十四亿的重复,而应该是各有各的精彩。

Spring Boot中使用Actuator的/info端点输出Git版本信息

Date: Author: codexu

本文章采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可。

Spring Boot中使用Actuator的/info端点输出Git版本信息

POM配置

首先,我们可以挑选任意一个Spring Boot项目,修改它的pom.xml:
引入 spring-boot-starter-actuator,提供/info端点

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

添加git-commit-id-plugin 插件,该插件用来产生git的版本信息:

<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>4.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
    </configuration>
</plugin>

产生git版本信息

  • 在完成了上面的配置之后,执行git-commit-id-plugin插件
    运行完成后,我们可以在控台中看到类似下面的信息:
    [INFO] Writing properties file to [D:\+work\SpringBootScaffold\target\classes\git.properties] (for module springbootscaffold)...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  3.814 s
    [INFO] Finished at: 2020-02-21T15:40:19+08:00
    [INFO] ------------------------------------------------------------------------
    

    同时,在target/classes目录下,我们可以发现产生了一个git.properties配置信息:

    这个文件就是当前项目的git信息,它的内容如下:

    #Generated by Git-Commit-Id-Plugin
    #Fri Feb 21 15:40:19 CST 2020
    git.branch=master
    git.build.host=DESKTOP-1B1K4KD
    git.build.time=2020-02-21T15\:40\:19+0800
    git.build.user.email=codexu@foxmail.com
    git.build.user.name=xuyh
    git.build.version=0.0.1-SNAPSHOT
    git.closest.tag.commit.count=
    git.closest.tag.name=
    git.commit.id=ffe176447602c9c3dcaa8a876cbefb3d1cd07918
    git.commit.id.abbrev=ffe1764
    git.commit.id.describe=ffe1764-dirty
    git.commit.id.describe-short=ffe1764-dirty
    git.commit.message.full=Spring Boot Actuator
    git.commit.message.short=Spring Boot Actuator
    git.commit.time=2020-02-16T16\:40\:28+0800
    git.commit.user.email=codexu@foxmail.com
    git.commit.user.name=xuyh
    git.dirty=true
    git.local.branch.ahead=0
    git.local.branch.behind=0
    git.remote.origin.url=https\://github.com/wintry/SpringBootScaffold.git
    git.tags=
    git.total.commit.count=6
    

启动测试

完成了上述配置之后,启动应用并访问端点,比如:curl localhost:8080/info,我们可以获得如下输出:

{
    "app":{
        "name":"谋杀复读机的测试"},
    "git":{
        "commit":{
            "time":1581842428000,
            "id":"ffe1764"
            },
            "branch":"master"
            }
}

其中包含了关于branch和commit的基础信息。而这个信息格式是最简模式,我们也可以通过配置下面的参数来获取更全面的git信息:

management.info.git.mode=full

重启应用后再访问/info端点,可以获得类似下面更为详细的版本信息了。

{
    "app": {
        "name": "谋杀复读机的测试"
    },
    "git": {
        "build": {
            "host": "DESKTOP-1B1K4KD",
            "version": "0.0.1-SNAPSHOT",
            "time": 1582270819000,
            "user": {
                "name": "xuyh",
                "email": "codexu@foxmail.com"
            }
        },
        "branch": "master",
        "commit": {
            "message": {
                "short": "Spring Boot Actuator",
                "full": "Spring Boot Actuator"
            },
            "id": "ffe176447602c9c3dcaa8a876cbefb3d1cd07918",
            "id.describe-short": "ffe1764-dirty",
            "id.abbrev": "ffe1764",
            "id.describe": "ffe1764-dirty",
            "time": 1581842428000,
            "user": {
                "email": "codexu@foxmail.com",
                "name": "xuyh"
            }
        },
        "closest": {
            "tag": {
                "name": "",
                "commit": {
                    "count": ""
                }
            }
        },
        "local": {
            "branch": {
                "ahead": "0",
                "behind": "0"
            }
        },
        "dirty": "true",
        "remote": {
            "origin": {
                "url": "https://github.com/wintry/SpringBootScaffold.git"
            }
        },
        "tags": "",
        "total": {
            "commit": {
                "count": "6"
            }
        }
    }
}

对于本文内容有问题或建议的小伙伴,欢迎在文章底部留言交流讨论。