'Spring Boot - get current configuration file path in runtime

I need to get absolute path to current active configuration file in Spring boot that don't locate in classpath or resources

It can be locate in default place - project folder, subfolder "config", set via spring.config.location and in random place, also in another disk

Something like "E:\projects\configs\myProject\application.yml"



Solution 1:[1]

Someday I found same question here, but cant find it now

So here my solution, maybe someone needs it

    @Autowired
    private ConfigurableEnvironment env;
    private static final String YAML_NAME = "application.yml";

    private String getYamlPath() throws UnsupportedEncodingException {
        String projectPath = System.getProperty("user.dir");
        String decodedPath = URLDecoder.decode(projectPath, "UTF-8");

        //Get all properies
        MutablePropertySources propertySources = env.getPropertySources();

        String result = null;
        for (PropertySource<?> source : propertySources) {
            String sourceName = source.getName();
            
            //If configuration loaded we can find properties in environment with name like
            //"Config resource '...[absolute or relative path]' via ... 'path'"
            //If path not in classpath -> take path in brackets [] and build absolute path

            if (sourceName.contains("Config resource 'file") && !sourceName.contains("classpath")) {
                String filePath = sourceName.substring(sourceName.indexOf("[") + 1, sourceName.indexOf("]"));
                if (Paths.get(filePath).isAbsolute()) {
                    result = filePath;
                } else {
                    result = decodedPath + File.separator + filePath;
                }
                break;
            }
        }

        //If configuration not loaded - return default path
        return result == null ? decodedPath + File.separator + YAML_NAME : result;
    }

Not the best solution I suppose but it works

If you have any idea how to improve it I would be really appreciate it

Solution 2:[2]

Assume that you have these application-{env}.yml config profiles in resources folder, and we are going to activate the dev configuration.

application.yml
application-dev.yml
application-prod.yml
application-test.yml 
...

There are two ways you can activate the dev :

  1. modify your application.yml,
spring:
  profiles:
    active: dev
  1. or by command line when you want to start your application :
java -jar -Dspring.profiles.active=dev application.jar

Then, try this code in your program:

    // get the active config dynamically
    @Value("${spring.profiles.active}")
    private String activeProfile;

    public String readActiveProfilePath() {
        try {
            URL res = getClass().getClassLoader().getResource(String.format("application-%s.yml", activeProfile));
            if (res == null) {
                res = getClass().getClassLoader().getResource("application.yml");
            }
            File file = Paths.get(res.toURI()).toFile();
            return file.getAbsolutePath();
        } catch (Exception e) {
            // log the error.
            return "";
        }
    }

The output will be an absolute path of application-dev.yml

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2