使用 gradle 在 spring-boot 应用程序中显示 Git 提交 ID/SHA?

u12*_*123 6 java git spring-boot

基于本指南:

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

我正在尝试实现一个打印 git 信息(SHA、分支、作者等)的端点。上面的指南提到你会得到这个端点:/info当使用:

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

但由于我想让我的示例尽可能简单,我想知道是否有可能获得类似的东西,但使用执行器项目,但仍然使用:

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

我的build.gradle文件中有这个:

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()
}
Run Code Online (Sandbox Code Playgroud)

和我的 Spring boot 应用程序:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

使用此自定义/info控制器/端点:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

它完成了工作。但想知道我是否可以做得更好/更多的 spring-boot 标准?

Eug*_*ene 3

根据这个

如果 git.properties 文件在类路径的根目录下可用,则 GitProperties bean 会自动配置。有关更多详细信息,请参阅“生成 git 信息”。

接下来解释暴露的内容。

如果您需要额外的属性,您可以在 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}"
}
Run Code Online (Sandbox Code Playgroud)

我通常也会将它们添加到清单中:

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
        )
    }
 }
Run Code Online (Sandbox Code Playgroud)

我曾经写过一个插件,可以自动配置一些东西,包括这个功能。