From dbc525e8fb59f70c474863304e3c580e81dcedf0 Mon Sep 17 00:00:00 2001
From: 648540858 <18010473990@163.com>
Date: 星期一, 09 八月 2021 16:54:36 +0800
Subject: [PATCH] 添加版本信息接口

---
 src/main/java/com/genersoft/iot/vmp/utils/GitUtil.java                    |   44 +++++++
 src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java          |    2 
 src/main/java/com/genersoft/iot/vmp/conf/VersionInfo.java                 |   37 ++++++
 src/main/java/com/genersoft/iot/vmp/utils/JarFileUtils.java               |   73 ++++++++++++
 pom.xml                                                                   |    6 +
 src/main/java/com/genersoft/iot/vmp/common/VersionPo.java                 |  136 ++++++++++++++++++++++
 src/main/java/com/genersoft/iot/vmp/vmanager/server/ServerController.java |   16 ++
 src/main/java/com/genersoft/iot/vmp/conf/VersionConfig.java               |   37 ++++++
 8 files changed, 349 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 49fa3b3..5cd5f04 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,6 +13,7 @@
 	<artifactId>wvp-pro</artifactId>
 	<version>2.0</version>
 	<name>web video platform</name>
+	<description>鍥芥爣28181瑙嗛骞冲彴</description>
 
 	<repositories>
 		<repository>
@@ -251,6 +252,11 @@
 			</plugin>
 
 			<plugin>
+				<groupId>pl.project13.maven</groupId>
+				<artifactId>git-commit-id-plugin</artifactId>
+			</plugin>
+
+			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-surefire-plugin</artifactId>
 				<configuration>
