gradle 构建项目添加版本信息

news/2024/11/14 9:21:09/

gradle 构建项目添加版本信息,打包使用 spring boot 的打包插件

gradle__1">build.gradle 配置文件

bootJar {manifest {attributes('Project-Name': project.name,'Project-Version': project.version,"project-Vendor": "XXX Corp","Built-By": "Gradle ${gradle.gradleVersion}","Built-At": new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),'Git-Branch': getGitBranch(),'Git-Commit': getGitCommit(),'Git-Commit-Time': getGitCommitTime())}
}// 获取当前分支名称
static def getGitBranch() {try {def branch = 'git rev-parse --abbrev-ref HEAD'.execute().text.trim()return branch ?: 'unknown'} catch (Exception e) {System.err.println("fail to get branch, errMsg:" + e.getMessage())return 'unknown'}
}// 获取当前提交的记录的commitId
static def getGitCommit() {try {def commit = 'git rev-parse  HEAD'.execute().text.trim()return commit ?: 'unknown'} catch (Exception e) {System.err.println("fail to get commit id, errMsg:" + e.getMessage())return 'unknown'}
}// 获取当前Git提交的时间
static def getGitCommitTime() {try {def commitTime = 'git show -s --format=%ct HEAD'.execute().text.trim()return commitTime ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(commitTime) * 1000L)) : 'unknown'} catch (Exception e) {System.err.println("fail to get commit time, errMsg:" + e.getMessage())return 'unknown'}
}

版本信息模型

java">public class VersionModel {private String projectName;private String projectVersion;private String projectVendor;private String builtBy;private String builtAt;private String gitBranch;private String gitCommit;private String gitCommitTime;public String getProjectName() {return projectName;}public void setProjectName(String projectName) {this.projectName = projectName;}public String getProjectVersion() {return projectVersion;}public void setProjectVersion(String projectVersion) {this.projectVersion = projectVersion;}public String getProjectVendor() {return projectVendor;}public void setProjectVendor(String projectVendor) {this.projectVendor = projectVendor;}public String getBuiltBy() {return builtBy;}public void setBuiltBy(String builtBy) {this.builtBy = builtBy;}public String getBuiltAt() {return builtAt;}public void setBuiltAt(String builtAt) {this.builtAt = builtAt;}public String getGitBranch() {return gitBranch;}public void setGitBranch(String gitBranch) {this.gitBranch = gitBranch;}public String getGitCommit() {return gitCommit;}public void setGitCommit(String gitCommit) {this.gitCommit = gitCommit;}public String getGitCommitTime() {return gitCommitTime;}public void setGitCommitTime(String gitCommitTime) {this.gitCommitTime = gitCommitTime;}@Overridepublic String toString() {return "VersionModel{" +"projectName='" + projectName + '\'' +", projectVersion='" + projectVersion + '\'' +", implementationVendor='" + projectVendor + '\'' +", builtBy='" + builtBy + '\'' +", builtAt='" + builtAt + '\'' +", gitBranch='" + gitBranch + '\'' +", gitCommit='" + gitCommit + '\'' +", gitCommitTime='" + gitCommitTime + '\'' +'}';}
}

版本工具类

java">import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.Manifest;public class BuildUtils {public static final String projectName = "Project-Name";public static final String projectVersion = "Project-Version";public static final String projectVendor = "Project-Vendor";public static final String builtBy = "Built-By";public static final String builtAt = "Built-At";public static final String gitBranch = "Git-Branch";public static final String gitCommit = "Git-Commit";public static final String gitCommitTime = "Git-Commit-Time";private static final Logger log = LoggerFactory.getLogger(BuildUtils.class);public static String getManifestAttribute(String attributeName) {try {Manifest manifest = new Manifest(BuildUtils.class.getResourceAsStream("/META-INF/MANIFEST.MF"));Attributes attributes = manifest.getMainAttributes();return attributes.getValue(attributeName);} catch (IOException e) {log.warn("fail to get info from manifest file, errMsg:{}", e.getMessage(), e);return "unknown";}}public static Attributes getManifestAttribute() {try {Manifest manifest = new Manifest(BuildUtils.class.getResourceAsStream("/META-INF/MANIFEST.MF"));return manifest.getMainAttributes();} catch (IOException e) {log.warn("fail to get info from manifest file, errMsg:{}", e.getMessage(), e);return new Attributes();}}public static String getManifestAttributeJsonFormat() {return JSONObject.toJSONString(getVersionModel(), true);}public static VersionModel getVersionModel() {VersionModel versionModel = new VersionModel();try {Manifest manifest = new Manifest(BuildUtils.class.getResourceAsStream("/META-INF/MANIFEST.MF"));versionModel.setBuiltAt(manifest.getMainAttributes().getValue(builtAt));versionModel.setBuiltBy(manifest.getMainAttributes().getValue(builtBy));versionModel.setProjectName(manifest.getMainAttributes().getValue(projectName));versionModel.setProjectVersion(manifest.getMainAttributes().getValue(projectVersion));versionModel.setProjectVendor(manifest.getMainAttributes().getValue(projectVendor));versionModel.setGitBranch(manifest.getMainAttributes().getValue(gitBranch));versionModel.setGitCommit(manifest.getMainAttributes().getValue(gitCommit));versionModel.setGitCommitTime(manifest.getMainAttributes().getValue(gitCommitTime));} catch (IOException e) {log.warn("fail to get info from manifest file, errMsg:{}", e.getMessage(), e);}return versionModel;}
}

