Present Git commit ID/SHA in a spring-boot application using gradle?

Viewed 1347

Based on this guide:

https://blog.mrhaki.com/2016/12/spring-sweets-add-git-info-to-info.html

I am trying to implement an endpoint that prints git information (SHA, branch, author, etc.). The above guide mentions that you get this endpoint: /info when using:

https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html

But since I would like to keep my example as simple as possible I was wondering if it was possible to get something similar but without using the actuator project but still use:

https://plugins.gradle.org/plugin/com.gorylenko.gradle-git-properties

I have this in my build.gradle file:

plugins {
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "com.gorylenko.gradle-git-properties" version "2.3.1"    
}

group = projectGroup
version = projectVersion
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

And my Spring boot application:

package helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
public class HelloApplication {

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

}

With this custom /info controller/endpoint:

package helloworld;

import org.springframework.core.io.ClassPathResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@RestController
public class InfoController {

    @GetMapping("/info")
    public ResponseEntity<String> info() throws IOException {

        File resource = new ClassPathResource("git.properties").getFile();
        String gitInfo = new String(Files.readAllBytes(resource.toPath()));

        return ResponseEntity.ok(gitInfo);
    }
}

And it gets the job done. But was wondering if I could do better/more spring-boot standard?

1 Answers

According to this:

A GitProperties bean is auto-configured if a git.properties file is available at the root of the classpath. See "Generate git information" for more details.

It follows explaining what is exposed.

If you need extra properties, you could do in gradle:

gitProperties {
    extProperty = 'gitProps' // git properties will be put in a map at project.ext.gitProps
    customProperty 'git.build.version', { project.parent.version }
    dotGitDirectory = "${project.rootDir}"
}

I also add them in manifest usually:

bootJar {
    mainClassName = 'com.App'
    archiveFileName = 'app.jar'
    manifest {
        attributes(
                'Build-Revision': "${-> project.ext.gitProps['git.commit.id.abbrev']}"  // Use GString lazy evaluation to delay until git properties are populated
        )
    }
 }

I once wrote a plugin that will auto-configure some of the things, including this functionality.

Related