簡(jiǎn)介
git-commit-id-maven-plugin 是一個(gè)maven 插件,用來(lái)在打包的時(shí)候?qū)it-commit 信息打進(jìn)jar中。
這樣做的好處是可以將發(fā)布的某版本和對(duì)應(yīng)的代碼關(guān)聯(lián)起來(lái),方便查閱和線上項(xiàng)目的維護(hù)。至于它的作用,用官方說(shuō)法,這個(gè)功能對(duì)于大型分布式項(xiàng)目來(lái)說(shuō)是無(wú)價(jià)的。
功能
你是否經(jīng)常遇到這樣的問(wèn)題:
-
測(cè)試提交了一個(gè)bug,開(kāi)發(fā)人員無(wú)法確認(rèn)是哪個(gè)版本有這個(gè)問(wèn)題,當(dāng)前測(cè)試環(huán)境部署的是某個(gè)版本嗎?生產(chǎn)環(huán)境會(huì)不會(huì)也有這個(gè)問(wèn)題?
-
公司內(nèi)部的項(xiàng)目,總共幾十、幾百個(gè)服務(wù),每天都有服務(wù)的生產(chǎn)環(huán)境部署,一個(gè)服務(wù)甚至一天上線好幾次,對(duì)于項(xiàng)目管理來(lái)說(shuō)無(wú)法清晰了解某一時(shí)刻某個(gè)服務(wù)的版本
-
如何驗(yàn)證我的代碼是否已經(jīng)上線?
-
。。。。。。
以上種種,都有一個(gè)共同的訴求,就是我希望在打包的時(shí)候?qū)⒆詈笠淮?git commit id 和當(dāng)前 jar 關(guān)聯(lián)起來(lái)并可試試查詢jar對(duì)應(yīng)的git commit id 。
實(shí)踐
引入插件
本例SpringBoot版本為 2.7.6,java版本為11
此插件已經(jīng)上傳到中央倉(cāng)庫(kù)(https://central.sonatype.com/artifact/io.github.git-commit-id/git-commit-id-maven-plugin?smo=true)
在項(xiàng)目pom.xml 中引入如下插件
<project>
? ? ......
? ? <build>
? ? ? ? <plugins>
? ? ? ? ? ? <!-- ?git-commit-id-maven-plugin :打包的時(shí)候攜帶git提交信息 ?-->
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>io.github.git-commit-id</groupId>
? ? ? ? ? ? ? ? <artifactId>git-commit-id-maven-plugin</artifactId>
? ? ? ? ? ? ? ? <version>5.0.0</version>
? ? ? ? ? ? ? ? <executions>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <id>get-the-git-infos</id>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>revision</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? ? ? <phase>initialize</phase>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? </executions>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <generateGitPropertiesFile>true</generateGitPropertiesFile>
? ? ? ? ? ? ? ? ? ? <generateGitPropertiesFilename>${project.build.outputDirectory}/git.</generateGitPropertiesFilename>
? ? ? ? ? ? ? ? ? ? <includeOnlyProperties>
? ? ? ? ? ? ? ? ? ? ? ? <includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
? ? ? ? ? ? ? ? ? ? ? ? <includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
? ? ? ? ? ? ? ? ? ? </includeOnlyProperties>
? ? ? ? ? ? ? ? ? ? <format>txt</format>
? ? ? ? ? ? ? ? ? ? <commitIdGenerationMode>full</commitIdGenerationMode>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>
</project>
- generateGitPropertiesFilename:用于指定生成的gitCommitInfo存放到哪個(gè)位置,后綴可以任意指定,如果不指定將使用format的值
- format:指定文件后綴,一般為 properties , json
- commitIdGenerationMode:記錄完整信息,若format為json,此值必須為full
此外為了能成功打出jar包,還需要如下插件的配合:
<plugin>
? ? <groupId>org.springframework.boot</groupId>
? ? <artifactId>spring-boot-maven-plugin</artifactId>
? ? <version>${spring-boot.version}</version>
? ? <configuration>
? ? ? ? <includeSystemScope>true</includeSystemScope>
? ? ? ? <excludes>
? ? ? ? ? ? <exclude>
? ? ? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? </exclude>
? ? ? ? </excludes>
? ? </configuration>
? ? <executions>
? ? ? ? <execution>
? ? ? ? ? ? <id>repackage</id>
? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? <goal>repackage</goal>
? ? ? ? ? ? </goals>
? ? ? ? </execution>
? ? </executions>
</plugin>
使用maven執(zhí)行 clean and package ,將在target\classes下生成 git.json文件,內(nèi)容如下:
#Generated by Git-Commit-Id-Plugin
git.build.time=2024-02-21T10\:41\:24+0800
git.build.version=0.0.1-SNAPSHOT
git.commit.id.abbrev=3fc9c80
git.commit.id.full=3fc9c8009a48e22ef171c98a97398005e9f30a4a
同時(shí),如果我們反編譯生成的jar包,將在BOOT-INF/classes下看到git.json 文件
GitCommitIdMavenPlugin插件有豐富的配置選項(xiàng),更多配置參考:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-832983.html
<configuration>
? ? <!--
? ? ? ? ? ? ? ? ? ? git文件記錄,默認(rèn)是
? ? ? ? ? ? ? ? ? ? ${project.basedir}/.git
? ? ? ? ? ? ? ? ? ? 如果非默認(rèn),可以指定,例如: ${project.basedir}/../.git
? ? ? ? ? ? ? ? -->
? ? <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
? ? <!--
? ? ? ? ? ? ? ? ? ? 屬性前綴,可以理解為namespace,默認(rèn)是git, 例如 `${configured-prefix}.commit.id`.
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? 更多信息可以參考 (see
? ? ? ? ? ? ? ? ? ? https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/137#issuecomment-418144756
? ? ? ? ? ? ? ? ? ? for a full example).
? ? ? ? ? ? ? ? -->
? ? <prefix>git</prefix>
? ? <!-- @since 2.2.0 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)的日期格式,使用方式(e.g. `git.commit.time` and `git.build.time`).
? ? ? ? ? ? ? ? ?-->
? ? <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
? ? <!-- @since 2.2.0 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 時(shí)區(qū)(java.util.TimeZone.getDefault().getID()).
? ? ? ? ? ? ? ? ? ? *Note*: 指定時(shí)區(qū)可以如下 `MAVEN_OPTS=-Duser.timezone=UTC mvn clean package`, `mvn clean package -Duser.timezone=UTC`
? ? ? ? ? ? ? ? ? ?或者使用 Asia/Shanghai 直接指定,該屬性會(huì)使用在
? ? ? ? ? ? ? ? ? ? (e.g. `git.commit.time` and `git.build.time`).
? ? ? ? ? ? ? ? -->
? ? <dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)false,構(gòu)建時(shí)打印信息
? ? ? ? ? ? ? ? -->
? ? <verbose>false</verbose>
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)false, 如果是true, 會(huì)生成properties 文件(填充文件中的屬性值),文件配置在 generateGitPropertiesFilename 中, 構(gòu)建時(shí)間使用如下
? ? ? ? ? ? ? ? ? ? ``` git.build.time=${git.build.time}
? ? ? ? ? ? ? ? ? ? ```
? ? ? ? ? ? ? ? -->
? ? <generateGitPropertiesFile>true</generateGitPropertiesFile>
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)文件
? ? ? ? ? ? ? ? ? ? ${project.build.outputDirectory}/git.properties
? ? ? ? ? ? ? ? ? ? 該路徑可以使用相對(duì)于${project.basedir}的相對(duì)路徑(e.g. target/classes/git.properties),或者全路徑(e.g. ${project.build.outputDirectory}/git.properties)
? ? ? ? ? ? ? ? -->
? ? <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
? ? <!--
? ? ? ? ? ? ? ? ? ?文件格式,默認(rèn)properties,可以使用json
如果將其設(shè)置為“json”,則還應(yīng)該簽出關(guān)于 `commitIdGenerationMode` 而且設(shè)置
? ? ? ? ? ? ? ? ? ? `<commitIdGenerationMode>full</commitIdGenerationMode>`.
? ? ? ? ? ? ? ? -->
? ? <format>properties</format>
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)是true,如果打包是pom(e.g. `<packaging>pom</packaging>`),則運(yùn)行該插件
? ? ? ? ? ? ? ? -->
? ? <skipPoms>true</skipPoms>
? ? <!-- @since 2.1.4 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 告訴maven git commit id將git屬性注入到所有reactor項(xiàng)目中,而不僅僅是現(xiàn)在的那個(gè)。默認(rèn)情況下,屬性設(shè)置為“false”,以防止重寫(xiě)可能與項(xiàng)目無(wú)關(guān)的屬性。如果需要公開(kāi)git屬性對(duì)于另一個(gè)maven模塊(例如maven antrun plugin),您需要將其設(shè)置為“true”。
? ? ? ? ? ? ? ? -->
? ? <injectAllReactorProjects>false</injectAllReactorProjects>
? ? <!-- @since 2.0.4 -->
? ? <!-- 默認(rèn)false, 指定在找不到.git目錄時(shí)插件是否應(yīng)失敗。當(dāng)設(shè)置為“false”并且沒(méi)有找到.git目錄時(shí),插件將跳過(guò)執(zhí)行。
? ? ? ? ? ? ? ? -->
? ? <failOnNoGitDirectory>true</failOnNoGitDirectory>
? ? <!-- @since 2.1.5 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)true,默認(rèn)情況下,如果插件無(wú)法獲取足夠的數(shù)據(jù)來(lái)完成,跳過(guò)執(zhí)行該插件。
? ? ? ? ? ? ? ? -->
? ? <failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
? ? <!-- @since 2.1.8 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)false, 當(dāng)設(shè)置為“true”時(shí),插件執(zhí)行將完全跳過(guò)。這對(duì)于配置文件激活的插件調(diào)用或使用屬性來(lái)啟用/禁用pom功能。在版本*2.2.3*中,您還可以使用命令行選項(xiàng)跳過(guò)插件`-Dmaven.gitcommitid.skip=true`
? ? ? ? ? ? ? ? -->
? ? <skip>false</skip>
? ? <!-- @since 3.0.1 -->
? ? <!--
? ? ? ? ? ? ? ? ? ?默認(rèn)false,當(dāng)設(shè)置為“true”時(shí),插件將不會(huì)嘗試聯(lián)系任何遠(yuǎn)程存儲(chǔ)庫(kù)。任何操作都將只使用回購(gòu)的本地狀態(tài)。如果設(shè)置為“false”,它將執(zhí)行“git fetch”操作,例如確定“ahead”和“behind”分支信息。
? ? ? ? ? ? ? ? -->
? ? <offline>false</offline>
? ? <!-- @since 2.1.12 -->
? ? <!--
默認(rèn) false,如果為true,只在一個(gè)模塊中運(yùn)行一次。這意味著插件的效果對(duì)執(zhí)行圖中的第一個(gè)項(xiàng)目執(zhí)行一次
? ? ? ? ? ? ? ? -->
? ? <runOnlyOnce>false</runOnlyOnce>
? ? <!-- @since 2.1.9 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 排除屬性 ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? -->
? ? <excludeProperties>
? ? ? ? <!-- <excludeProperty>git.user.*</excludeProperty> -->
? ? </excludeProperties>
? ? <!-- @since 2.1.14 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 只包含某類屬性,和excludeProperties相對(duì)
? ? ? ? ? ? ? ? -->
? ? <includeOnlyProperties>
? ? ? ? <!-- <includeOnlyProperty>^git.commit.id.full$</includeOnlyProperty> -->
? ? </includeOnlyProperties>
? ? <!-- @since 2.2.3 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 屬性替換,匹配到規(guī)則的屬性值在某個(gè)階段替換為另外的屬性值
? ? ? ? ? ? ? ? -->
? ? <replacementProperties>
? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? example:
? ? ? ? ? ? ? ? ? ? ? apply replacement only to the specific property git.branch and replace '/' with '-'
? ? ? ? ? ? ? ? ? ? ? see also [issue 138](https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/138)
? ? ? ? ? ? ? ? ? <replacementProperty>
? ? ? ? ? ? ? ? ? ? <property>git.branch</property>
? ? ? ? ? ? ? ? ? ? <propertyOutputSuffix>something</propertyOutputSuffix>
? ? ? ? ? ? ? ? ? ? <token>^([^\/]*)\/([^\/]*)$</token>
? ? ? ? ? ? ? ? ? ? <value>$1-$2</value>
? ? ? ? ? ? ? ? ? ? <regex>true</regex>
? ? ? ? ? ? ? ? ? ? <forceValueEvaluation>false</forceValueEvaluation>
? ? ? ? ? ? ? ? ? ? <transformationRules>
? ? ? ? ? ? ? ? ? ? ? <transformationRule>
? ? ? ? ? ? ? ? ? ? ? ? <apply>BEFORE</apply>
? ? ? ? ? ? ? ? ? ? ? ? <action>UPPER_CASE</action>
? ? ? ? ? ? ? ? ? ? ? </transformationRule>
? ? ? ? ? ? ? ? ? ? ? <transformationRule>
? ? ? ? ? ? ? ? ? ? ? ? <apply>AFTER</apply>
? ? ? ? ? ? ? ? ? ? ? ? <action>LOWER_CASE</action>
? ? ? ? ? ? ? ? ? ? ? </transformationRule>
? ? ? ? ? ? ? ? ? ? </transformationRules>
? ? ? ? ? ? ? ? ? </replacementProperty>
? ? ? ? ? ? ? ? ? -->
? ? </replacementProperties>
? ? <!-- @since 2.1.10 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)false,此插件附帶自定義的“jgit”實(shí)現(xiàn),用于獲取所有相關(guān)信息。如果設(shè)置為“true”,則此插件將使用本機(jī)“git”二進(jìn)制文件而不是自定義的“jgit”, 也可以使用以下命令開(kāi)啟 `-Dmaven.gitcommitid.nativegit=true`
? ? ? ? ? ? ? ? -->
? ? <useNativeGit>false</useNativeGit>
? ? <!-- @since 3.0.0 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)情況下,此超時(shí)設(shè)置為30000(30秒),允許指定使用本機(jī)獲取信息的超時(shí)(毫秒)
? ? ? ? ? ? ? ? -->
? ? <nativeGitTimeoutInMs>30000</nativeGitTimeoutInMs>
? ? <!-- @since v2.0.4 -->
? ? <!--
默認(rèn)7,配置縮寫(xiě)git提交id的長(zhǎng)度(`git.commit.id.abbrev`)到長(zhǎng)度至少為N。`0'具有特殊含義(簽出git/git文檔描述-描述.md)對(duì)于特殊情況,縮寫(xiě)為0)。最大值為“40”,因?yàn)樽畲骃HA-1長(zhǎng)度。
? ? ? ? ? ? ? ? ?-->
? ? <abbrevLength>7</abbrevLength>
? ? <!-- @since v2.2.0 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 目前,交換機(jī)允許兩種不同的選擇:1默認(rèn)情況下,此屬性設(shè)置為“flat”,并將生成以前已知的財(cái)產(chǎn)`git.commit.id`就像以前版本的插件一樣。保持默認(rèn)情況下,它將“flat”保留向后兼容性,不需要進(jìn)一步的操作最終用戶調(diào)整。2如果將此開(kāi)關(guān)設(shè)置為“full”,則插件將導(dǎo)出以前已知的屬性`git.commit.id`作為`git.commit.id.full`因此將生成完全有效的導(dǎo)出機(jī)制中的json對(duì)象。
? ? ? ? ? ? ? ? -->
? ? <commitIdGenerationMode>flat</commitIdGenerationMode>
? ? <!-- @since 2.1.0 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 可以用作非常強(qiáng)大的版本控制助手, 可以參考https://git-scm.com/docs/git-describe
? ? ? ? ? ? ? ? -->
? ? <gitDescribe>
? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ? 默認(rèn)false, 如果true,則不使用該配置
? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <skip>false</skip>
? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ?默認(rèn)true,
在某些情況下,在提交附近找不到標(biāo)記(例如,通常在執(zhí)行淺克隆)。如果將其設(shè)置為“true”,并且未找到標(biāo)記,則此屬性將改為回退到提交的id(當(dāng)“true”時(shí),此屬性不會(huì)變?yōu)榭眨?? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <always>true</always>
? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ? 在describe輸出中,哈希的對(duì)象id總是縮寫(xiě)為N個(gè)字母(默認(rèn)為7)
? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <abbrev>7</abbrev>
? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ? Default (optional):
? ? ? ? ? ? ? ? ? ? ? ? -dirty
? ? ? ? ? ? ? ? ? ? ? ? 在處于“臟狀態(tài)”(未提交)的存儲(chǔ)庫(kù)上運(yùn)行“描述”時(shí)更改),說(shuō)明輸出將包含一個(gè)附加后綴
? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <dirty>-dirty</dirty>
? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ? 默認(rèn):*,包含所有信息,
Git describe可能包含標(biāo)記名的信息。將此配置設(shè)置為僅考慮與給定模式匹配的標(biāo)記。這可以用來(lái)避免從存儲(chǔ)庫(kù)泄漏私有標(biāo)記。
? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <match>*</match>
? ? ? ? <!--
默認(rèn)false,運(yùn)行g(shù)it describe時(shí),默認(rèn)情況下只查找*帶注釋的標(biāo)記*。如果您希望在描述中也考慮*輕量級(jí)標(biāo)記*,則需要把這個(gè)轉(zhuǎn)換成'true'。
? ? ? ? ? ? ? ? ? ? ? ? depth here: https://github.com/git-commit-id/git-commit-id-maven-plugin/#git-describe-and-a-small-gotcha-with-tags
? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <tags>false</tags>
? ? ? ? <!--
默認(rèn)情況下,如果當(dāng)前提交被標(biāo)記,git descripe只返回標(biāo)記名。將此選項(xiàng)設(shè)置為“true”以強(qiáng)制它使用典型的describe格式化輸出格式(“${tag name}-${committes_from_tag}-g${commit_id-maybe_dirty}”),即使是“on”標(biāo)記
? ? ? ? ? ? ? ? ? ? -->
? ? ? ? <forceLongFormat>false</forceLongFormat>
? ? </gitDescribe>
? ? <!-- @since 2.2.2 -->
? ? <!--
附加的驗(yàn)證實(shí)用程序,可用于驗(yàn)證項(xiàng)目屬性是否設(shè)置
? ? ? ? ? ? ? ? -->
? ? <validationProperties>
? ? ? ? <validationProperty>
? ? ? ? ? ? <!--
用于識(shí)別驗(yàn)證的描述性名稱,不匹配(將顯示在錯(cuò)誤消息中)
? ? ? ? ? ? ? ? ? ? ? ? -->
? ? ? ? ? ? <name>validating project version</name>
? ? ? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?需要驗(yàn)證的值*注意*:為了能夠驗(yàn)證在pom本身您可能需要設(shè)置配置`<injectAllReactorProjects>true</injectAllReactorProjects>`。
? ? ? ? ? ? ? ? ? ? ? ? -->
? ? ? ? ? ? <value>${project.version}</value>
? ? ? ? ? ? <!--
? ? ? ? ? ? ? ? ? ? ? ? ? ? the expected value
? ? ? ? ? ? ? ? ? ? ? ? -->
? ? ? ? ? ? <shouldMatchTo><![CDATA[^.*(?<!-SNAPSHOT)$]]></shouldMatchTo>
? ? ? ? </validationProperty>
? ? ? ? <!-- the next validationProperty you would like to validate -->
? ? </validationProperties>
? ? <!-- @since 2.2.2 -->
? ? <!--
? ? ? ? ? ? ? ? ? ? 默認(rèn)true,如果有與預(yù)期不符,則校驗(yàn)失敗
? ? ? ? ? ? ? ? -->
? ? <validationShouldFailIfNoMatch>true</validationShouldFailIfNoMatch>
? ? <!-- @since 2.2.4 -->
? ? <!--默認(rèn)值(可選):默認(rèn)情況下,此屬性只需設(shè)置為“HEAD”,它應(yīng)該引用最新的在存儲(chǔ)庫(kù)中提交。
說(shuō)明:
允許告訴插件應(yīng)該使用什么提交作為生成屬性來(lái)自。
一般情況下,可以將此屬性設(shè)置為“HEAD^1”或指向分支或標(biāo)記名稱。為了支持任何類型或用例,也可以設(shè)置此配置整個(gè)提交哈?;蛩目s寫(xiě)版本。
? ? ? ? ? ? ? ? -->
? ? <evaluateOnCommit>HEAD</evaluateOnCommit>
? ? <!-- @since 3.0.0 -->
? ? <!--
默認(rèn)true,當(dāng)設(shè)置為“true”時(shí),此插件將嘗試使用生成環(huán)境中的分支名稱。
? ? ? ? ? ? ? ? -->
? ? ? ? ? ?useBranchNameFromBuildEnvironment>true</useBranchNameFromBuildEnvironment>
<!-- @since 3.0.0 -->
<!--
默認(rèn)true,說(shuō)明:
當(dāng)設(shè)置為“true”時(shí),此插件將嘗試將生成的屬性公開(kāi)到`System.getProperties()`. 設(shè)置為{@code'false'}以避免此曝光。 -->
<injectIntoSysProperties>true</injectIntoSysProperties>
</configuration>
查詢gitCommitInfo
通過(guò)編寫(xiě)一個(gè)接口,用來(lái)查詢生成的GitCommitInfo,核心代碼如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-832983.html
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
@Slf4j
@RestController
@RequestMapping("/version")
public class VersionController {
? ? /**
? ? ?* 獲取 git.json 中的內(nèi)容
? ? ?* @return
? ? ?* @throws IOException
? ? ?*/
? ? @GetMapping("/gitCommitId")
? ? public String getGitCommitId() throws IOException {
? ? ? ? //git.json ?or ?git.properties
? ? ? ? File file = ResourceUtils.getFile("classpath:git.json");
? ? ? ? if (file.exists()) {
? ? ? ? ? ? String s = "";
? ? ? ? ? ? InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
? ? ? ? ? ? BufferedReader br = new BufferedReader(in);
? ? ? ? ? ? StringBuffer content = new StringBuffer();
? ? ? ? ? ? while ((s = br.readLine()) != null) {
? ? ? ? ? ? ? ? content = content.append(s);
? ? ? ? ? ? }
? ? ? ? ? ? return content.toString();
? ? ? ? } else {
? ? ? ? ? ? return "";
? ? ? ? }
? ? }
}
兼容性
與java的兼容性
- java8:插件版本=4.x
- java11:插件版本>=5
與maven的兼容性
- maven3:插件版本=4.x
- maven3.2.x:插件版本>=7
引用
- https://search.maven.org/artifact/io.github.git-commit-id/git-commit-id-maven-plugin
- https://github.com/git-commit-id/git-commit-id-maven-plugin
到了這里,關(guān)于SpringBoot使用git-commit-id-maven-plugin打包的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!