0

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 located 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"

EoinKanro
  • 105
  • 1
  • 7

2 Answers2

0

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

So here my solution, maybe someone needs it

    @Autowired
    private ConfigurableEnvironment env;

    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 really appreciate it

EoinKanro
  • 105
  • 1
  • 7
0

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

Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
  • I thought about it. We can try find file recursively in project folder with "*.yml". But who says that the config is active?... My first solution was hardcode - config file near jar file. But I found dynamic way – EoinKanro Apr 08 '22 at 06:52
  • I have improved my answer. I hope it helps – Kai-Sheng Yang Apr 08 '22 at 07:42