Using config file absolutely make your life easier to deploy, debug, and test your applications. Sometimes people want to use different config file to run dev or run prod, Sometimes people just want to use different config to test.
So, I’m going to show how to use multiple config files in sbt project. In this post, I create minimal project by using typesafe’s activator, and I strongly command this tool to create small and clean project with least dependency to train skills.
Normal way to use config in sbt project
Firstly, create simple sbt Scala project. I created it by using activator:
And then follow the command wizard to choose
minimal-scala as your project type, and fill up the project name.
To load config file, you will need one extra dependency: typesafe’s config.
And following to the
build.sbt file under the root directory of the project.
build.sbt file should looks like:
Ok, by now, you could use a module named ConfigFactory in your project to load your config files.
It’s time to add your own config file.
application.config under your
src/main/resources, and write some random configs into it.
Here is mine:
test.text1="1" test.text2="2" test.text3="3"
To check whether this config file could be properly loaded, write a simple program to check it out:
When run this script, it will give:
Use config in your test
In most cases, people use a set of different configs in their tests.
It would be great if one can just replace some of the configs in the
src/main/resources/application.conf and keep the others.
Actually, when run
sbt test, sbt will first look for
src/main/resources/application.config and then use
src/test/resources/application.confg to overwrite configs in the first one.
Note: sbt will do the overwrite even if you not use
include "application.config"in the
To check that, add new test case under
src/test/scala in your project:
Then add config file for the test as
When you run
sbt test you will get this as expected:
1 22 3
Use specific config file
Normally, you can specify the config file by use sbt parameter
-Dconfig.resource.(for detailed information please refer to this doc)
But in team work, you probably want this to be static, and use the file you have specified whenever and whoever run
In that case, you need to put a extra in your
And then, put your test.conf to the
Note that in this time you will have to use
And this time when you run
sbt test, you will get:
1 22 333
That is because the sequence of config files overwrite each others is:
src/test/resources/test.conf src/test/resources/application.conf src/main/resources/application.conf
Note that no matter you like or not, once you include the “application.conf”,
the sbt will first try to find
If that does not exist, then it will find
Even if you delete the
src/main/resources/application.conf is still in the system.
Here is the result if there is no
src/test/resources/application.conf in the project:
1 2 333