Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load setting values from a Java properties file?

Tags:

sbt

Is there a way for me to dynamically load a setting value from a properties file?

I mean, instead of hardcoding into build.sbt

name := "helloWorld"

Have some application.properties file with

name=helloWorld

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

like image 472
Gilad Avatar asked Sep 04 '14 12:09

Gilad


People also ask

How do you get the data from properties file in Java?

Each key and its corresponding value in the property list is a string. The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load .

How fetch data from properties file?

To get information from the properties file, create the properties file first. Now, let's create the java class to read the data from the properties file. Now if you change the value of the properties file, you don't need to recompile the java class. That means no maintenance problem.

How set value in properties file in Java?

To set properties in a Java Properties instance you use the setProperty() method.


1 Answers

You can create a setting key which holds 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")
like image 83
Daniel Olszewski Avatar answered Oct 23 '22 22:10

Daniel Olszewski