648540858
2021-08-09 dbc525e8fb59f70c474863304e3c580e81dcedf0
添加版本信息接口
3个文件已修改
5个文件已添加
351 ■■■■■ 已修改文件
pom.xml 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/common/VersionPo.java 136 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/conf/ProxyServletConfig.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/conf/VersionConfig.java 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/conf/VersionInfo.java 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/utils/GitUtil.java 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/utils/JarFileUtils.java 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/genersoft/iot/vmp/vmanager/server/ServerController.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
src/main/java/com/genersoft/iot/vmp/common/VersionPo.java
New file
@@ -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的url
     */
    @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;
    }
}
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/*");
src/main/java/com/genersoft/iot/vmp/conf/VersionConfig.java
New file
@@ -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;
    }
}
src/main/java/com/genersoft/iot/vmp/conf/VersionInfo.java
New file
@@ -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;
    }
}
src/main/java/com/genersoft/iot/vmp/utils/GitUtil.java
New file
@@ -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;
/**
 * 一个优秀的颓废程序猿(CSDN)
 */
@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;
    }
}
src/main/java/com/genersoft/iot/vmp/utils/JarFileUtils.java
New file
@@ -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的运行路径,因linux下jar的路径为”file:/app/.../test.jar!/BOOT-INF/class!/“这种格式,所以需要去掉”file:“和”!/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的getJarEntry方法读取META-INF/MANIFEST.MF
            jarFile = new JarFile(jarFilePath);
            JarEntry entry = jarFile.getJarEntry("META-INF/MANIFEST.MF");
            log.info("读取的内容:" + entry.toString());
            // 如果读取到MANIFEST.MF文件内容,则转换为string
            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;
    }
}
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;
    }
}