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?