Packaging configurations outside Jars with SBT Native Plugin
In this post we are going to see how we can separately package any files (especially config and log configuration) out of packaged jar when we use sbt native packager for packaging our scala application. Here we are building from our last post.
In order to use sbt native packager; first we need to add the plugin in our packages configuration file (packages.sbt) under project folder.
Now we need to enable the plugin. Here JavaAppPackaging would package up jar files for our application. We can also notice a couple of mappings configurations. They copy application.dev.conf and logback.dev.xml files from resources folder to conf folder in the resulting package.
We have very simple configurations for our application.conf and application.dev.conf. We are simply keeping config.environment configuration. It has been assigned Debug and Dev values for debug and dev environments.
In order to demonstrate what configuration file is being used; here we are just pulling up config.environment configuration and logging it. Here we are using typesafe configs for handling with configuration files. If we simply run this code in IntelliJ debugger, you can notice that it pulls up application.conf file by default, hence writing Debug message to the logs.
Let’s simply package it using sbt. Here we are running sbt universal:packageBin to package it.
And this is how it packages it. You can notice that it has copied application.dev.conf and logback.dev.xml separately in conf folder.
Let’s simple run it using. We can notice that it has picked up application.conf file for configuration and has written Debug to the logs.
We can override configuration file using -Dconfig.file argument. We can also override logback configuration file using -Dlogback.configurationFile argument.
But below we have just overridden the configuration file. You can notice that it has written Dev to the logs. This shows that it has correctly picked up application.dev.conf file as specified in the startup argument.