🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # git-commit-id-plugin ## 简介 [https://github.com/git-commit-id/maven-git-commit-id-plugin](https://github.com/git-commit-id/maven-git-commit-id-plugin) 将以下内容添加到*POM*文件的插件部分  : ~~~ <plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <version>2.2.4</version> <executions> <execution> <id>get-the-git-infos</id> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <dotGitDirectory>${project.basedir}/.git</dotGitDirectory> <prefix>git</prefix> <verbose>false</verbose> <generateGitPropertiesFile>true</generateGitPropertiesFile> <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename> <format>json</format> <gitDescribe> <skip>false</skip> <always>false</always> <dirty>-dirty</dirty> </gitDescribe> </configuration> </plugin> ~~~ 运行Maven build命令构建。在目录  target / classes中,  git.properties文件添加了JSON格式的版本信息 ~~~ { "git.branch" : "master", "git.build.host" : "LT0265", "git.build.time" : "2018-01-21T17:34:26+0100", "git.build.user.email" : "gunter@mydeveloperplanet.com", "git.build.user.name" : "Gunter Rotsaert", "git.build.version" : "1.0-SNAPSHOT", "git.closest.tag.commit.count" : "", "git.closest.tag.name" : "", "git.commit.id" : "6f592254e2e08d99a8145f1295d4ba3042310848", "git.commit.id.abbrev" : "6f59225", "git.commit.id.describe" : "6f59225-dirty", "git.commit.id.describe-short" : "6f59225-dirty", "git.commit.message.full" : "Created basic Spring Boot application with webservice for retrieving hard-coded version information", "git.commit.message.short" : "Created basic Spring Boot application with webservice for retrieving hard-coded version information", "git.commit.time" : "2018-01-21T17:33:13+0100", "git.commit.user.email" : "gunter@mydeveloperplanet.com", "git.commit.user.name" : "Gunter Rotsaert", "git.dirty" : "true", "git.remote.origin.url" : "https://github.com/mydeveloperplanet/mygitcommitidplanet.git", "git.tags" : "" } ~~~ 现在仔细看看  mygitcommitidplanet-1.0-SNAPSHOT.jar文件。在目录  BOOT-INF/ classes中,文件  git.properties可用。在这一点上,我们已经拥有了我们想要的:版本信息包含在我们的可交付成果中。我们总是可以查看  *JAR*文件来查找源代码的确切版本信息。 ## 将版本信息添加到REST风格的Web服务 下一步是将我们的REST风格的Web服务中的硬编码版本信息替换为git.properties文件的内容  。由于  git.properties文件已经是JSON格式,我们唯一要做的就是读取文件的内容并将其返回到我们的Web服务中。 我们的  VersionController.java 文件变成以下内容: ~~~ @RequestMapping(value = "/version", method = GET) public String versionInformation() { return readGitProperties(); } private String readGitProperties() { ClassLoader classLoader = getClass().getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("git.properties"); try { return readFromInputStream(inputStream); } catch (IOException e) { e.printStackTrace(); return "Version information could not be retrieved"; } } private String readFromInputStream(InputStream inputStream) throws IOException { StringBuilder resultStringBuilder = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = br.readLine()) != null) { resultStringBuilder.append(line).append("\n"); } } return resultStringBuilder.toString(); } ~~~ 使用Maven构建并运行应用程序。再次,转到URL` http://localhost:8080/version`。显示我们的*git.properties*文件的内容  。 这正是我们想要的:版本信息可以随时检索并始终保持最新。如果您有客户端应用程序,例如浏览器应用程序,则可以在关于部分中轻松使用此信息。 ## 验证git属性  当您想限制格式时,也可以将验证添加到  *git.properties*文件。如果构建不符合验证配置,构建将失败。我们现在要添加一个验证,以便在存储库变脏时让构建失败。 首先,我们在我们的*pom.xml *文件的*配置*部分  添加一个  *ValidationProperties*部分  : ~~~ <validationProperties> <!-- verify that the current repository is not dirty --> <validationProperty> <name>validating git dirty</name> <value>${git.dirty}</value> <shouldMatchTo>false</shouldMatchTo> </validationProperty> </validationProperties> ~~~ 其次,我们必须激活验证。这是在 git-commit-id插件的*执行*部分完成的  : ~~~ <execution> <id>validate-the-git-infos</id> <goals> <goal>validateRevision</goal> </goals> <phase>package</phase> </execution> ~~~ 我没有提交我对*pom.xml *文件所做的更改  ,所以我的存储库很脏。使用Maven构建应用程序。正如所料,构建失败并出现以下错误: ~~~ Validation 'validating git dirty' failed! Expected 'true' to match with 'false'! ~~~ 如果我们现在将*shouldMatchTo*更改  为  *true*并再次运行构建,则构建会成功。