springboot获取项目的版本信息(git+maven,项目的版本号及名称等等)

引言

springboot获取项目的版本其实pom集成git的插件就可以了,但是只能获取git的提价的commit-id等简单的信息,如项目的版本号、名称等这是无法获取的,那么来谈谈如何获取自己想要的信息呢,springboot集成git插件会生成git.properties文件及maven会生成MANIFEST.MF及pom文件就可以获取所有的相关信息,这样就可以根据需要来进行提取了

读取git.properties文件

集成git和maven插件

  
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                pl.project13.maven
                git-commit-id-plugin
            
        
    

使用maven包时会在classes文件在生成git.properties文件,文件信息如下:

springboot获取项目的版本信息(git+maven,项目的版本号及名称等等)_第1张图片

分支、commit号、构建的时间等信息

在META-INF下会生成MANIFEST.MF文件,文件信息如下:

springboot获取项目的版本信息(git+maven,项目的版本号及名称等等)_第2张图片

可以看到版本号、项目名称等

既然能得到文件,那就好办了,因为git.properties和MANIFEST.MF有相似的信息,我这块按git.properties中的信息。

读取git.properties信息工具类

@Component
@PropertySource(value = {"classpath:git.properties" }, ignoreResourceNotFound =true)
public class GitUtil {
	@Value("${git.branch}")
	private String branch;
	@Value("${git.commit.id}")
	private String gitCommitId;
	@Value("${git.remote.origin.url}")
	private String gitUrl;
	@Value("${git.build.time}")
	private String buildDate;
	@Value("${git.commit.id.abbrev}")
	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;
	}
}

读取MANIFEST.MF的信息

@Component
public class JarFileUtils {
	private static Logger log = LoggerFactory.getLogger(JarFileUtils.class);
	private static Map map = new HashMap<>();

	public  Map 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 (ServiceException ex) {
			log.debug("读取MANIFEST.MF文件异常:" + ex.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != br) {
					br.close();
				}
				if (null != jarFile) {
					jarFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return map;

	}

}

读取pom的信息工具类

@Component
@ConfigurationProperties(prefix = "app")
public class Config {

	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;
	}

}

yml文件配置如下:

springboot获取项目的版本信息(git+maven,项目的版本号及名称等等)_第3张图片

在写一个统一的工具类

@Component
public class VersionUtil {
	@Autowired
	Config config;
	@Autowired
	GitUtil gitUtil;
	@Autowired
	JarFileUtils jarFileUtils;

	public VersionPo getVersion() {
		VersionPo versionPo = new VersionPo();
		Map 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;
	}
}

及po类

public class VersionPo implements Serializable {

	/**
	 * 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;
	}
}

使用这个统一的工具类 就可以对版本信息就可以统一输出,编写controller进行访问

@RestController
@RequestMapping("/appVersion")
@Api(description = "获取项目版本信息")
public class VersionController {
	@Autowired
	VersionUtil versionUtil;

	@ResponseBody
	@GetMapping("/version")
    //ResVo是自己封装的类
	public ResVo getConfig() {
		return ResVo.ok(versionUtil.getVersion());
	}
}

信息如下:

springboot获取项目的版本信息(git+maven,项目的版本号及名称等等)_第4张图片

这样就可以啦

如果只是需要简单的git信息通过暴露/actuator/info接口即可。

你可能感兴趣的:(springboot)