如何从类路径而不是顶级资源中加载conf文件

如何从类路径而不是顶级资源中加载conf文件

本文介绍了Scala TypeSafe配置-如何从类路径而不是顶级资源中加载conf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个项目中使用scala typesafe config(1.2.1版)来读取application.conf文件,我的项目具有对多个其他项目的依赖关系,并且创建了一个具有依赖关系的jar来运行依赖项项目.

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.

问题-这些项目也使用类型安全并且在顶层目录中有application.conf文件,而我的具有依赖项的maven jar在同一类路径中仅拾取一个application.conf并将其其余部分丢弃(我尝试使用maven shade plugin进行合并这些conf文件,我不想走那条路).我正在尝试将这些application.conf文件放在程序包中,因此它们不会在具有依赖关系的jar中被覆盖,但是typesafe似乎无法识别资源目录中程序包名称下的文件,它只能找到conf文件.如果放置在顶级资源目录下.

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.

是否有更好的解决方案通过使用typesafe来实现?或者我愿意对scala使用完全不同的配置库(如果有的话).

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.

推荐答案

给出这样的文件结构:

$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")

这篇关于Scala TypeSafe配置-如何从类路径而不是顶级资源中加载conf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:23