基于本指南:
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()
}
还有我的 Spring 启动应用程序:
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);
}
}
使用此自定义/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);
}
}
它完成了工作。但是想知道我是否可以做得更好/更多弹簧靴标准?
根据这个:
如果类路径的根目录中有 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}"
}
我通常也会在清单中添加它们:
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
)
}
}
我曾经写过一个插件,可以自动配置一些东西,包括这个功能。