简体   繁体   English

强制 pureconfig 使用 -dconfig.file application.conf 文件而不是资源 application.conf

[英]force pureconfig to use -dconfig.file application.conf file and not resource application.conf

I have the following application.conf file:我有以下 application.conf 文件:

# App
tables = [
    "table_1",
    "talbe_2"
]

and the following scala code:以及以下 Scala 代码:

import pureconfig._
import pureconfig.generic.auto._
import com.typesafe.config.ConfigFactory
import pureconfig.generic.ProductHint

case class Configuration(tables: Array[String])

object Config {
  implicit def hint[A] = ProductHint[A](ConfigFieldMapping(CamelCase, CamelCase))

  val conf = ConfigSource.fromConfig(ConfigFactory.load("application.conf")).load[Configuration] match {
    case Right(conf) => conf
    case Left(failures) => throw new Exception(s"Unable to load the configuration ${failures.toList.mkString("\n")}")
  }
}

and to test the application.conf, I have this code:并测试application.conf,我有这个代码:

object Test extends App {

  val res = Config.conf.tables
  println("hello")
}

when I drop application.conf in resources folder, it works.当我将 application.conf 放入资源文件夹时,它可以工作。 But It does not works when i use但是当我使用时它不起作用

-Dconfig.file=C:/Users/path/to/application.conf

It use the resources application.conf file despite the one in argument -Dconfig.file.尽管参数 -Dconfig.file 中有一个,但它使用资源 application.conf 文件。 Do you have any idea?你有什么主意吗?

The problem is the call问题是调用

ConfigFactory.load("application.conf")

From scaladocs来自 Scaladocs

Loads an application's configuration from the given classpath resource or classpath resource basename, sandwiches it between default reference config and default overrides, and then resolves it.从给定的类路径资源或类路径资源基名加载应用程序的配置,将其夹在默认引用配置和默认覆盖之间,然后解析它。

You have to use你必须使用

ConfigFactory.load()

which哪一个

Loads a default configuration, equivalent to load(defaultApplication())加载一个默认配置,相当于 load(defaultApplication())

And defaultApplication supports config.filedefaultApplication支持config.file

If the system properties config.resource, config.file, or config.url are set, then the classpath resource, file, or URL specified in those properties will be used rather than the default application.{conf,json,properties} classpath resources.如果设置了系统属性 config.resource、config.file 或 config.url,则将使用这些属性中指定的类路径资源、文件或 URL,而不是默认的 application.{conf,json,properties} 类路径资源.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM