简体   繁体   中英

force pureconfig to use -dconfig.file application.conf file and not resource 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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