方法1 controller 示例

java">@RestController
@RequestMapping("/version")
public class VersionController {@GetMapping("/build")public VersionModel getVersionModel(){return BuildUtils.getVersionModel();}
}

方法2 执行 jar 包中的类执行获取版本信息

java">package com.version;
public class Version {public static void main(String[] args) {System.out.println(BuildUtils.getManifestAttributeJsonFormat());}
}

java -cp you-app.jar -Dloader.main=com.version.Version org.springframework.boot.loader.PropertiesLauncher

参考连接

https://blog.csdn.net/russle/article/details/130658805


http://www.ppmy.cn/news/1499820.html

相关文章

信号【Linux】

文章目录 信号处理方式(信号递达)前后台进程 终端按键产生信号kill系统调用接口向进程发信号阻塞信号sigset_tsigprocmasksigpending内核态与用户态:内核空间与用户空间内核如何实现信号的捕捉 1、信号就算没有产生,进程也必须识别…

【论文速读】| LLMCloudHunter:利用大语言模型(LLMs)从基于云的网络威胁情报(CTI)中自动提取检测规则

本次分享论文:LLMCloudHunter: Harnessing LLMs for Automated Extraction of Detection Rules from Cloud-Based CTI 基本信息 原文作者:Yuval Schwartz, Lavi Benshimol, Dudu Mimran, Yuval Elovici, Asaf Shabtai 作者单位:Ben-Gurion…

微服务:网关

网关 网关,即网络的关口,当一个网络传输到另一个网络时就需要经过网关来实现 数据的路由和转发 以及 数据安全的校验 网关技术实现 SpringCloudGateWay: 基于Spring的WebFlux技术,完全支持响应式编程,吞吐能力更强 SpringCloudGateWay 依赖 <!--网关--><depe…

【Android】性能实践—编码优化与布局优化学习笔记

编码优化 使用场景 如果需要拼接字符串&#xff0c;优先使用StringBuffer和StringBuilder进行凭借&#xff0c;他们的性能优于直接用加号进行拼接&#xff0c;因为使用加号连接符会创建多余的对象一般情况下使用基本数据类来代替封装数据类型&#xff08;比如int优于Integer&…

四步实现网站HTTPS访问

随着网络安全的重要性日益凸显&#xff0c;HTTPS&#xff08;超文本传输安全协议&#xff09;已成为现代网站的标准配置。HTTPS协议作为HTTP协议的安全版本&#xff0c;通过SSL协议加密数据传输&#xff0c;不仅能保护用户数据的安全&#xff0c;还能提升搜索引擎排名&#xff…

3U/6U CPCI 系统核心模块,可应用于电力、轨道交通等领域

一、3U CPCI核心模块 提供CPCI系列产品&#xff0c;具有&#xff0c;高性能&#xff0c;应用扩展灵活&#xff0c;X86软件兼容&#xff0c;无风扇设计&#xff0c;宽工作温度范围&#xff0c;长嵌入式生命周期等特点&#xff0c;满足不同客户的需求。 产品特点 Intel Pentium …

openCv -- 优势

OpenCV(开源计算机视觉库)是一个广泛使用的计算机视觉和机器学习软件框架。它最初由Intel开发,后来由Itseez公司维护,最终于2015年成为非营利组织OpenCV.org的一部分。OpenCV的目的是实现一个易于使用且高效的计算机视觉框架,支持实时视觉应用。 以下是关于OpenCV的一些关…

聚观早报 | Meta发布Llama 3.1 405B;特斯拉发布二季度财报

聚观早报每日整理最值得关注的行业重点事件&#xff0c;帮助大家及时了解最新行业动态&#xff0c;每日读报&#xff0c;就读聚观365资讯简报。 整理丨Cutie 7月25日消息 Meta发布Llama 3.1 405B 特斯拉发布二季度财报 NVIDIA AI Foundry上线 iPhone 16将改进内部设计 快…