Spring Boot教程(十四)Spring Boot中使用Actuator的/info端點輸出Git版本信息

POM配置

首先,咱們能夠挑選任意一個Spring Boot項目,修改它的pom.xmlhtml

  • 引入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>2.1.15</version>
        <executions>
            <execution>
                <goals>
                    <goal>revision</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        </configuration>
    </plugin>

    產生git版本信息

  • 在完成了上面的配置以後,執行git-commit-id-plugin插件

    運行完成後,咱們能夠在控臺中看到相似下面的信息:git

    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - dotGitDirectory E:\git_project\oschina\SpringBoot-Learning\.git
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.build.user.name didi
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.build.user.email dyc87112@qq.com
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.branch master
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - --always = true
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - --dirty = -dirty
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - --abbrev = 7
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - Tag refs [ [Ref[refs/tags/chapter1=ec8713f61cd49569886708a08adea02c8ef0a112]] ]
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - Created map: [ {} ] 
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - HEAD is [ e0540b3524378de9b5d938668a0f75ec016fa5e5 ] 
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - Repo is in dirty state [ true ]
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.commit.id.describe e0540b3-dirty
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.commit.id e0540b3524378de9b5d938668a0f75ec016fa5e5
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.commit.id.abbrev e0540b3
    [INFO] pl.project13.maven.git.log.MavenLoggerBridge - git.dirty true
    ...

    同時,在target/classes目錄下,咱們能夠發現產生了一個git.properties配置信息:spring

    這個文件就是當前項目的git信息,它的內容以下:curl

    #Generated by Git-Commit-Id-Plugin
    #Thu Jun 01 17:57:53 CST 2017
    git.build.user.email=dyc87112@qq.com
    git.build.host=Lenovo-zhaiyc
    git.dirty=true
    git.remote.origin.url=https\://git.oschina.net/didispace/SpringBoot-Learning.git
    git.closest.tag.name=chapter1
    git.commit.id.describe-short=e0540b3-dirty
    git.commit.user.email=dyc87112@qq.com
    git.commit.time=2017-06-01T17\:57\:10+0800
    git.commit.message.full=update
    git.build.version=1.0.0
    git.commit.message.short=update
    git.commit.id.abbrev=e0540b3
    git.branch=master
    git.build.user.name=didi
    git.closest.tag.commit.count=240
    git.commit.id.describe=e0540b3-dirty
    git.commit.id=e0540b3524378de9b5d938668a0f75ec016fa5e5
    git.tags=
    git.build.time=2017-06-01T17\:57\:53+0800
    git.commit.user.name=didi

    啓動測試

    完成了上述配置以後,啓動應用並訪問端點,好比:curl localhost:8080/info,咱們能夠得到以下輸出:maven

    {
      "git": {
        "commit": {
          "time": 1496311030000,
          "id": "e0540b3"
        },
        "branch": "master"
      }
    }

    其中包含了關於branch和commit的基礎信息。而這個信息格式是最簡模式,咱們也能夠經過配置下面的參數來獲取更全面的git信息:spring-boot

    management.info.git.mode=full

    重啓應用後再訪問/info端點,能夠得到相似下面更爲詳細的版本信息了。測試

    {
      "git": {
        "build": {
          "host": "Lenovo-zhaiyc",
          "version": "1.0.0",
          "time": 1496311073000,
          "user": {
            "name": "didi",
            "email": "dyc87112@qq.com"
          }
        },
        "branch": "master",
        "commit": {
          "message": {
            "short": "update",
            "full": "update"
          },
          "id": "e0540b3524378de9b5d938668a0f75ec016fa5e5",
          "id.describe-short": "e0540b3-dirty",
          "id.abbrev": "e0540b3",
          "id.describe": "e0540b3-dirty",
          "time": 1496311030000,
          "user": {
            "email": "dyc87112@qq.com",
            "name": "didi"
          }
        },
        "closest": {
          "tag": {
            "name": "chapter1",
            "commit": {
              "count": "240"
            }
          }
        },
        "dirty": "true",
        "remote": {
          "origin": {
            "url": "https://git.oschina.net/didispace/SpringBoot-Learning.git"
          }
        },
        "tags": ""
      }
    }

    源碼來源ui

相關文章
相關標籤/搜索