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

1.4k views Asked by At

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

There are 1 answers

0
Ivan Stanislavciuc On BEST ANSWER

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.