1. 概述
在本教程中,我们将展示如何将 Git 仓库信息注入到基于 Maven 构建的 Spring Boot 应用程序中。
为此,我们将使用 maven-git-commit-id-plugin——一个专为此目的创建的便捷工具。
2. Maven 依赖
让我们在项目的 pom.xml 文件中的
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.1</version>
</plugin>
你可以在这里找到最新版本。请记住,此插件至少需要 Maven 3.1.1 版本。
注意,此插件有一个更新的重定位版本(5.x 或更高版本)在不同的仓库坐标中可用。但是,该版本需要 Java 11。由于我们将使用 Spring Boot 2.x 开发示例应用程序,其基线是 Java 8,因此我们需要使用旧版本的插件。这使我们可以维护 Spring Boot 和 git-commit-id-plugin 之间的兼容性。
3. 配置
该插件有许多方便的标志和属性来扩展其功能。在本节中,我们将简要描述其中一些。如果你想了解所有这些,请访问 maven-git-commit-id-plugin 的页面,如果你想直接看示例,请转到第 4 节。
以下代码片段包含插件属性的示例;根据你的需要在
3.1. 缺少仓库
如果找不到 Git 仓库,你可以配置它以省略错误:
<failOnNoGitDirectory>false</failOnNoGitDirectory>
3.2. Git 仓库位置
如果你想指定自定义的 .git 仓库位置,使用 dotGitDirectory 属性:
<dotGitDirectory>${project.basedir}/submodule_directory/.git</dotGitDirectory>
3.3. 输出文件
为了生成具有自定义名称和/或目录的属性文件,使用以下部分:
<generateGitPropertiesFilename>
${project.build.outputDirectory}/filename.properties
</generateGitPropertiesFilename>
3.4. 详细程度
为了获得更详细的日志记录,使用:
<verbose>true</verbose>
3.5. 属性文件生成
你可以关闭 git.properties 文件的创建:
<generateGitPropertiesFile>false</generateGitPropertiesFile>
3.6. 属性前缀
如果你想指定自定义属性前缀,使用:
<prefix>git</prefix>
3.7. 仅适用于父仓库
当处理带有子模块的项目时,设置此标志确保插件仅适用于父仓库:
<runOnlyOnce>true</runOnlyOnce>
3.8. 属性排除
你可能想要排除一些敏感数据,如仓库用户信息:
<excludeProperties>
<excludeProperty>git.user.*</excludeProperty>
</excludeProperties>
3.9. 属性包含
只包含指定的数据也是可能的:
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
</includeOnlyProperties>
4. 示例应用程序
让我们创建一个示例 REST 控制器,它将返回有关我们项目的基本信息。
我们将使用 Spring Boot 创建示例应用程序。如果你不知道如何设置 Spring Boot 应用程序,请参阅介绍性文章:配置 Spring Boot Web 应用程序。
我们的应用程序将包含 2 个类:Application 和 CommitIdController
4.1. 应用程序
CommitIdApplication 将作为我们应用程序的根:
@SpringBootApplication(scanBasePackages = { "com.baeldung.git" })
public class CommitIdApplication {
public static void main(String[] args) {
SpringApplication.run(CommitIdApplication.class, args);
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer propsConfig
= new PropertySourcesPlaceholderConfigurer();
propsConfig.setLocation(new ClassPathResource("git.properties"));
propsConfig.setIgnoreResourceNotFound(true);
propsConfig.setIgnoreUnresolvablePlaceholders(true);
return propsConfig;
}
}
除了配置我们应用程序的根之外,我们还创建了 PropertyPlaceHolderConfigurer bean,以便我们能够访问插件生成的属性文件。
我们还设置了一些标志,以便即使 Spring 无法解析 git.properties 文件,应用程序也能顺利运行。
4.2. 控制器
@RestController
public class CommitInfoController {
@Value("${git.commit.message.short}")
private String commitMessage;
@Value("${git.branch}")
private String branch;
@Value("${git.commit.id}")
private String commitId;
@RequestMapping("/commitId")
public Map<String, String> getCommitId() {
Map<String, String> result = new HashMap<>();
result.put("Commit message",commitMessage);
result.put("Commit branch", branch);
result.put("Commit id", commitId);
return result;
}
}
如你所见,我们正在将 Git 属性注入到类字段中。
要查看所有可用属性,请参考 git.properties 文件或作者的 Github 页面。我们还创建了一个简单的端点,在 HTTP GET 请求时,它将返回包含注入值的 JSON。
4.3. Maven 条目
我们将首先设置插件要执行的执行步骤,以及任何其他我们认为有用的配置属性:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
<execution>
<id>validate-the-git-infos</id>
<goals>
<goal>validateRevision</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- ... -->
</configuration>
</plugin>
为了使我们的代码正常工作,我们需要在类路径中最终得到一个 git.properties 文件。为了实现这一点,我们有两个选择。
第一个是让插件生成文件。我们可以通过设置 generateGitPropertiesFile 配置属性为 true 值来指定这一点:
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
</configuration>
第二个选择是我们自己在资源文件夹中包含一个 git.properties 文件。我们可以只包含我们将在项目中使用的条目:
# git.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.closest.tag.name=${git.closest.tag.name}
git.closest.tag.commit.count=${git.closest.tag.commit.count}
git.build.user.name=${git.build.user.name}
git.build.user.email=${git.build.user.email}
git.build.time=${git.build.time}
git.build.host=${git.build.host}
git.build.version=${git.build.version}
Maven将替换占位符为适当的值。
注意:某些 IDE 在与此插件配合使用时可能不太正常,并且在我们如上所述定义属性时可能会在启动时抛出"循环占位符引用"错误。
启动并请求 localhost:8080/commitId 后,你可以看到一个结构类似于以下的 JSON 文件:
{
"Commit id":"7adb64f1800f8a84c35fef9e5d15c10ab8ecffa6",
"Commit branch":"commit_id_plugin",
"Commit message":"Merge branch 'master' into commit_id_plugin"
}
5. 与 Spring Boot Actuator 集成
你可以轻松地将插件与 Spring Actuator 一起使用。
正如你在文档中可以读到的,GitInfoContributor 将获取 git.properties 文件(如果可用)。因此,使用默认插件配置,调用 /info 端点时将返回 Git 信息:
{
"git": {
"branch": "commit_id_plugin",
"commit": {
"id": "7adb64f",
"time": "2016-08-17T19:30:34+0200"
}
}
}
6. 总结
在本教程中,我们展示了使用 maven-git-commit-id-plugin 的基础知识,并创建了一个简单的 Spring Boot 应用程序,该应用程序使用插件生成的属性。
展示的配置并未涵盖所有可用的标志和属性,但它涵盖了开始使用此插件所需的所有基础知识。