how to create different builds using sbt based on environment? - scala

I am using sbt for my scala project, i want to create build for different environment like local, development, stage and production. these build will have different property file, log4j file and token files. So to handle this i need different folders with files under src/main/resources like local, dev, stage and prod. Based on the build environment it should pick files from respective folders. Can you guide me to do this using sbt.

Instead of create different files use environment variables inside https://github.com/typesafehub/config , for instance:
akka {
loglevel = "DEBUG"
loglevel = ${?LOGLEVEL}
}
If LOGLEVEL is not set "DEBUG" is the default config. Anyway, if you want to include different files making jars you can use different configuration for the sbt-assembly plugging.

Related

Scala, Docker - how to choose configuration for different environments?

I have created a simple application with DockerPlugin in sbt file and it uses my default application.conf file to load resources. But now, I will need to change this configurations depend on environment (other for dev, other one for prod). Is it possible to tell docker which configuration file should be used? I tried to separate application.conf data into prod and dev and load files in case of given environment in path, but I don't think so it is good solution.

Using external conf folder as resource folder with sbt-native-packager

I'm pretty new to sbt-native-packager. What I want is using external conf folder like a resource folder and configure the files inside it differently on each environment such as production, dev and etc.
Project has src/universal/conf and bunch of configuration files under this directory used by libraries at runtime.
I exclude src/universal/conf during build so final jar does not has these conf files as resources. I checked bin/start.sh and only lib/ folder set as classpath.
How can I accomplish it? What's the best practices? I don't think that JavaServerPackaging is more convenient because final jar has several main methods to be used as separate applications on same node. So one dameon application looks like doesn't fit.
There are already a few questions with proper solutions for this
SBT using sbt-native-packager how to create different build files?
What is the recommended way to set JVM options...

How to properly manage logback configrations in development and production using SBT & Scala?

I have a pretty standard Scalatra project using Logback for logging.
Following the logback manual I have added a logback-test.xml for my development configuration (logs of debugs), whilst maintaining a production logback.xml.
However, in development while using the xsbt-web-plugin to run a container with code reloading, my app only seems to pick up the logback.xml.
How do I get the desired behavior?:
In development mode (./sbt container:start) the app uses logback-test.xml
When assembled into a zip using SBT-assembly, exclude the test config.
Currently neither of these seems to work.
You're misusing logback-test.xml. It's intended for unit-like tests only and should be placed in src/test/resources (which is automatically excluded from prod). To achieve what you want - you may set up path to your logback-dev.xml by system property:
javaOptions in container += "-Dlogback.configurationFile=/some/path/logback-dev.xml"
This path may be relative. See, https://stackoverflow.com/a/26538449/1809978
In my practice we don't pack logback.xml even in prod (it's pointed to some external place) to have ability to change logging configuration ad-hoc.
P.S. If you're also interested about excluding files from sbt-assembly - this may help

Swapping out .properties files for different environments

I have a Play 2 application that uses some external Java libraries. Some of these (e.g. the Paypal merchant sdk) depend on having a properties file for configuration (e.g. sdk_config.properties) which I have put into the conf directory.
When deploying this application to a different environment using play dist is there a sensible way to swap out properties files for each environment? For the main configuration in application.conf this is straightforward e.g. start -Dconfig.resource=prod.conf, but I am not sure how to do something similar for the properties files.
The support for specifying config.resource (and a lot of other nice stuff) is built into the typesafe config lib that comes with play. Other third party libs that depend on properties files on the classpath may or may not accept something like that, so if you are lucky then there is a lib specific way to point it to a separate config file.
If they do not then the only option is to somehow provide a different file on the classpath for the different environments. This could basically be done in two ways:
A. To not ship the file with the artifact and instead provide it per environment and add that on the classpath when starting play.
B. To create different artifacts for the different configurations, this would require you to customize dist to create one artifact per distinct env.
I would go for A since it allows there to be one artifact that can be run anywhere, but it might add some annoyances to running the app in a dev env etc.

How do I specify external java source files using SBT?

For example suppose my main Scala project is:
c:\code\mainproject
There is some other java code I need in a separate project
c:\code\secondproject
How can a specify this in the lite version of the configuration file? I have tried
unmanagedClasspath += "C:/code/secondproject"
However it does not even run
If the projects are separate, you should publish the second project locally, add that local repository to the first project (or, more generally, to SBT configuration), and then add the project as a normal dependency.