Skip to content

Commit

Permalink
Merge pull request didi#494 from didi/dev
Browse files Browse the repository at this point in the history
1、打包时自动生成版本信息及git提交信息;2、优化swagger对版本信息的获取;
  • Loading branch information
ZQKC authored Jun 23, 2022
2 parents 5816429 + 7074bda commit 8478fb8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.xiaojukeji.kafka.manager.common.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class GitPropUtil {
private static final Logger log = LoggerFactory.getLogger(GitPropUtil.class);

private static Properties props = null;

public static final String VERSION_FIELD_NAME = "git.build.version";

public static final String COMMIT_ID_FIELD_NAME = "git.commit.id.abbrev";

public static String getProps(String fieldName) {
if (props == null) {
props = JsonUtils.stringToObj(readGitPropertiesInJarFile(), Properties.class);
}

return props.getProperty(fieldName);
}

public static Properties getProps() {
if (props == null) {
props = JsonUtils.stringToObj(readGitPropertiesInJarFile(), Properties.class);
}

return props;
}

private static String readGitPropertiesInJarFile() {
InputStream inputStream = null;
try {
inputStream = GitPropUtil.class.getClassLoader().getResourceAsStream("git.properties");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;

StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} catch (Exception e) {
log.error("method=readGitPropertiesInJarFile||errMsg=exception.", e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
log.error("method=readGitPropertiesInJarFile||msg=close failed||errMsg=exception.", e);
}
}

return "{}";
}

private GitPropUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ private ConfigUtils() {

@Value(value = "${spring.profiles.active:dev}")
private String kafkaManagerEnv;

@Value(value = "${spring.application.version:unknown}")
private String applicationVersion;
}
19 changes: 18 additions & 1 deletion kafka-manager-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
<build>
<finalName>kafka-manager</finalName>
<plugins>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand All @@ -126,7 +125,25 @@
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<format>json</format>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xiaojukeji.kafka.manager.web.config;

import com.xiaojukeji.kafka.manager.service.utils.ConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.xiaojukeji.kafka.manager.common.utils.GitPropUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
Expand All @@ -22,9 +21,6 @@
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Autowired
private ConfigUtils configUtils;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
Expand All @@ -43,10 +39,13 @@ public Docket createRestApi() {
}

private ApiInfo apiInfo() {
String version = GitPropUtil.getProps(GitPropUtil.VERSION_FIELD_NAME);
String commitId = GitPropUtil.getProps(GitPropUtil.COMMIT_ID_FIELD_NAME);

return new ApiInfoBuilder()
.title("LogiKM接口文档")
.description("欢迎使用滴滴LogiKM")
.version(configUtils.getApplicationVersion())
.version(String.format("%s-%s", version == null? "": version, commitId == null? "": commitId))
.build();
}

Expand Down

0 comments on commit 8478fb8

Please sign in to comment.