Spring - 在自定义健康端点上显示 git 提交 ID

joh*_*ohn 9 spring

我试图在我的自定义运行状况端点上显示 Git 版本信息(分支、提交等)。

我尝试使用management.info.git.mode=full+git-commit-id-plugin但没有直接的方法将 git 信息提取到 Java 类中。如果有的话,这将是理想的方式。

我也在我的 Java 类中尝试了同样的git-commit-id-plugin值注释,@Value("${git.commit.id}")但 Spring 找不到属性值。我看到git.properties在目标目录中创建的文件。

我在这里缺少什么?提前致谢

Vel*_*aga 4

我们必须配置PropertyPlaceHolderConfigurer bean,以便我们能够访问插件生成的属性文件,请使用以下代码供您参考,

@Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propsConfig 
          = new PropertySourcesPlaceholderConfigurer();
        propsConfig.setLocation(new ClassPathResource("git.properties"));
        propsConfig.setIgnoreResourceNotFound(true);
        propsConfig.setIgnoreUnresolvablePlaceholders(true);
        return propsConfig;
    }
Run Code Online (Sandbox Code Playgroud)

然后在您的自定义健康检查类中,您可以使用,

@Value("${git.commit.id}") 私有字符串commitId;

我希望这能解决您的问题。