javascript
git hok json_从战中反弹:将Git提交信息作为JSON返回
git hok json
在某些情況下,我們必須知道部署到遠(yuǎn)程服務(wù)器的Web應(yīng)用程序的確切版本。 例如,客戶可能想知道我們是否已經(jīng)在服務(wù)器X上部署了錯(cuò)誤修復(fù)程序。
當(dāng)然,我們可以嘗試使用“傳統(tǒng)”方法找到該問題的答案。 問題是:
- 沒有人不記得是誰(shuí)更新了服務(wù)器X或何時(shí)更新了服務(wù)器X。
- 更新它的人不記得哪個(gè)是構(gòu)建中包含的最后一次提交。
換句話說(shuō),我們被搞砸了。 我們可以嘗試測(cè)試服務(wù)器X上是否仍然存在該錯(cuò)誤,但這對(duì)我們沒有幫助,因?yàn)槲覀兊腻e(cuò)誤修復(fù)可能無(wú)法正常工作。
這篇博客文章描述了我們?nèi)绾谓鉀Q這個(gè)問題。 讓我們從提取Git存儲(chǔ)庫(kù)的構(gòu)建時(shí)狀態(tài)開始。
如果使用Spring Boot,則應(yīng)使用Spring Boot Actuator 。 它可以幫助您發(fā)布有關(guān)Git存儲(chǔ)庫(kù)狀態(tài)的信息 。
如果您尚未閱讀以下博客文章, 則應(yīng)先閱讀它們,然后再繼續(xù)閱讀此博客文章 :
- 從槽中彈起:將屬性值注入配置Bean描述了為什么應(yīng)將屬性值注入配置Bean并幫助您做到這一點(diǎn)。
- 從槽中彈跳:以JSON返回運(yùn)行時(shí)配置描述了如何將Web應(yīng)用程序的運(yùn)行時(shí)配置寫入日志文件并將其作為JSON返回。
提取我們的Git倉(cāng)庫(kù)的構(gòu)建時(shí)間狀態(tài)
我們可以使用Maven Git Commit Id插件提取Git存儲(chǔ)庫(kù)的構(gòu)建時(shí)狀態(tài)。 讓我們了解如何配置Maven Git Commit Id插件并將提取的信息添加到屬性文件中。
首先 ,我們需要配置資源目錄的位置,并確保將從屬性文件中找到的屬性占位符替換為實(shí)際的屬性值。 我們可以通過將以下XML添加到pom.xml文件的build部分中來(lái)實(shí)現(xiàn):
<resources><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>**/*.properties</include></includes></resource> </resources>其次 ,我們需要配置Maven Git Commit Id插件。 我們可以按照以下步驟進(jìn)行操作:
我們需要將以下XML添加到pom.xml文件的plugins部分:
<plugin><groupId>pl.project13.maven</groupId><artifactId>git-commit-id-plugin</artifactId><version>2.1.13</version><!--Ensure that the revision goal is invoked during the initializephase.--><executions><execution><goals><goal>revision</goal></goals></execution></executions><configuration><!--Configure the location of the .git directory.--><dotGitDirectory>${project.basedir}/../.git</dotGitDirectory></configuration> </plugin>如果您對(duì)Maven Git Commit Id插件的默認(rèn)配置不滿意,則應(yīng)仔細(xì)閱讀其自述文件:
- 使用該插件提供了帶注釋的XML配置文件,該文件描述了Maven Git Commit Id插件的配置。
- 深入的配置選項(xiàng)描述了Maven Git Commit Id插件的每個(gè)配置選項(xiàng)。
第三 ,我們需要?jiǎng)?chuàng)建屬性文件,其中包含從Git存儲(chǔ)庫(kù)中提取的信息。 application.properties文件如下所示:
git.tags=${git.tags} git.branch=${git.branch} git.dirty=${git.dirty} git.remote.origin.url=${git.remote.origin.url}git.commit.id=${git.commit.id} git.commit.id.abbrev=${git.commit.id.abbrev} git.commit.id.describe=${git.commit.id.describe} git.commit.id.describe-short=${git.commit.id.describe-short} git.commit.user.name=${git.commit.user.name} git.commit.user.email=${git.commit.user.email} git.commit.message.full=${git.commit.message.full} git.commit.message.short=${git.commit.message.short} git.commit.time=${git.commit.time}git.build.user.name=${git.build.user.name} git.build.user.email=${git.build.user.email} git.build.time=${git.build.time}現(xiàn)在,我們已經(jīng)配置了Maven Git Commit Id插件。 編譯項(xiàng)目時(shí),將從application.properties文件中找到的屬性占位符替換為從Git存儲(chǔ)庫(kù)中提取的實(shí)際屬性值。
從target / classes目錄中找到的application.properties文件如下所示:
git.tags= git.branch=master git.dirty=true git.remote.origin.url=git@github.com:pkainulainen/spring-from-the-trenches.gitgit.commit.id=1bdfe9cf22b550a3ebe170f60df165e5c26448f9 git.commit.id.abbrev=1bdfe9c git.commit.id.describe=1bdfe9c-dirty git.commit.id.describe-short=1bdfe9c-dirty git.commit.user.name=Petri Kainulainen git.commit.user.email=petri.kainulainen@gmail.com git.commit.message.full=Declare PropertySourcesPlaceholderConfigurer in a static @Bean method git.commit.message.short=Declare PropertySourcesPlaceholderConfigurer in a static @Bean method git.commit.time=16.04.2015 @ 23:35:23 EESTgit.build.user.name=Petri Kainulainen git.build.user.email=petri.kainulainen@gmail.com git.build.time=18.04.2015 @ 17:07:55 EEST如果您不想創(chuàng)建屬性文件,則Maven Git Commit Id插件可以為您生成一個(gè) 。
讓我們繼續(xù)前進(jìn),找出如何將Git提交信息注入到屬性bean中。
將Git提交信息注入到屬性Bean中
我們需要?jiǎng)?chuàng)建以下描述的三個(gè)屬性bean類:
- BuildProperties類包含有關(guān)開始構(gòu)建的人的信息。
- CommitProperties類包含有關(guān)構(gòu)建中包含的最新提交的信息。
- GitProperties類包含一些“公共”屬性,例如branch , tags和remoteOriginUrl 。 它還包含對(duì)BuildProperties和CommitProperties對(duì)象的引用。
屬性Bean與以下內(nèi)容中描述的配置Bean相似
我以前的博客文章 。 我為這些類使用不同的后綴的原因是,它們不是Web應(yīng)用程序配置的一部分。 它們僅包含寫入日志文件并以JSON返回的信息。
另外,如果您不知道為什么要將屬性值注入特殊的bean類中以及如何將其值注入,則應(yīng)閱讀我的博客文章,該文章回答了這兩個(gè)問題 。
首先 ,我們需要?jiǎng)?chuàng)建BuildProperties類。 此類具有final time , userEmail和userName字段。 通過使用構(gòu)造函數(shù)注入將實(shí)際字段值注入到這些字段中。 BuildProperties類的源代碼如下所示:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public class BuildProperties {private final String time;private final String userEmail;private final String userName;@Autowiredpublic BuildProperties(@Value("${git.build.time}") String time,@Value("${git.build.user.email}") String userEmail,@Value("${git.build.user.name}") String userName) {this.time = time;this.userEmail = userEmail;this.userName = userName;}//Getters are omitted for the sake of clarity }其次 ,我們需要?jiǎng)?chuàng)建CommitProperties類。 此類具有最后的describe , describeShort , fullMessage , id , idAbbrev , shortMessage , time , userEmail和userName字段。 通過使用構(gòu)造函數(shù)注入將實(shí)際屬性值注入到這些字段中。 CommitProperties類的源代碼如下所示:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public class CommitProperties {private final String describe;private final String describeShort;private final String fullMessage;private final String id;private final String idAbbrev;private final String shortMessage;private final String time;private final String userEmail;private final String userName;@Autowiredpublic CommitProperties(@Value("${git.commit.id.describe}") String describe,@Value("${git.commit.id.describe-short}") String describeShort,@Value("${git.commit.message.full}") String fullMessage,@Value("${git.commit.id}") String id,@Value("${git.commit.id.abbrev}") String idAbbrev,@Value("${git.commit.message.short}") String shortMessage,@Value("${git.commit.time}") String time,@Value("${git.commit.user.email}") String userEmail,@Value("${git.commit.user.name}") String userName) {this.describe = describe;this.describeShort = describeShort;this.fullMessage = fullMessage;this.id = id;this.idAbbrev = idAbbrev;this.shortMessage = shortMessage;this.time = time;this.userEmail = userEmail;this.userName = userName;}//Getters are omitted for the sake of clarity }第三 ,我們需要?jiǎng)?chuàng)建GitProperties類。 此類具有最后的branch , build , commit , dirty , remoteOriginUrl和tags字段。 通過使用構(gòu)造函數(shù)注入將實(shí)際字段值(或?qū)ο?#xff09;注入這些字段。 GitProperties類的源代碼如下所示:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public class GitProperties {private String branch;private final BuildProperties build;private final CommitProperties commit;private final boolean dirty;private final String remoteOriginUrl;private final String tags;@Autowiredpublic GitProperties(@Value("${git.branch}") String branch,BuildProperties build,CommitProperties commit,@Value("${git.dirty}") boolean dirty,@Value("${git.remote.origin.url}") String remoteOriginUrl,@Value("${git.tags}") String tags) {this.branch = branch;this.build = build;this.commit = commit;this.dirty = dirty;this.remoteOriginUrl = remoteOriginUrl;this.tags = tags;}//Getters are omitted for the sake of clarity }讓我們繼續(xù)并將Git提交信息寫入日志文件。
將Git提交信息寫入日志文件
下一步是將Git提交信息信息寫入日志文件。 讓我們找出如何做到這一點(diǎn)。
首先 ,我們必須向BuildProperties , CommitProperties和GitProperties類添加toString()方法,并使用ToStringBuilder類實(shí)現(xiàn)這些方法。
BuildProperties類的源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public class BuildProperties {private final String time;private final String userEmail;private final String userName;@Autowiredpublic BuildProperties(@Value("${git.build.time}") String time,@Value("${git.build.user.email}") String userEmail,@Value("${git.build.user.name}") String userName) {this.time = time;this.userEmail = userEmail;this.userName = userName;}//Getters are omitted for the sake of clarity@Overridepublic String toString() {return new ToStringBuilder(this).append("time", this.time).append("userEmail", this.userEmail).append("userName", this.userName).toString();} }CommitProperties類的源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public class CommitProperties {private final String describe;private final String describeShort;private final String fullMessage;private final String id;private final String idAbbrev;private final String shortMessage;private final String time;private final String userEmail;private final String userName;@Autowiredpublic CommitProperties(@Value("${git.commit.id.describe}") String describe,@Value("${git.commit.id.describe-short}") String describeShort,@Value("${git.commit.message.full}") String fullMessage,@Value("${git.commit.id}") String id,@Value("${git.commit.id.abbrev}") String idAbbrev,@Value("${git.commit.message.short}") String shortMessage,@Value("${git.commit.time}") String time,@Value("${git.commit.user.email}") String userEmail,@Value("${git.commit.user.name}") String userName) {this.describe = describe;this.describeShort = describeShort;this.fullMessage = fullMessage;this.id = id;this.idAbbrev = idAbbrev;this.shortMessage = shortMessage;this.time = time;this.userEmail = userEmail;this.userName = userName;}//Getters are omitted for the sake of clarity@Overridepublic String toString() {return new ToStringBuilder(this).append("describe", this.describe).append("describeShort", this.describeShort).append("fullMessage", this.fullMessage).append("id", this.id).append("idAbbrev", this.idAbbrev).append("shortMessage", this.shortMessage).append("time", this.time).append("userEmail", this.userEmail).append("userName", this.userName).toString();} }GitProperties類的源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public class GitProperties {private String branch;private final BuildProperties build;private final CommitProperties commit;private final boolean dirty;private final String remoteOriginUrl;private final String tags;@Autowiredpublic GitProperties(@Value("${git.branch}") String branch,BuildProperties build,CommitProperties commit,@Value("${git.dirty}") boolean dirty,@Value("${git.remote.origin.url}") String remoteOriginUrl,@Value("${git.tags}") String tags) {this.branch = branch;this.build = build;this.commit = commit;this.dirty = dirty;this.remoteOriginUrl = remoteOriginUrl;this.tags = tags;}//Getters are omitted for the sake of clarity@Overridepublic String toString() {return new ToStringBuilder(this).append("branch", this.branch).append("build", this.build).append("commit", this.commit).append("dirty", this.dirty).append("remoteOriginUrl", this.remoteOriginUrl).append("tags", this.tags).toString();} }其次 ,我們必須在啟動(dòng)應(yīng)用程序時(shí)將Git提交信息寫入日志文件。 我們可以按照以下步驟進(jìn)行操作:
完成這些更改后, GitProperties類的源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component public class GitProperties {private static final Logger LOGGER = LoggerFactory.getLogger(GitProperties.class);private String branch;private final BuildProperties build;private final CommitProperties commit;private final boolean dirty;private final String remoteOriginUrl;private final String tags;@Autowiredpublic GitProperties(@Value("${git.branch}") String branch,BuildProperties build,CommitProperties commit,@Value("${git.dirty}") boolean dirty,@Value("${git.remote.origin.url}") String remoteOriginUrl,@Value("${git.tags}") String tags) {this.branch = branch;this.build = build;this.commit = commit;this.dirty = dirty;this.remoteOriginUrl = remoteOriginUrl;this.tags = tags;}//Getters are omitted for the sake of clarity@Overridepublic String toString() {return new ToStringBuilder(this).append("branch", this.branch).append("build", this.build).append("commit", this.commit).append("dirty", this.dirty).append("remoteOriginUrl", this.remoteOriginUrl).append("tags", this.tags).toString();}@PostConstructpublic void writeGitCommitInformationToLog() {LOGGER.info("Application was built by using the Git commit: {}", this);} }啟動(dòng)Web應(yīng)用程序時(shí),我們應(yīng)該從其日志文件中找到以下信息:
INFO - GitProperties - Application was built by using the Git commit: net.petrikainulainen.spring.trenches.config.GitProperties@47044f7d[branch=master,build=net.petrikainulainen.spring.trenches.config.BuildProperties@7b14c61[time=19.04.2015 @ 00:47:37 EEST,userEmail=petri.kainulainen@gmail.com,userName=Petri Kainulainen],commit=net.petrikainulainen.spring.trenches.config.CommitProperties@8fcc534[describe=1bdfe9c-dirty,describeShort=1bdfe9c-dirty,fullMessage=Declare PropertySourcesPlaceholderConfigurer in a static @Bean method,id=1bdfe9cf22b550a3ebe170f60df165e5c26448f9,idAbbrev=1bdfe9c,shortMessage=Declare PropertySourcesPlaceholderConfigurer in a static @Bean method,time=16.04.2015 @ 23:35:23 EEST,userEmail=petri.kainulainen@gmail.com,userName=Petri Kainulainen],dirty=true,remoteOriginUrl=git@github.com:pkainulainen/spring-from-the-trenches.git,tags= ]該信息寫在一行中,但是我對(duì)它進(jìn)行了格式化,因?yàn)槲蚁胧蛊涓子陂喿x。
讓我們了解如何將Git提交信息作為JSON返回。
以JSON形式返回Git提交信息
之前,我們創(chuàng)建了一個(gè)控制器類 , 該類將Web應(yīng)用程序的運(yùn)行時(shí)配置作為JSON返回。 讓我們修改此類以將Git提交信息作為JSON返回。 我們可以按照以下步驟進(jìn)行操作:
PropertiesController的源代碼如下所示:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;@RestController final class PropertiesController {private final ApplicationProperties applicationProperties;private final GitProperties gitProperties;@AutowiredPropertiesController(ApplicationProperties applicationProperties, GitProperties gitProperties) {this.applicationProperties = applicationProperties;this.gitProperties = gitProperties;}@RequestMapping(value = "/config", method = RequestMethod.GET)ApplicationProperties getAppConfiguration() {return applicationProperties;}@RequestMapping(value = "/version", method = RequestMethod.GET)GitProperties getVersion() {return gitProperties;} }當(dāng)我們將GET請(qǐng)求發(fā)送到url'/ version'時(shí),我們的控制器方法將返回以下JSON:
{"branch":"master","build":{"time":"19.04.2015 @ 00:47:37 EEST","userEmail":"petri.kainulainen@gmail.com","userName":"Petri Kainulainen"},"commit":{"describe":"1bdfe9c-dirty","describeShort":"1bdfe9c-dirty","fullMessage":"Declare PropertySourcesPlaceholderConfigurer in a static @Bean method","id":"1bdfe9cf22b550a3ebe170f60df165e5c26448f9","idAbbrev":"1bdfe9c","shortMessage":"Declare PropertySourcesPlaceholderConfigurer in a static @Bean method","time":"16.04.2015 @ 23:35:23 EEST","userEmail":"petri.kainulainen@gmail.com","userName":"Petri Kainulainen"},"dirty":true,"remoteOriginUrl":"git@github.com:pkainulainen/spring-from-the-trenches.git","tags":"" }我們不應(yīng)該允許所有人訪問我們應(yīng)用程序的Git提交信息。 如果這將是一個(gè)真實(shí)的應(yīng)用程序,我們應(yīng)確保只有管理員才能訪問此信息。
讓我們繼續(xù)并總結(jié)從這篇博客文章中學(xué)到的知識(shí)。
摘要
這篇博客文章教會(huì)了我們?nèi)?#xff1a;
- 我們可以使用Maven Git Commit Id插件從Git存儲(chǔ)庫(kù)中提取構(gòu)建時(shí)狀態(tài)。
- 我們可以通過重寫屬性bean類的toString()方法并將這些bean的屬性值注入到日志文件中后,將Git提交信息寫入日志文件。
- 通過創(chuàng)建返回“根”屬性bean對(duì)象( GitProperties )的控制器方法,我們可以將Git提交信息作為JSON返回。
- PS:您可以從Github獲得此博客文章的示例應(yīng)用程序 。
翻譯自: https://www.javacodegeeks.com/2015/04/spring-from-the-trenches-returning-git-commit-information-as-json.html
git hok json
總結(jié)
以上是生活随笔為你收集整理的git hok json_从战中反弹:将Git提交信息作为JSON返回的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是写作的特点? 写作的特点有哪些
- 下一篇: 海信 U8KL 系列 Mini LED