Spring actuator + maven git plugin 配置/info 接口获取git信息

需求

通过 http://domain:port/info 接口, 获取 git 的信息, 比如分支,提交时间,提交信息等等。如下图:

代码

maven 依赖:

        <!-- 这是 spring 的actuator组件,提供了/info接口 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 这是maven git 插件依赖 -->
        <dependency>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>4.0.2</version>
        </dependency>

然后添加 maven 的 plugin

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <configuration>
                    <failOnNoGitDirectory>false</failOnNoGitDirectory>
                    <verbose>true</verbose>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                </configuration>
            </plugin>

就行啦!

额外配置

如果想让某个环境,比如prod不能用这个功能, 需要在 application-prod.yml 中这样配置

management:
  health:
    defaults:
      enabled: false
  info:
    defaults:
      enabled: false

此外,默认情况只会显示git 的commitid, commit时间,分支信息。如果想要更多信息需要自行配置,都在management下,比如全要:

management:
  info:
    git:
      mode: full

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Scroll to Top