diff --git a/src/main/java/com/genersoft/iot/vmp/common/VersionPo.java b/src/main/java/com/genersoft/iot/vmp/common/VersionPo.java
new file mode 100644
index 0000000..a7d8ce6
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/common/VersionPo.java
@@ -0,0 +1,136 @@
+package com.genersoft.iot.vmp.common;
+
+import com.alibaba.fastjson.annotation.JSONField;
+
+public class VersionPo {
+    /**
+     * git鐨勫叏鐗堟湰鍙�
+     */
+    @JSONField(name="GIT-Revision")
+    private String GIT_Revision;
+    /**
+     * maven鐗堟湰
+     */
+    @JSONField(name = "Create-By")
+    private String Create_By;
+    /**
+     * git鐨勫垎鏀�
+     */
+    @JSONField(name = "GIT-BRANCH")
+    private String GIT_BRANCH;
+    /**
+     * git鐨剈rl
+     */
+    @JSONField(name = "GIT-URL")
+    private String GIT_URL;
+    /**
+     * 鏋勫缓鏃ユ湡
+     */
+    @JSONField(name = "BUILD-DATE")
+    private String BUILD_DATE;
+    /**
+     * 椤圭洰鍚嶇О 閰嶅悎pom浣跨敤
+     */
+    @JSONField(name = "artifactId")
+    private String artifactId;
+    /**
+     * git灞�閮ㄧ増鏈彿
+     */
+    @JSONField(name = "GIT-Revision-SHORT")
+    private String GIT_Revision_SHORT;
+    /**
+     * 椤圭洰鐨勭増鏈2.0.1.0 閰嶅悎pom浣跨敤
+     */
+    @JSONField(name = "version")
+    private String version;
+    /**
+     * 瀛愮郴缁熷悕绉�
+     */
+    @JSONField(name = "project")
+    private String project;
+    /**
+     * jdk鐗堟湰
+     */
+    @JSONField(name="Build_Jdk")
+    private String Build_Jdk;
+
+    public void setGIT_Revision(String GIT_Revision) {
+        this.GIT_Revision = GIT_Revision;
+    }
+
+    public void setCreate_By(String create_By) {
+        Create_By = create_By;
+    }
+
+    public void setGIT_BRANCH(String GIT_BRANCH) {
+        this.GIT_BRANCH = GIT_BRANCH;
+    }
+
+    public void setGIT_URL(String GIT_URL) {
+        this.GIT_URL = GIT_URL;
+    }
+
+    public void setBUILD_DATE(String BUILD_DATE) {
+        this.BUILD_DATE = BUILD_DATE;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+
+    public void setGIT_Revision_SHORT(String GIT_Revision_SHORT) {
+        this.GIT_Revision_SHORT = GIT_Revision_SHORT;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public void setProject(String project) {
+        this.project = project;
+    }
+
+    public void setBuild_Jdk(String build_Jdk) {
+        Build_Jdk = build_Jdk;
+    }
+
+    public String getGIT_Revision() {
+        return GIT_Revision;
+    }
+
+    public String getCreate_By() {
+        return Create_By;
+    }
+
+    public String getGIT_BRANCH() {
+        return GIT_BRANCH;
+    }
+
+    public String getGIT_URL() {
+        return GIT_URL;
+    }
+
+    public String getBUILD_DATE() {
+        return BUILD_DATE;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public String getGIT_Revision_SHORT() {
+        return GIT_Revision_SHORT;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public String getProject() {
+        return project;
+    }
+
+    public String getBuild_Jdk() {
+        return Build_Jdk;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java
index ab327b0..907e30d 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java
@@ -141,8 +141,6 @@
         }
     }
 
-
-
     @Bean
     public ServletRegistrationBean recordServletRegistrationBean(){
         ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RecordProxySerlet(),"/record_proxy/*");
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/VersionConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/VersionConfig.java
new file mode 100644
index 0000000..a24ade4
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/conf/VersionConfig.java
@@ -0,0 +1,37 @@
+package com.genersoft.iot.vmp.conf;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "version")
+public class VersionConfig {
+
+    private String version;
+    private String artifactId;
+    private String description;
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getVersion() {
+        return version;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/VersionInfo.java b/src/main/java/com/genersoft/iot/vmp/conf/VersionInfo.java
new file mode 100644
index 0000000..4ff5eba
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/conf/VersionInfo.java
@@ -0,0 +1,37 @@
+package com.genersoft.iot.vmp.conf;
+
+import com.genersoft.iot.vmp.common.VersionPo;
+import com.genersoft.iot.vmp.utils.GitUtil;
+import com.genersoft.iot.vmp.utils.JarFileUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class VersionInfo {
+
+    @Autowired
+    VersionConfig config;
+    @Autowired
+    GitUtil gitUtil;
+    @Autowired
+    JarFileUtils jarFileUtils;
+
+    public VersionPo getVersion() {
+        VersionPo versionPo = new VersionPo();
+        Map<String,String> map=jarFileUtils.readJarFile();
+        versionPo.setGIT_Revision(gitUtil.getGitCommitId());
+        versionPo.setCreate_By(map.get("Created-By"));
+        versionPo.setGIT_BRANCH(gitUtil.getBranch());
+        versionPo.setGIT_URL(gitUtil.getGitUrl());
+        versionPo.setBUILD_DATE(gitUtil.getBuildDate());
+        versionPo.setArtifactId(config.getArtifactId());
+        versionPo.setGIT_Revision_SHORT(gitUtil.getCommitIdShort());
+        versionPo.setVersion(config.getVersion());
+        versionPo.setProject(config.getDescription());
+        versionPo.setBuild_Jdk(map.get("Build-Jdk"));
+
+        return versionPo;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/utils/GitUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/GitUtil.java
new file mode 100644
index 0000000..60ee987
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/utils/GitUtil.java
@@ -0,0 +1,44 @@
+package com.genersoft.iot.vmp.utils;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.stereotype.Component;
+
+/**
+ * 涓�涓紭绉�鐨勯搴熺▼搴忕尶锛圕SDN锛�
+ */
+@Component
+@PropertySource(value = {"classpath:git.properties" }, ignoreResourceNotFound = true)
+public class GitUtil {
+
+    @Value("${git.branch:null}")
+    private String branch;
+    @Value("${git.commit.id:null}")
+    private String gitCommitId;
+    @Value("${git.remote.origin.url:null}")
+    private String gitUrl;
+    @Value("${git.build.time:null}")
+    private String buildDate;
+    @Value("${git.commit.id.abbrev:null}")
+    private String commitIdShort;
+
+    public String getGitCommitId() {
+        return gitCommitId;
+    }
+
+    public String getBranch() {
+        return branch;
+    }
+
+    public String getGitUrl() {
+        return gitUrl;
+    }
+
+    public String getBuildDate() {
+        return buildDate;
+    }
+
+    public String getCommitIdShort() {
+        return commitIdShort;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/utils/JarFileUtils.java b/src/main/java/com/genersoft/iot/vmp/utils/JarFileUtils.java
new file mode 100644
index 0000000..686b562
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/utils/JarFileUtils.java
@@ -0,0 +1,73 @@
+package com.genersoft.iot.vmp.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ClassUtils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+/**
+ * 涓�涓紭绉�鐨勯搴熺▼搴忕尶
+ */
+@Component
+public class JarFileUtils {
+    private static Logger log = LoggerFactory.getLogger(JarFileUtils.class);
+    private static Map<String, String> map = new HashMap<>();
+
+    public  Map<String, String> readJarFile() {
+        JarFile jarFile = null;
+        BufferedReader br = null;
+        try {
+            // 鑾峰彇jar鐨勮繍琛岃矾寰勶紝鍥爈inux涓媕ar鐨勮矾寰勪负鈥漟ile:/app/.../test.jar!/BOOT-INF/class!/鈥滆繖绉嶆牸寮忥紝鎵�浠ラ渶瑕佸幓鎺夆�漟ile:鈥滃拰鈥�!/BOOT-INF/class!/鈥�
+            String jarFilePath = ClassUtils.getDefaultClassLoader().getResource("").getPath().replace("!/BOOT-INF/classes!/", "");
+            if (jarFilePath.startsWith("file")) {
+                jarFilePath = jarFilePath.substring(5);
+            }
+            log.debug("jarFilePath:" + jarFilePath);
+            // 閫氳繃JarFile鐨刧etJarEntry鏂规硶璇诲彇META-INF/MANIFEST.MF
+            jarFile = new JarFile(jarFilePath);
+            JarEntry entry = jarFile.getJarEntry("META-INF/MANIFEST.MF");
+            log.info("璇诲彇鐨勫唴瀹�:" + entry.toString());
+            // 濡傛灉璇诲彇鍒癕ANIFEST.MF鏂囦欢鍐呭锛屽垯杞崲涓簊tring
+            if (entry != null) {
+                InputStream in = jarFile.getInputStream(entry);
+
+                StringBuilder sb = new StringBuilder();
+                br = new BufferedReader(new InputStreamReader(in));
+                String line = "";
+                while ((line = br.readLine()) != null) {
+                    if (line != null && line.contains(":")) {
+                        int index = line.indexOf(":");
+                        map.put(line.substring(0, index).trim(), line.substring(index + 1, line.length()).trim());
+                    }
+                }
+                return map;
+            }
+        } catch (IOException e) {
+            log.debug("璇诲彇MANIFEST.MF鏂囦欢寮傚父:" + e.getMessage());
+        } finally {
+            try {
+                if (null != br) {
+                    br.close();
+                }
+                if (null != jarFile) {
+                    jarFile.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+
+        return map;
+
+    }
+
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/server/ServerController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/server/ServerController.java
index 76ad86c..4b7adc6 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/server/ServerController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/server/ServerController.java
@@ -1,6 +1,8 @@
 package com.genersoft.iot.vmp.vmanager.server;
 
 import com.genersoft.iot.vmp.VManageBootstrap;
+import com.genersoft.iot.vmp.common.VersionPo;
+import com.genersoft.iot.vmp.conf.VersionInfo;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
 import com.genersoft.iot.vmp.service.IMediaServerService;
 import com.genersoft.iot.vmp.utils.SpringBeanFactory;
@@ -30,6 +32,9 @@
 
     @Autowired
     private IMediaServerService mediaServerService;
+
+    @Autowired
+    VersionInfo versionInfo;
 
 
     @ApiOperation("娴佸獟浣撴湇鍔″垪琛�")
@@ -97,4 +102,15 @@
         restartThread.start();
         return "success";
     }
+
+    @ApiOperation("鐗堟湰淇℃伅")
+    @GetMapping(value = "/version")
+    @ResponseBody
+    public WVPResult<VersionPo> getVersion(){
+        WVPResult<VersionPo> result = new WVPResult<>();
+        result.setCode(0);
+        result.setMsg("success");
+        result.setData(versionInfo.getVersion());
+        return result;
+    }
 }

--
Gitblit v1.8.0