/actuator/info 返回代码版本号和时间戳

为了显示区分部署代码版本,一般会在打包的时候带上 git/hg 版本号。

配置之前,调用 /actuator/info 接口:

GET http://localhost:8080/actuator/info

HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Fri, 09 Apr 2021 06:34:06 GMT

{}

Git 配置

若代码发布在 Git 仓库,可以使用 maven 插件 git-commit-id-plugin。该插件会产生一个 git.properties 文件,并被包含进最终的 jar 文件中。

(1)在 pom.xml 中添加 git-commit-id-plugin maven 插件。


        
            ...
            
                pl.project13.maven
                git-commit-id-plugin
                3.0.1
            
        
    

(2)执行 mvn 编译命令。

meikai@mk MINGW64 /f/work/demo/codetest (master)
$ mvn clean compile -DskipTests
...
[INFO] --- git-commit-id-plugin:3.0.1:revision (default) @ codetest ---
[INFO] dotGitDirectory F:\work\demo\codetest\.git
[INFO] Collected git.build.user.name with value meikai
[INFO] Collected git.commit.id with value 4153cb8802a0e507acba9714f46737821a2de3e1
...
[INFO] Writing properties file to [F:\work\demo\codetest\target\classes\git.properties] (for module codetest)...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.578 s
[INFO] Finished at: 2021-04-09T14:44:25+08:00
[INFO] ------------------------------------------------------------------------

在 classpath 目录 F:\work\demo\codetest\target\classes\ 下,生成 git.properties 文件。

(3)重新启动项目,调用 /actuator/info

GET http://localhost:8080/actuator/info

HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Fri, 09 Apr 2021 06:49:34 GMT

{
  "git": {
    "commit": {
      "time": "2021-04-09T06:22:49Z",
      "id": "4153cb8"
    },
    "branch": "master"
  }
}

git.commit.id 和最新的 commit id 一致。

hg 配置

(1)在 pom.xml 中添加 buildnumber-maven-plugin maven 插件。

    
        
                ...
            
                org.codehaus.mojo
                buildnumber-maven-plugin
                1.4
                
                    
                        validate
                        
                            hgchangeset
                        
                    
                
            
        
    

(2)执行 mvn 编译命令。

meikai@mk MINGW64 /f/work/demo/hg-demo
$ mvn clean compile
....
[INFO] --- buildnumber-maven-plugin:1.4:hgchangeset (default) @ hg-demo ---
[INFO] Setting Mercurial Changeset: b66059fecf49
[INFO] Setting Mercurial Changeset Date: 2021-04-09 15:52 +0800
....
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.057 s
[INFO] Finished at: 2021-04-09T15:55:23+08:00
[INFO] ------------------------------------------------------------------------

buildnumber-maven-plugin:1.4:hgchangeset 会执行 org.codehaus.mojo.build.HgChangeSetMojo 类的 execute() 方法,其会往 MavenProject 中写入 changeSetchangeSetDate 两个 properties。

(3)生成 /META-INF/build-info.properties 文件。

pom.xml 中添加 spring-boot-maven-plugin 插件的 build-info goal,完整的配置如下所示:

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            build-info
                        
                        
                            
                                ${changeSet}
                                ${changeSetDate}
                            
                        
                    
                
            
            
                org.codehaus.mojo
                buildnumber-maven-plugin
                1.4
                
                    
                        validate
                        
                            hgchangeset
                        
                    
                
            
        
    

build-info 会执行 org.springframework.boot.maven.BuildInfoMojo 类的 execute() 方法,其会往 ${project.build.outputDirectory}/META-INF/build-info.properties 文件写入相应的配置。包含自定义的 配置。

执行 mvn 编译命令:

meikai@mk MINGW64 /f/work/demo/hg-demo
$ mvn clean compile
...
[INFO] --- buildnumber-maven-plugin:1.4:hgchangeset (default) @ hg-demo ---
[INFO] Setting Mercurial Changeset: a6256866c372+
[INFO] Setting Mercurial Changeset Date: 2021-04-09 16:12 +0800
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.4:build-info (default) @ hg-demo ---
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.635 s
[INFO] Finished at: 2021-04-09T16:32:55+08:00
[INFO] ------------------------------------------------------------------------

spring-boot-maven-plugin:2.4.4:build-info goal 会把 buildnumber-maven-plugin:1.4:hgchangeset goal 生成的代码版本信息写入到 /META-INF/build-info.properties 文件中。

(4)重新启动项目,调用 /actuator/info

GET http://localhost:8080/actuator/info

HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Fri, 09 Apr 2021 08:37:21 GMT
Keep-Alive: timeout=60
Connection: keep-alive

{
  "build": {
    "changeset": {
      "date": "2021-04-09 16:12 +0800",
      "id": "a6256866c372+"
    },
    "version": "0.0.1-SNAPSHOT",
    "artifact": "hg-demo",
    "name": "hg-demo",
    "time": "2021-04-09T08:36:43.388Z",
    "group": "com.example"
  }
}

build.changeset.id 和最新的 commit id 一致。

$ hg id -i
a6256866c372+

参考

  • maven打包把git版本号打包进去
  • Maven打包使用代码版本号和时间戳
  • https://www.mojohaus.org/buildnumber-maven-plugin/plugin-info.html
  • SpringBoot项目——打印项目构建信息
  • https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-build-info
  • https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/maven-plugin/index.html

你可能感兴趣的:(/actuator/info 返回代码版本号和时间戳)