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

huangapple go评论67阅读模式
英文:

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

问题

以下是翻译好的内容:

我有以下的application.conf文件:

# 应用程序
tables = [
    "table_1",
    "table_2"
]

以及以下的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"无法加载配置 ${failures.toList.mkString("\n")}")
  }
}

为了测试application.conf,我有以下代码:

object Test extends App {

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

当我将application.conf放在资源文件夹中时,它可以正常工作。但是当我使用

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

时,它不起作用。尽管使用了-Dconfig.file参数,它仍然使用资源文件夹中的application.conf文件。你有任何想法吗?

英文:

I have the following application.conf file:

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

and the following scala code:

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:

object Test extends App {

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

when I drop application.conf in resources folder, it works. 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.
Do you have any idea?

答案1

得分: 3

问题在于调用

ConfigFactory.load("application.conf")

来自 scaladocs

> 从给定的类路径资源或类路径资源基名加载应用程序的配置,
> 将其夹在默认引用配置和默认覆盖配置之间,然后进行解析。

您需要使用

ConfigFactory.load()

它等同于

> 加载默认配置,相当于
> load(defaultApplication())

defaultApplication 支持 config.file

> 如果设置了系统属性 config.resource、config.file 或 config.url,
> 则将使用这些属性中指定的类路径资源、文件或 URL,
> 而不是默认的 application.{conf,json,properties} 类路径资源。

英文:

The problem is the call

ConfigFactory.load("application.conf")

From 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())

And defaultApplication supports 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.

huangapple
  • 本文由 发表于 2020年9月30日 04:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64127047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定