Mit Spring Boot können bequem Build und Git Informationen erzeugt und ausgelesen werden. Damit die Informationen erzeugt werden, muss die Konfiguration zunächst entsprechend angepasst werden.

Maven

Zum Erzeugen der Build Informationen muss das Spring Boot Maven Plugin wie folgt konfiguriert werden:

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

Zum Erzeugen von Git Informationen muss das git-commit-id-plugin eingebunden werden:

<build>
    <plugins>
        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Gradle

Bei der Verwendung von Gradle als Build Werkzeug, muss das Spring Boot Plugin wie folgt konfiguriert werden, um Build Informationen zu erzeugen:

springBoot  {
    buildInfo()
}

Zur Erzeugung von Git Informationen muss das gradle-git-properties Plugin eingebunden werden:

plugins {
    id "com.gorylenko.gradle-git-properties" version "1.4.17"
}

Informationen auslesen

Sofern Build oder Git Informationen vorliegen, stellt Spring Boot automatisch entsprechende Beans zum Auslesen der Informationen bereit. Die Beans können wie folgt verwendet werden:

package projectinfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;

@SpringBootApplication
public class Application implements CommandLineRunner {

    private final BuildProperties buildProperties;
    private final GitProperties gitProperties;

    @Autowired
    public Application(final BuildProperties buildProperties, final GitProperties gitProperties) {
        this.buildProperties = buildProperties;
        this.gitProperties = gitProperties;
    }

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

    @Override
    public void run(final String... args) throws Exception {
        this.buildProperties.forEach(entry -> System.out.printf("%s -> %s\n", entry.getKey(), entry.getValue()));
        this.gitProperties.forEach(entry -> System.out.printf("%s -> %s\n", entry.getKey(), entry.getValue()));
    }

}

Quellen