Skip to content

Commit 7074bda

Browse files
author
zengqiao
committed
1、打包时自动生成版本信息及git提交信息;2、优化swagger对版本信息的获取
1 parent bd62212 commit 7074bda

File tree

4 files changed

+90
-10
lines changed

4 files changed

+90
-10
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.xiaojukeji.kafka.manager.common.utils;
2+
3+
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.BufferedReader;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.util.Properties;
11+
12+
public class GitPropUtil {
13+
private static final Logger log = LoggerFactory.getLogger(GitPropUtil.class);
14+
15+
private static Properties props = null;
16+
17+
public static final String VERSION_FIELD_NAME = "git.build.version";
18+
19+
public static final String COMMIT_ID_FIELD_NAME = "git.commit.id.abbrev";
20+
21+
public static String getProps(String fieldName) {
22+
if (props == null) {
23+
props = JsonUtils.stringToObj(readGitPropertiesInJarFile(), Properties.class);
24+
}
25+
26+
return props.getProperty(fieldName);
27+
}
28+
29+
public static Properties getProps() {
30+
if (props == null) {
31+
props = JsonUtils.stringToObj(readGitPropertiesInJarFile(), Properties.class);
32+
}
33+
34+
return props;
35+
}
36+
37+
private static String readGitPropertiesInJarFile() {
38+
InputStream inputStream = null;
39+
try {
40+
inputStream = GitPropUtil.class.getClassLoader().getResourceAsStream("git.properties");
41+
42+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
43+
String line = null;
44+
45+
StringBuilder sb = new StringBuilder();
46+
while ((line = bufferedReader.readLine()) != null) {
47+
sb.append(line).append("\n");
48+
}
49+
return sb.toString();
50+
} catch (Exception e) {
51+
log.error("method=readGitPropertiesInJarFile||errMsg=exception.", e);
52+
} finally {
53+
try {
54+
if (inputStream != null) {
55+
inputStream.close();
56+
}
57+
} catch (Exception e) {
58+
log.error("method=readGitPropertiesInJarFile||msg=close failed||errMsg=exception.", e);
59+
}
60+
}
61+
62+
return "{}";
63+
}
64+
65+
private GitPropUtil() {
66+
}
67+
}

kafka-manager-core/src/main/java/com/xiaojukeji/kafka/manager/service/utils/ConfigUtils.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,4 @@ private ConfigUtils() {
2020

2121
@Value(value = "${spring.profiles.active:dev}")
2222
private String kafkaManagerEnv;
23-
24-
@Value(value = "${spring.application.version:unknown}")
25-
private String applicationVersion;
2623
}

kafka-manager-web/pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
<build>
115115
<finalName>kafka-manager</finalName>
116116
<plugins>
117-
118117
<plugin>
119118
<groupId>org.springframework.boot</groupId>
120119
<artifactId>spring-boot-maven-plugin</artifactId>
@@ -126,7 +125,25 @@
126125
</goals>
127126
</execution>
128127
</executions>
128+
</plugin>
129129

130+
<plugin>
131+
<groupId>pl.project13.maven</groupId>
132+
<artifactId>git-commit-id-plugin</artifactId>
133+
<executions>
134+
<execution>
135+
<goals>
136+
<goal>revision</goal>
137+
</goals>
138+
</execution>
139+
</executions>
140+
<configuration>
141+
<verbose>true</verbose>
142+
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
143+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
144+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
145+
<format>json</format>
146+
</configuration>
130147
</plugin>
131148
</plugins>
132149
</build>

kafka-manager-web/src/main/java/com/xiaojukeji/kafka/manager/web/config/SwaggerConfig.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.xiaojukeji.kafka.manager.web.config;
22

3-
import com.xiaojukeji.kafka.manager.service.utils.ConfigUtils;
4-
import org.springframework.beans.factory.annotation.Autowired;
3+
import com.xiaojukeji.kafka.manager.common.utils.GitPropUtil;
54
import org.springframework.context.annotation.Bean;
65
import org.springframework.context.annotation.Configuration;
76
import org.springframework.web.servlet.config.annotation.*;
@@ -22,9 +21,6 @@
2221
@EnableWebMvc
2322
@EnableSwagger2
2423
public class SwaggerConfig implements WebMvcConfigurer {
25-
@Autowired
26-
private ConfigUtils configUtils;
27-
2824
@Override
2925
public void addResourceHandlers(ResourceHandlerRegistry registry) {
3026
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
@@ -43,10 +39,13 @@ public Docket createRestApi() {
4339
}
4440

4541
private ApiInfo apiInfo() {
42+
String version = GitPropUtil.getProps(GitPropUtil.VERSION_FIELD_NAME);
43+
String commitId = GitPropUtil.getProps(GitPropUtil.COMMIT_ID_FIELD_NAME);
44+
4645
return new ApiInfoBuilder()
4746
.title("LogiKM接口文档")
4847
.description("欢迎使用滴滴LogiKM")
49-
.version(configUtils.getApplicationVersion())
48+
.version(String.format("%s-%s", version == null? "": version, commitId == null? "": commitId))
5049
.build();
5150
}
5251

0 commit comments

Comments
 (0)