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

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-idgroupId>
            <artifactId>git-commit-id-maven-pluginartifactId>
            <version>4.9.9version>
            <executions>
                <execution>
                    <id>get-the-git-infosid>
                    <goals>
                        <goal>revisiongoal>
                    goals>
                    <phase>initializephase>
                execution>
            executions>
            <configuration>
                <injectAllReactorProjects>falseinjectAllReactorProjects>
                <verbose>trueverbose>
                <skipPoms>trueskipPoms>
                <generateGitPropertiesFile>truegenerateGitPropertiesFile>
                <generateGitPropertiesFilename>${project.build.outputDirectory}/git.propertiesgenerateGitPropertiesFilename>
                <generateGitPropertiesFileWithEscapedUnicode>falsegenerateGitPropertiesFileWithEscapedUnicode>
                <dotGitDirectory>${project.basedir}/.gitdotGitDirectory>
                <format>propertiesformat>
                <prefix>gitprefix>
                <dateFormat>yyyy-MM-dd HH:mm:ssdateFormat>
                <dateFormatTimeZone>${user.timezone}dateFormatTimeZone>
                <failOnNoGitDirectory>falsefailOnNoGitDirectory>
                <failOnUnableToExtractRepoInfo>falsefailOnUnableToExtractRepoInfo>
                <useNativeGit>falseuseNativeGit>
                <skip>falseskip>
                <runOnlyOnce>falserunOnlyOnce>
                <commitIdGenerationMode>fullcommitIdGenerationMode>
                <evaluateOnCommit>HEADevaluateOnCommit>
                <useBranchNameFromBuildEnvironment>trueuseBranchNameFromBuildEnvironment>
                <injectIntoSysProperties>trueinjectIntoSysProperties>
                <offline>trueoffline>
            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 ");
                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博客;

你可能感兴趣的:(Java,Git,maven,jar,java,git)