648540858
4 years ago
8 changed files with 349 additions and 2 deletions
@ -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; |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
@ -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; |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue