0

I am writing a custom Info Contributor. I have a properties file that is generated during the build process in target/classes folder. How Can I use this generated file in the custom info contributor.

I checked the below code for GitInfoContributor

public class GitInfoContributor extends InfoPropertiesInfoContributor<GitProperties> {

    public GitInfoContributor(GitProperties properties) {
        this(properties, Mode.SIMPLE);
    }

    public GitInfoContributor(GitProperties properties, Mode mode) {
        super(properties, mode);
    }

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("git", generateContent());
    }

    @Override
    protected PropertySource<?> toSimplePropertySource() {
        Properties props = new Properties();
        copyIfSet(props, "branch");
        String commitId = getProperties().getShortCommitId();
        if (commitId != null) {
            props.put("commit.id", commitId);
        }
        copyIfSet(props, "commit.time");
        return new PropertiesPropertySource("git", props);
    }

    /**
     * Post-process the content to expose. By default, well known keys representing dates
     * are converted to {@link Instant} instances.
     * @param content the content to expose
     */
    @Override
    protected void postProcessContent(Map<String, Object> content) {
        replaceValue(getNestedMap(content, "commit"), "time", getProperties().getCommitTime());
        replaceValue(getNestedMap(content, "build"), "time", getProperties().getInstant("build.time"));
    }

}

I am not able to figure out how the git properties are being injected to GitProperties class here ? I need to do the same for my custom info contributor using my properties file.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Sam
  • 1

1 Answers1

0

In addition to a git contributor, Spring boot also comes with a EnvironmentInfoContributor. This contributor automatically includes all application properties starting with info.*.

So the easiest solution is to make sure your custom properties all start with info.* and to treat your custom properties file as an additional application properties file. This can be done by providing the spring.config.additional-location VM argument. For example:

java -jar -Dspring.config.additional-location=classpath:custom-properties myApp.jar 

Now you only have to enable the environment info contributor by setting:

management.info.env.enabled=true

If this isn't possible, you can indeed write your own contributor. One way of doing so is by programmatically reading your properties and converting it to a Map<String, String>. Once that's done, you can call the Info.Builder.withDetails(..) method with your Map (see the source code of EnvironmentInfoContributor).

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • I am aware of this. What I am trying to achieve is to show all the other dependencies on which this project depends on , in the /actuator/info endpoint. Also I need to modify the values present in the custom properties file a bit before they can be directly used. – Sam Jun 16 '22 at 11:41
  • @Sam In that case you may want to check the last paragraph I added. You could read your properties-file programmatically, do your modifications, put it in a `Map` and call the `Info.Builder.withDetails()` method. – g00glen00b Jun 16 '22 at 11:43