<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

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

    運行完成后,我們可以在控臺中看到類似下面的信息:


    [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配置信息:

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


    #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,我們可以獲得如下輸出:


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

    其中包含了關于branch和commit的基礎信息。而這個信息格式是最簡模式,我們也可以通過配置下面的參數來獲取更全面的git信息:


    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": ""
      }
    }
    
    
    
    
    
    
    

    #代碼示例

    本文的相關例子可以查看下面倉庫中的Chapter6-2-1目錄:


    請登錄后查看

    CRMEB 最后編輯于2025-01-22 17:28:00

    快捷回復
    回復
    回復
    回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
    排序 默認正序 回復倒序 點贊倒序

    {{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    1098
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服