SpringBoot 启动输出 Git 版本信息(2023/07/11)

news/2024/9/18 2:51:58

SpringBoot 启动输出 Git 版本信息

文章目录

  • SpringBoot 启动输出 Git 版本信息
    • 1. 环境依赖
    • 2. pom.xml 配置
    • 3. 启动类配置

为了方便记录项目打包时的 Git 版本,本文将介绍如何将 Git 版本信息打包进 JAR 文件,并在项目启动时输出。

1. 环境依赖

  • SpringBoot 2.7.13;
  • git-commit-id-maven-plugin 4.9.9;

2. pom.xml 配置

<build>
    <plugins>
        <plugin>
            <groupId>io.github.git-commit-id</groupId>
            <artifactId>git-commit-id-maven-plugin</artifactId>
            <version>4.9.9</version>
            <executions>
                <execution>
                    <id>get-the-git-infos</id>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                    <phase>initialize</phase>
                </execution>
            </executions>
            <configuration>
                <injectAllReactorProjects>false</injectAllReactorProjects>
                <verbose>true</verbose>
                <skipPoms>true</skipPoms>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                <generateGitPropertiesFileWithEscapedUnicode>false</generateGitPropertiesFileWithEscapedUnicode>
                <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                <format>properties</format>
                <prefix>git</prefix>
                <dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
                <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
                <failOnNoGitDirectory>false</failOnNoGitDirectory>
                <failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
                <useNativeGit>false</useNativeGit>
                <skip>false</skip>
                <runOnlyOnce>false</runOnlyOnce>
                <commitIdGenerationMode>full</commitIdGenerationMode>
                <evaluateOnCommit>HEAD</evaluateOnCommit>
                <useBranchNameFromBuildEnvironment>true</useBranchNameFromBuildEnvironment>
                <injectIntoSysProperties>true</injectIntoSysProperties>
                <offline>true</offline>
            </configuration>
        </plugin>
    </plugins>
</build>

3. 启动类配置

package com.xiaoqqya.gitinfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.info.GitProperties;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * Spring boot git info demo.
 *
 * @author <a href="mailto:xiaoQQya@126.com>xiaoQQya</a>
 * @since 2023/07/11
 */
@SpringBootApplication
public class GitInfoApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(GitInfoApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(GitInfoApplication.class, args);

        printGitInfo(context);
    }

    /**
     * 输出 Git 版本控制信息.
     *
     * @param context SpringBoot 上下文信息
     */
    private static void printGitInfo(ConfigurableApplicationContext context) {
        try {
            if (LOGGER.isInfoEnabled()) {
                GitProperties gitProperties = context.getBean(GitProperties.class);
                LOGGER.info("Git branch: {}", gitProperties.get("branch"));
                LOGGER.info("Git build time: {}", gitProperties.get("build.time"));
                LOGGER.info("Git commit id: {}", gitProperties.get("commit.id.full"));
                LOGGER.info("Git commit user: {}", gitProperties.get("commit.user.name") + "<" + gitProperties.get("commit.user.email") + ">");
                LOGGER.info("Git commit time: {}", gitProperties.get("commit.time"));
                LOGGER.info("Git commit message: {}", gitProperties.get("commit.message.full"));
            }
        } catch (NoSuchBeanDefinitionException e) {
            LOGGER.warn(e.getMessage());
        }
    }
}

参考文章:

  • git-commit-id/git-commit-id-maven-plugin (github.com);
  • Injecting Git Information Into Spring Beans | Baeldung;
  • springboot获取项目git版本信息的几种方式_gitproperties_sky~hello的博客-CSDN博客;

http://lihuaxi.xjx100.cn/news/1334462.html

相关文章

字典树TRIE(前缀树)

字典树(Trie 树)是一种用于快速查找前缀的数据结构。它的主要思想是: 树的每个节点表示一个字符从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串每个节点的所有子节点包含共同的前缀 例如,对于单词列表[“hello”, “help”, “world”]可以构造这样的字典…

标准单元库---NLDM/CCS library model

Timing Model 数字芯片设计&#xff0c;除了全定制设计外&#xff0c;绝大部分都是基于std cell的半定制设计&#xff0c;那么std cell的模型就极为重要&#xff0c;尤其半定制&#xff0c;需要把一个std cell看成block box&#xff0c;只考虑其input/output pin。其input pin对…

Python 生成随机datetime和date

Python 生成随机datetime和date 验证程序经常需要生成随机的datetime和date 。 import datetime import time# 2000年到2023年日期时间 def randdatetime():mintime datetime.datetime(2000,1,1,0,0,0)maxtime datetime.datetime(2023,12,31,23,23,59)mintime_ts int(time…

【每天40分钟,我们一起用50天刷完 (剑指Offer)】第二十三天 23/50

专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客&#xff0c;如有问题交流&#xff0c;欢迎评论区留言&#xff0c;一定尽快回复&#xff01;&#xff08;大家可以去看我的专栏&#xff0c;是所有文章的目录&#xff09;   文章字体风格&#xff1a; 红色文字表示&#…

springboot驾校管理系统

框架&#xff1a;springboot JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09; 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.3.9 …

简单的复习下与 CSS Flex 布局相关的几个关键属性

揭开align-content、justify-content、align-items和justify-items的神秘面纱&#xff0c;解释它们各自的功能以及在不同的情境下如何使用。 在过去几年中&#xff0c;由于弹性盒子和网格布局的演变&#xff0c;CSS布局设计的艺术发生了重大变化。而这一变革的核心&#xff0c;…

【洛谷】P3954 [NOIP2017 普及组] 成绩

[NOIP2017 普及组] 成绩 题目背景 NOIP2017 普及组 T1 题目描述 牛牛最近学习了 C 入门课程&#xff0c;这门课程的总成绩计算方法是&#xff1a; 总成绩作业成绩$ \times 20% 小测成绩 小测成绩 小测成绩30% 期末考试成绩 期末考试成绩 期末考试成绩 \times 50%$ 牛牛想…

13 个最佳免费 PDF 编辑器清单

您正在寻找一款真正免费的 PDF 编辑器&#xff0c;不仅可以编辑和添加文本&#xff0c;还可以更改图像、添加您自己的图形、签署您的名字、填写表格等等&#xff1f;您来对地方了&#xff1a;我研究了这些类型的应用程序&#xff0c;以得出您正在寻找的内容的列表。 其中一些是…