gradle 构建项目添加版本信息

宁睿  金牌会员 | 2024-7-27 11:33:55 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 570|帖子 570|积分 1718

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

  1. bootJar {
  2.     manifest {
  3.         attributes(
  4.                 'Project-Name': project.name,
  5.                 'Project-Version': project.version,
  6.                 "project-Vendor": "XXX Corp",
  7.                 "Built-By": "Gradle ${gradle.gradleVersion}",
  8.                 "Built-At": new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
  9.                 'Git-Branch': getGitBranch(),
  10.                 'Git-Commit': getGitCommit(),
  11.                 'Git-Commit-Time': getGitCommitTime()
  12.         )
  13.     }
  14. }
  15. // 获取当前分支名称
  16. static def getGitBranch() {
  17.     try {
  18.         def branch = 'git rev-parse --abbrev-ref HEAD'.execute().text.trim()
  19.         return branch ?: 'unknown'
  20.     } catch (Exception e) {
  21.         System.err.println("fail to get branch, errMsg:" + e.getMessage())
  22.         return 'unknown'
  23.     }
  24. }
  25. // 获取当前提交的记录的commitId
  26. static def getGitCommit() {
  27.     try {
  28.         def commit = 'git rev-parse  HEAD'.execute().text.trim()
  29.         return commit ?: 'unknown'
  30.     } catch (Exception e) {
  31.         System.err.println("fail to get commit id, errMsg:" + e.getMessage())
  32.         return 'unknown'
  33.     }
  34. }
  35. // 获取当前Git提交的时间
  36. static def getGitCommitTime() {
  37.     try {
  38.         def commitTime = 'git show -s --format=%ct HEAD'.execute().text.trim()
  39.         return commitTime ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(commitTime) * 1000L)) : 'unknown'
  40.     } catch (Exception e) {
  41.         System.err.println("fail to get commit time, errMsg:" + e.getMessage())
  42.         return 'unknown'
  43.     }
  44. }
复制代码
版本信息模型

  1. public class VersionModel {
  2.     private String projectName;
  3.     private String projectVersion;
  4.     private String projectVendor;
  5.     private String builtBy;
  6.     private String builtAt;
  7.     private String gitBranch;
  8.     private String gitCommit;
  9.     private String gitCommitTime;
  10.     public String getProjectName() {
  11.         return projectName;
  12.     }
  13.     public void setProjectName(String projectName) {
  14.         this.projectName = projectName;
  15.     }
  16.     public String getProjectVersion() {
  17.         return projectVersion;
  18.     }
  19.     public void setProjectVersion(String projectVersion) {
  20.         this.projectVersion = projectVersion;
  21.     }
  22.     public String getProjectVendor() {
  23.         return projectVendor;
  24.     }
  25.     public void setProjectVendor(String projectVendor) {
  26.         this.projectVendor = projectVendor;
  27.     }
  28.     public String getBuiltBy() {
  29.         return builtBy;
  30.     }
  31.     public void setBuiltBy(String builtBy) {
  32.         this.builtBy = builtBy;
  33.     }
  34.     public String getBuiltAt() {
  35.         return builtAt;
  36.     }
  37.     public void setBuiltAt(String builtAt) {
  38.         this.builtAt = builtAt;
  39.     }
  40.     public String getGitBranch() {
  41.         return gitBranch;
  42.     }
  43.     public void setGitBranch(String gitBranch) {
  44.         this.gitBranch = gitBranch;
  45.     }
  46.     public String getGitCommit() {
  47.         return gitCommit;
  48.     }
  49.     public void setGitCommit(String gitCommit) {
  50.         this.gitCommit = gitCommit;
  51.     }
  52.     public String getGitCommitTime() {
  53.         return gitCommitTime;
  54.     }
  55.     public void setGitCommitTime(String gitCommitTime) {
  56.         this.gitCommitTime = gitCommitTime;
  57.     }
  58.     @Override
  59.     public String toString() {
  60.         return "VersionModel{" +
  61.                 "projectName='" + projectName + '\'' +
  62.                 ", projectVersion='" + projectVersion + '\'' +
  63.                 ", implementationVendor='" + projectVendor + '\'' +
  64.                 ", builtBy='" + builtBy + '\'' +
  65.                 ", builtAt='" + builtAt + '\'' +
  66.                 ", gitBranch='" + gitBranch + '\'' +
  67.                 ", gitCommit='" + gitCommit + '\'' +
  68.                 ", gitCommitTime='" + gitCommitTime + '\'' +
  69.                 '}';
  70.     }
  71. }
