Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 965
u123 Avatar asked Apr 29 '26 18:04

u123


2 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.

like image 185
Eugene Avatar answered May 01 '26 07:05

Eugene


As the other answer correctly pointed out, Spring Boot auto-configures a GitProperties bean.

What's not mentioned is, that you can inject that bean in your controller, instead of reading the properties file from disk.

Here's an example with injected BuildProperties and GitProperties:

@RestController
public class VersionController {

    private final BuildProperties buildProperties;
    private final GitProperties gitProperties;

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

    @GetMapping("/version")
    public ResponseEntity<Map<String, String>> getVersion() {
        Map<String, String> response = new HashMap<>();
        response.put("version", buildProperties.getVersion());
        response.put("commitId", gitProperties.getCommitId());
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}
like image 34
Dario Seidl Avatar answered May 01 '26 08:05

Dario Seidl