SpringBoot增加git和build信息

  1. 增加插件采集信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
</configuration>
</plugin>

通过 mvn clean package 即可完成采集

  1. 如果 actuator 需要暴露相关信息, 配置文件如下
1
2
management.info.git.mode=full
management.info.env.enabled=true
  1. 增加巡检接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@RestController
public class VersionController {

@Autowired(required = false)
private BuildProperties buildProperties;

@Autowired(required = false)
private GitProperties gitProperties;

private LocalDateTime deployTime = null;

@PostConstruct
public void deployTime() {
deployTime = LocalDateTime.now();
}

@GetMapping("/version")
public Object v() {
Map<String, Object> info = new HashMap<>();
// 启动时间
info.put("startTime", deployTime);
info.put("ip", NetUtils.getLocalHost());

// 构建信息
if (buildProperties != null) {
info.put("version", buildProperties.getVersion());
info.put("buildTime", buildProperties.getTime());
}

// Git 信息
if (gitProperties != null) {
info.put("branch", gitProperties.getBranch());
info.put("commitId", gitProperties.getCommitId());
info.put("commitMessage", gitProperties.get("commit.message.full"));
info.put("commitTime", gitProperties.getCommitTime());
}

return info;
}

}