复制代码
版本工具类

  1. import com.alibaba.fastjson.JSONObject;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.IOException;
  5. import java.util.jar.Attributes;
  6. import java.util.jar.Manifest;
  7. public class BuildUtils {
  8.     public static final String projectName = "Project-Name";
  9.     public static final String projectVersion = "Project-Version";
  10.     public static final String projectVendor = "Project-Vendor";
  11.     public static final String builtBy = "Built-By";
  12.     public static final String builtAt = "Built-At";
  13.     public static final String gitBranch = "Git-Branch";
  14.     public static final String gitCommit = "Git-Commit";
  15.     public static final String gitCommitTime = "Git-Commit-Time";
  16.     private static final Logger log = LoggerFactory.getLogger(BuildUtils.class);
  17.     public static String getManifestAttribute(String attributeName) {
  18.         try {
  19.             Manifest manifest = new Manifest(BuildUtils.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
  20.             Attributes attributes = manifest.getMainAttributes();
  21.             return attributes.getValue(attributeName);
  22.         } catch (IOException e) {
  23.             log.warn("fail to get info from manifest file, errMsg:{}", e.getMessage(), e);
  24.             return "unknown";
  25.         }
  26.     }
  27.     public static Attributes getManifestAttribute() {
  28.         try {
  29.             Manifest manifest = new Manifest(BuildUtils.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
  30.             return manifest.getMainAttributes();
  31.         } catch (IOException e) {
  32.             log.warn("fail to get info from manifest file, errMsg:{}", e.getMessage(), e);
  33.             return new Attributes();
  34.         }
  35.     }
  36.     public static String getManifestAttributeJsonFormat() {
  37.         return JSONObject.toJSONString(getVersionModel(), true);
  38.     }
  39.     public static VersionModel getVersionModel() {
  40.         VersionModel versionModel = new VersionModel();
  41.         try {
  42.             Manifest manifest = new Manifest(BuildUtils.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
  43.             versionModel.setBuiltAt(manifest.getMainAttributes().getValue(builtAt));
  44.             versionModel.setBuiltBy(manifest.getMainAttributes().getValue(builtBy));
  45.             versionModel.setProjectName(manifest.getMainAttributes().getValue(projectName));
  46.             versionModel.setProjectVersion(manifest.getMainAttributes().getValue(projectVersion));
  47.             versionModel.setProjectVendor(manifest.getMainAttributes().getValue(projectVendor));
  48.             versionModel.setGitBranch(manifest.getMainAttributes().getValue(gitBranch));
  49.             versionModel.setGitCommit(manifest.getMainAttributes().getValue(gitCommit));
  50.             versionModel.setGitCommitTime(manifest.getMainAttributes().getValue(gitCommitTime));
  51.         } catch (IOException e) {
  52.             log.warn("fail to get info from manifest file, errMsg:{}", e.getMessage(), e);
  53.         }
  54.         return versionModel;
  55.     }
  56. }
复制代码
方法1 controller 示例

  1. @RestController
  2. @RequestMapping("/version")
  3. public class VersionController {
  4.     @GetMapping("/build")
  5.     public VersionModel getVersionModel(){
  6.         return BuildUtils.getVersionModel();
  7.     }
  8. }
复制代码
方法2 实验 jar 包中的类实验获取版本信息

  1. package com.version;
  2. public class Version {
  3.     public static void main(String[] args) {
  4.         System.out.println(BuildUtils.getManifestAttributeJsonFormat());
  5.     }
  6. }
复制代码
  java -cp you-app.jar -Dloader.main=com.version.Version org.springframework.boot.loader.PropertiesLauncher
  参考毗连

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宁睿

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表