Springboot Actuator/info ノードに Maven、git、およびビルド関連情報を表示する方法

目次

1. Spring-Boot プロジェクトを準備し、Actuator/info ノードを公開します

2. Maven 関連の値を表示する

3. git 関連の値を表示する

4. ビルド情報を追加する


1. Spring-Boot プロジェクトを準備し、Actuator/info ノードを公開します

pom.xml


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

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

Javaコード

@SpringBootApplication()
public class TestApp {

    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

アプリケーションのプロパティ

management.info.env.enabled=true

2. Maven 関連の値を表示する

application.properties ファイルに、次のキーと値の属性を追加します。

[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
management.info.env.enabled=true

スターター親を使用していない場合は、pom.xml の <build/> 要素に次の要素を含める必要があります。

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

Maven clean apply -DskipTests コマンドを実行すると、application.properties で定義された Maven 関連のプレースホルダーが置き換えられていることがわかります。

結果は次のとおりです。

info.app.name=test_project
info.app.description=Parent pom providing dependency and plugin management for applications built with Maven
info.app.version=1.0-SNAPSHOT
info.app.encoding=UTF-8
info.app.java.source=1.8.0_321
info.app.java.target=1.8.0_321

spring-boot プロジェクトを開始し、actuator/info ノードにアクセスすると、次の情報が表示されます。

{
  "app" : {
    "name" : "test_project",
    "description" : "Parent pom providing dependency and plugin management for applications built with Maven",
    "version" : "1.0-SNAPSHOT",
    "encoding" : "UTF-8",
    "java" : {
      "source" : "1.8.0_321",
      "target" : "1.8.0_321"
    }
  },
}

3. git 関連の値を表示する

            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.1.4</version>
            </plugin>

Maven clean install-DskipTests を実行すると、classes ディレクトリの下に git.properties ファイルが生成されることがわかります。

git.branch=dev
git.commit.id=xxx
git.commit.user.name=xxx
......

spring-boot プロジェクトを開始し、actuator/info ノードにアクセスすると、次の情報が表示されます。

{
  "git" : {
    "commit" : {
      "time" : "2023-04-06T12:16:17Z",
      "id" : "e3dd1233"
    },
    "branch" : "dev"
  }
}

Maven と Git の情報を一緒に表示する

{
  "app" : {
    "name" : "test_project",
    "description" : "Parent pom providing dependency and plugin management for applications built with Maven",
    "version" : "1.0-SNAPSHOT",
    "encoding" : "UTF-8",
    "java" : {
      "source" : "1.8.0_321",
      "target" : "1.8.0_321"
    }
  },
  "git" : {
    "commit" : {
      "time" : "2023-04-06T12:16:17Z",
      "id" : "e3dd1233"
    },
    "branch" : "dev"
  }
}

4. ビルド情報を追加する

有用なビルド情報を追加すると、ビルド アーティファクトの名前、バージョン、作成時間などをすばやく特定するのに役立ちます。チームがアプリケーションの関連バージョンをデプロイしたかどうかを確認するのに便利です。Spring Boot では、Maven または Gradle ビルド プラグインを使用してこれを簡単に追加できます。

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>2.6.4</version>
  <executions>
    <execution>
      <goals>
        <goal>build-info</goal>
      </goals>
    </execution>
  </executions>
</plugin>

コードをコンパイルすると、target/classes/META-INF/build-info.properties ファイルが生成されます。

内容は以下の通りです

build.artifact=spring-boot-build-info
build.group=io.reflectoring
build.name=spring-boot-build-info
build.time=2022-03-06T05\:53\:45.236Z
build.version=0.0.1-SNAPSHOT

addedProperties 属性を使用して、カスタム プロパティをこのリストに追加することもできます。

<execution>
  <goals>
    <goal>build-info</goal>
  </goals>
  <configuration>
    <additionalProperties>
      <custom.key1>value1</custom.key1>
      <custom.key2>value2</custom.key2>
    </additionalProperties>
  </configuration>
</execution>

ここでアプリケーションを実行し、ブラウザで http://localhost:8080/actuator/info エンドポイントを開くと、次のような応答が表示されます。

{
  "build": {
    "custom": {
      "key2": "value2",
      "key1": "value1"
    },
    "version": "0.0.1-SNAPSHOT",
    "artifact": "spring-boot-build-info",
    "name": "spring-boot-build-info",
    "time": "2022-03-06T06:34:30.306Z",
    "group": "io.reflectoring"
  }
}

プロパティを除外する場合は、excludeInfoProperties を使用して構成できます。アーティファクトのプロパティを除外する方法を見てみましょう。

<configuration>
  <excludeInfoProperties>
    <infoProperty>artifact</infoProperty>
  </excludeInfoProperties>
</configuration>

 詳細については、Spring Boot の公式 ドキュメントを参照してください。

SpringBoot の関連ソース コード クラス

org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration

Spring Boot Actuator を使用して役立つ情報エンドポイントを公開する

おすすめ

転載: blog.csdn.net/keeppractice/article/details/130067119