How to load parameter values ​​from Java properties file?

Is there a way to dynamically load a parameter value from a properties file?

I mean, instead of hardcoding in build.sbt

 name := "helloWorld" 

Some application.properties files with

 name=helloWorld 

And then in the build.sbt file build.sbt enter name := application.properties["name"] (the last example is purely schematic, but I hope the idea is clear)

+7
sbt
source share
2 answers

You can create a configuration key that contains properties read from a file.

 import java.util.Properties val appProperties = settingKey[Properties]("The application properties") appProperties := { val prop = new Properties() IO.load(prop, new File("application.properties")) prop } name := appProperties.value.getProperty("name") 
+11
source share

Cheating on a little answer from @ daniel-olszewski.

In project/build.sbt declare a dependency on Configafe Config :

 libraryDependencies += "com.typesafe" % "config" % "1.2.1" 

In build.sbt load properties using Configes Config and set the settings:

 import com.typesafe.config.{ConfigFactory, Config} lazy val appProperties = settingKey[Config]("The application properties") appProperties := { ConfigFactory.load() } name := { try { appProperties.value.getString("name") } catch { case _: Exception => "<empty>" } } 

You can define def , which also set values ​​from properties.

+5
source share

All Articles