javascript
从战中反弹:将Git提交信息作为JSON返回
在某些情況下,我們必須知道部署到遠程服務器的Web應用程序的確切版本。 例如,客戶可能想知道我們是否已經在服務器X上部署了錯誤修復程序。
 當然,我們可以嘗試使用“傳統”方法找到該問題的答案。 問題是: 
- 沒有人不記得是誰更新了服務器X或何時更新了服務器X。
- 更新它的人不記得哪個是構建中包含的最后一次提交。
換句話說,我們被搞砸了。 我們可以嘗試測試服務器X上是否仍然存在該錯誤,但這并不能真正幫助我們,因為我們的錯誤修復可能無法正常工作。
這篇博客文章描述了我們如何解決這個問題。 讓我們從提取Git存儲庫的構建時狀態開始。
如果使用Spring Boot,則應使用Spring Boot Actuator 。 它可以幫助您發布有關Git存儲庫狀態的信息 。
如果您尚未閱讀以下博客文章, 則應先閱讀它們,然后再繼續閱讀此博客文章 :
- 從槽中彈起:將屬性值注入配置Bean描述了為什么應將屬性值注入配置Bean并幫助您做到這一點。
- 從槽中彈跳:以JSON返回運行時配置描述了如何將Web應用程序的運行時配置寫入日志文件并將其作為JSON返回。
提取我們的Git倉庫的構建時間狀態
我們可以使用Maven Git Commit Id插件提取Git存儲庫的構建時狀態。 讓我們了解如何配置Maven Git Commit Id插件并將提取的信息添加到屬性文件中。
首先 ,我們需要配置資源目錄的位置,并確保將從屬性文件中找到的屬性占位符替換為實際的屬性值。 為此,我們可以將以下XML添加到pom.xml文件的build部分中:
<resources><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>**/*.properties</include></includes></resource> </resources>其次 ,我們需要配置Maven Git Commit Id插件。 我們可以按照以下步驟進行操作:
我們需要將以下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>如果您對Maven Git Commit Id插件的默認配置不滿意,則應仔細閱讀其自述文件:
- 使用該插件提供了帶注釋的XML配置文件,該文件描述了Maven Git Commit Id插件的配置。
- 深入的配置選項描述了Maven Git Commit Id插件的每個配置選項。
第三 ,我們需要創建屬性文件,其中包含從Git存儲庫中提取的信息。 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}現在,我們已經配置了Maven Git Commit Id插件。 編譯項目時,將從application.properties文件中找到的屬性占位符替換為從Git存儲庫中提取的實際屬性值。
從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如果您不想創建屬性文件,則Maven Git Commit Id插件可以為您生成一個 。
讓我們繼續前進,找出如何將Git提交信息注入到屬性bean中。
將Git提交信息注入到屬性Bean中
我們需要創建以下描述的三個屬性bean類:
- BuildProperties類包含有關開始構建的人的信息。
- CommitProperties類包含有關構建中包含的最新提交的信息。
- GitProperties類包含一些“公共”屬性,例如branch , tags和remoteOriginUrl 。 它還包含對BuildProperties和CommitProperties對象的引用。
 屬性Bean與中描述的配置Bean相似 
 我以前的博客文章 。 我為這些類使用不同的后綴的原因是,它們不是Web應用程序配置的一部分。 它們僅包含寫入日志文件并以JSON返回的信息。 
另外,如果您不知道為什么要將屬性值注入特殊的bean類中以及如何將其值注入,則應閱讀我的博客文章,該文章回答了這兩個問題 。
首先 ,我們需要創建BuildProperties類。 此類具有final time , userEmail和userName字段。 通過使用構造函數注入將實際字段值注入到這些字段中。 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 }其次 ,我們需要創建CommitProperties類。 該類具有最后的describe , describeShort , fullMessage , id , idAbbrev , shortMessage , time , userEmail和userName字段。 通過使用構造函數注入將實際屬性值注入到這些字段中。 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 }第三 ,我們需要創建GitProperties類。 此類具有最后的branch , build , commit , dirty , remoteOriginUrl和tags字段。 通過使用構造函數注入將實際字段值(或對象)注入這些字段。 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 }讓我們繼續并將Git提交信息寫入日志文件。
將Git提交信息寫入日志文件
我們的下一步是將Git提交信息信息寫入日志文件。 讓我們找出如何做到這一點。
首先 ,我們必須向BuildProperties , CommitProperties和GitProperties類添加toString()方法,并使用ToStringBuilder類實現這些方法。
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();} }其次 ,我們必須在啟動應用程序時將Git提交信息寫入日志文件。 我們可以按照以下步驟進行操作:
完成這些更改后, 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);} }啟動Web應用程序時,應從其日志文件中找到以下信息:
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= ]該信息寫在一行中,但是我對其進行了格式化,因為我想使其更易于閱讀。
讓我們了解如何將Git提交信息作為JSON返回。
以JSON形式返回Git提交信息
之前,我們創建了一個控制器類 , 該類將Web應用程序的運行時配置作為JSON返回。 讓我們修改此類以將Git提交信息返回為JSON。 我們可以按照以下步驟進行操作:
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;} }當我們將GET請求發送到url'/ version'時,我們的控制器方法將返回以下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":"" }我們不應該允許所有人訪問我們應用程序的Git提交信息。 如果這將是一個真實的應用程序,則應確保只有管理員才能訪問此信息。
讓我們繼續并總結從這篇博客文章中學到的知識。
摘要
這篇博客文章教會了我們三件事:
- 我們可以使用Maven Git Commit Id插件從Git存儲庫中提取構建時狀態。
- 我們可以通過重寫屬性bean類的toString()方法并將這些bean的屬性值注入到日志文件中后,將Git提交信息寫入日志文件。
- 我們可以通過創建一個返回“根”屬性bean對象( GitProperties )的控制器方法來將Git提交信息作為JSON返回。
- PS:您可以從Github獲得此博客文章的示例應用程序 。
翻譯自: https://www.javacodegeeks.com/2015/04/spring-from-the-trenches-returning-git-commit-information-as-json.html
總結
以上是生活随笔為你收集整理的从战中反弹:将Git提交信息作为JSON返回的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 学校电脑管理员权限(电脑使用管理员权限)
- 下一篇: 21世纪的设计模式:抽象工厂模式
