1

I have a Spring boot application that defines some endpoints in the application.yml config file. The YAML contains multiple documents, one for each profile.

I would like to run a Gradle task before starting the application. The Gradle task is passed the profile as an argument. It should use the endpoints defined in the configuration for that profile.

How can I load the value from the configuration without loading the entire Spring framework?

Example

# src/main/resources/application.yml
endpoints: 
  foo: localhost:12345/foo
  bar: localhost:12345/bar

---
spring.profiles: staging
endpoints:
  foo: dev.foo.com/api
  bar: dev.bar.com/api

--- 
spring.profiles: production
endpoints:
  foo: foo.com/api
  bar: bar.com/api
public class Task {
    public static void main(String[] args) {
        String profile = arg[0];

        // TODO load endpoints from src/main/resources/application.yml
        Map<String, Object> endpoints = ...
        System.out.println(endpoints.get("foo");
    }
}
Brett Y
  • 7,171
  • 1
  • 28
  • 42
  • Parse it and pull out the data you want? – Dave Newton Feb 28 '21 at 14:53
  • Does this answer your question? [How do I parse a YAML file in groovy?](https://stackoverflow.com/questions/41731059/how-do-i-parse-a-yaml-file-in-groovy) – mentallurg Feb 28 '21 at 15:02
  • Yes basically I want to parse it and pull the data out but parsing it is difficult because of the documents with the single file. – Brett Y Feb 28 '21 at 15:04
  • I'm not sure what the issue is. Can you be more specific? – Dave Newton Feb 28 '21 at 15:26
  • The issue is that as far as I can tell there is no straight forward way to load properties from a YAML file that contains multiple documents. – Brett Y Feb 28 '21 at 15:35
  • And even once the YAML has been loaded we still need to handle the profile resolution. – Brett Y Feb 28 '21 at 15:37
  • SnakeYAML has `loadAll` to load multiple documents in the same file. Not sure what you mean by profile resolution. – flyx Mar 01 '21 at 11:06

0 Answers0