scala typesafe config - How to load conf file from

2019-07-12 11:28发布

问题:

I am using scala typesafe config (version 1.2.1) in one of my projects to read the application.conf file, my project has dependency on multiple other projects and I create a jar with dependencies to run the dependency projects.

Problem - those projects also use typesafe and has application.conf files in top level directory and my maven jar with dependencies pick up only one application.conf in same classpath and drops rest of them (I tried using maven shade plugin to merge these conf files and I don't want to go that route). I am trying to place these application.conf files in packages, so they don't get overriden in jar with dependencies, but typesafe doesn't seem to recognize the files under a package name in resources directory, it can only find the conf file if placed under top level resources directory.

Is there a better solution to achieve this by using typesafe ? or I am open to using a totally different config library for scala if there is any.

回答1:

Given a file structure like this:

$tree src/main/resources/
 src/main/resources
 └── path
     └── to
         └── file
             └── xxx.conf

You can load the config with the following command:

val config = ConfigFactory.load("path/to/file/xxx.conf")