Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala SBT Project Application Configuration File

I come from the C# .NET world and as I build my Scala project I noticed how many different ways there are to do things in the open source community. Anyways in .Net C# project we have the Web.config or the App.config file which is very useful to specify app keys that are used globally and it is also helpful for transformation for different environments and such.

What would be the equivalent for a Scala SBT project? Do you know a place to store your connection strings and other app settings?

like image 380
Francisco Arias Avatar asked Aug 13 '15 21:08

Francisco Arias


2 Answers

There is no universal configuration file name. The basic way is to use Properties, see e.g. how to read properties file in scala, and call the file your-application-name.properties. But there is a popular library called simply Config which I'd recommend; as https://github.com/typesafehub/config#standard-behavior says, its standard names for config files are

The convenience method ConfigFactory.load() loads the following (first-listed are higher priority):

  • system properties
  • application.conf (all resources on classpath with this name)
  • application.json (all resources on classpath with this name)
  • application.properties (all resources on classpath with this name)
  • reference.conf (all resources on classpath with this name)

The idea is that libraries and frameworks should ship with a reference.conf in their jar. Applications should provide an application.conf , or if they want to create multiple configurations in a single JVM, they could use ConfigFactory.load("myapp") to load their own myapp.conf . (Applications can provide a reference.conf also if they want, but you may not find it necessary to separate it from application.conf .)

As @ashalynd's answer says, in order for any such library to see your configuration files, they should go into src/{main,test}/resources.

like image 136
Alexey Romanov Avatar answered Sep 22 '22 13:09

Alexey Romanov


I think you are talking about application.conf. It can be found in /src/main/resources or in /src/test/resources (affecting main and test settings, respectively). If application.conf is not present in /src/test/resources, the main application.conf will be used for the tests.

like image 32
Ashalynd Avatar answered Sep 21 '22 13:09

Ashalynd