Load Different Config File per Task on SBT - scala

I am trying to load different config file per task in sbt, for example I want to be able to run a task like:
sbt devRun
and loads src/main/resources/application.dev.conf while running:
sbt run
will load src/main/resources/application.conf.
As I understand it, we can load different application.conf when running test by putting it on src/test/resource/application.conf, but using different config file for different task (or scope) will need some code on SBT.
I've been trying to google around and usually the suggestion is to use it on run task like:
$ sbt run -Dconfig.resources="application.dev.conf"
But as far as I understand, the above will only load different config on runtime. I have multiple projects with lots of config file. I want to load them dynamically, based on scope / task. What's the best way to do this?
Thanks before.

You can(must) use the javaOptions setting. It represents Java options when sbt runs a forked JVM, e.g. it runs tests in a different JVM process(by default).
javaOptions in Test += "-Dconfig.resource=" + System.getProperty("config.resource", "application.test.conf")
Instead of Test plug in whatever config you have. If you don't explicitly pass a config.resource parameter it will use application.test.conf.

Related

How to re-use compiled sources in different machines

To speed up our development workflow we split the tests and run each part on multiple agents in parallel. However, compiling test sources seem to take most of the time for the testing steps.
To avoid this, we pre-compile the tests using sbt test:compile and build a docker image with compiled targets.
Later, this image is used in each agent to run the tests. However, it seems to recompile the tests and application sources even though the compiled classes exists.
Is there a way to make sbt use existing compiled targets?
Update: To give more context
The question strictly relates to scala and sbt (hence the sbt tag).
Our CI process is broken down in to multiple phases. Its roughly something like this.
stage 1: Use SBT to compile Scala project into java bitecode using sbt compile We compile the test sources in the same test using sbt test:compile The targes are bundled in a docker image and pushed to the remote repository,
stage 2: We use multiple agents to split and run tests in parallel.
The tests run from the built docker image, so the environment is the
same. However, running sbt test causes the project to recompile even
through the compiled bitecode exists.
To make this clear, I basically want to compile on one machine and run the compiled test sources in another without re-compiling
Update
I don't think https://stackoverflow.com/a/37440714/8261 is the same problem because unlike it, I don't mount volumes or build on the host machine. Everything is compiled and run within docker but in two build stages. The file modified times and paths are retained the same because of this.
The debug output has something like this
Initial source changes:
removed:Set()
added: Set()
modified: Set()
Invalidated products: Set(/app/target/scala-2.12/classes/Class1.class, /app/target/scala-2.12/classes/graph/Class2.class, ...)
External API changes: API Changes: Set()
Modified binary dependencies: Set()
Initial directly invalidated classes: Set()
Sources indirectly invalidated by:
product: Set(/app/Class4.scala, /app/Class5.scala, ...)
binary dep: Set()
external source: Set()
All initially invalidated classes: Set()
All initially invalidated sources:Set(/app/Class4.scala, /app/Class5.scala, ...)
Recompiling all 304 sources: invalidated sources (266) exceeded 50.0% of all sources
Compiling 302 Scala sources and 2 Java sources to /app/target/scala-2.12/classes ...
It has no Initial source changes, but products are invalidated.
Update: Minimal project to reproduce
I created a minimal sbt project to reproduce the issue.
https://github.com/pulasthibandara/sbt-docker-recomplile
As you can see, nothing changes between the build stages, other than running in the second stage in a new step (new container).
While https://stackoverflow.com/a/37440714/8261 pointed at the right direction, the underlying issue and the solution for this was different.
Issue
SBT seems to recompile everything when it's run on different stages of a docker build. This is because docker compresses images created in each stage, which strips out the millisecond portion of the lastModifiedDate from sources.
SBT depends on lastModifiedDate when determining if sources have changed, and since its different (the milliseconds part) the build triggers a full recompilation.
Solution
Java 8:
Setting -Dsbt.io.jdktimestamps=true when running SBT as recommended in https://github.com/sbt/sbt/issues/4168#issuecomment-417655678 to workaround this issue.
Newer:
Follow recomendation in https://github.com/sbt/sbt/issues/4168#issuecomment-417658294
I solved the issue by setting SBT_OPTS env variable in the docker file like
ENV SBT_OPTS="${SBT_OPTS} -Dsbt.io.jdktimestamps=true"
The test project has been updated with this workaround.
Using SBT:
I think there is already an answer to this here: https://stackoverflow.com/a/37440714/8261
It looks tricky to get exactly right. Good luck!
Avoiding SBT:
If the above approach is too difficult (i.e. getting sbt test to consider that your test classes do not need re-compiling), you could instead avoid using sbt but instead run your test suite using java directly.
If you can get sbt to log the java command that it is using to run your test suite (e.g. using debug logging), then you could run that command on your test runner agents directly, which would completely preclude sbt re-compiling things.
(You might need to write the java command into a script file, if the classpath is too long to pass as a command-line argument in your shell. I have previously had to do that for a large project.)
This would be a much hackier approach that the one above, but might be quicker to get working.
A possible solution might be defining your own sbt task without dependencies or try to change the test task. For example you could create a task to run a JUnit runner if that was your testing framework. To define a task see this on Implementing Tasks.
You could even go as far as compiling sending the code and running the remotes from the same task as it is any scala code you want. From the sbt reference manual
You could be defining your own task, or you could be planning to redefine an existing task. Either way looks the same; use := to associate some code with the task key

Typesafe config loads wrong configuration

So the problem is really simple and I hope solution will be as well.
So basically I have two configuration files application.conf and dev.conf. I'm passing my config files from command line like that sbt -Dconfig.file=dev.conf.
The problem is when I use ConfigFactory.load from main object(the one which extends App) it loads config I passed via command line(in this case dev.conf), but when I load the config from different object it loads default application.conf.
Can I load somehow config passed from arguments from any object?
When you run your application with the runMain SBT task, then by default SBT won't create a separate JVM for your code. This has several consequences around the application lifecycle, and of course with regard to system properties as well.
In general, your approach should work, as long as your build configuration does not enable forking. However, I think the better approach would be to actually rely on forking and specify the system property explicitly. This is guaranteed to work. To do this, you need to set the fork setting in the run task to true, and then add a JVM command line option:
Compile / run / fork := true,
Compile / run / javaOptions += "-Dconfig.file=dev.conf",
Don't forget to restart SBT after that. You won't need to pass the config.file property to SBT with this approach; rather, it is controlled by the javaOptions setting, as in the example above.

how to disclude development.conf from docker image creation of play framework application artifact

Using scala playframework 2.5,
I build the app into a jar using sbt plugin PlayScala,
And then build and pushes a docker image out of it using sbt plugin DockerPlugin
Residing in the source code repository conf/development.conf (same where application.conf is).
The last line in application.conf says include development which means that in case development.conf exists, the entries inside of it will override some of the entries in application.conf in such way that provides all default values necessary for making the application runnable locally right out of the box after the source was cloned from source control with zero extra configuration. This technique allows every new developer to slip right in a working application without wasting time on configuration.
The only missing piece to make that architectural design complete is finding a way to exclude development.conf from the final runtime of the app - otherwise this overrides leak into production runtime and obviously the application fails to run.
That can be achieved in various different ways.
One way could be to some how inject logic into the build task (provided as part of the sbt pluging PlayScala I assume) to exclude the file from the jar artifact.
Other way could be injecting logic into the docker image creation process. this logic could manually delete development.conf from the existing jar prior to executing it (assuming that's possible)
If you ever implemented one of the ideas offered,
or maybe some different architectural approach that gives the same "works out of the box" feature, please be kind enough to share :)
I usually have the inverse logic:
I use the application.conf file (that Play uses by default) with all the things needed to run locally. I then have a production.conf file that starts by including the application.conf, and then overrides the necessary stuff.
for deploying to production (or staging) I specify the production/staging.conf file to be used
This is how I solved it eventually.
conf/application.conf is production ready configuration, it contains placeholders for environment variables whom values will be injected in runtime by k8s given the service's deployment.yaml file.
right next to it, conf/development.conf - its first line is include application.conf and the rest of it are overrides which will make the application run out of the box right after git clone by a simple sbt run
What makes the above work, is the addition of the following to build.sbt :
PlayKeys.devSettings := Seq(
"config.resource" -> "development.conf"
)
Works like a charm :)
This can be done via the mappings config key of sbt-native-packager:
mappings in Universal ~= (_.filterNot(_._1.name == "development.conf"))
See here.

Set the stack size for SBT

I'm running SBT using my specially built Scala. My built Scala Compiler will do a lot of things at runtime, with a lot of function calls, which can be recursive.
So when I run SBT using my built Scala Compiler, stack overflows after a long time. I try to set -J-Xss when starting SBT. But that doesn't work.
I encountered the problem with SBT heap size before. And many posts says setting -J-Xmx when starting SBT won't change the JVM heap size because it is overridden by the default SBT memory options.
How to set heap size for sbt?
Now, I wonder whether -J-Xss can be overridden by default SBT options, just like -J-Xmx being overridden. Or I should simply try to set -J-Xss larger?
There are a number of ways to do this, but it depends what you are trying to achieve. If you want larger heap for running tests for instance, look at the secondary approach undertaken here.
SBT_OPTS
First you can simply set the environment variable SBT_OPTS which SBT will natively look for while loading itself, and this should override any settings that you want to specify.
export SBT_OPTS = "-Xmx1G;-Xms256m;...";
Custom launcher
The other way to achieve the same is to basically create a custom SBT launching script. Have a look at the example here.
For testing
If you want to modify the testing options, you need to use javaOptions in ThisBuild ++= Seq("-Xmx1g", ...). For them to even be run, you always need to have fork in Test := true, which will create a forked JVM for running tests. Without that, the options specified will not be honoured.

SBT: How to trigger separate actions when files change in two separate subprojects

My build.sbt looks like this:
val client = project.in(file("client"))
val server = project.in(file("server"))
The main project comprises two separate projects, client and server. I would like to develop them simultaneously: I need both of them built and the server running when I'm working. Each project has their own additional build steps: client needs a packageJS after being compiled, while server needs a container:restart.
However, doing ~; restartServer; restartClient from the root directory doesn't do what I want, since it listens to either subproject and always restarts both of them, and in my case causes a restart loop since one subproject dumps files into the other subproject for it to use.
Is there anyway to do this "~restartXXX" in both subprojects simultaneously, so I can edit either of them and it will restart only the edited project?
Have you tried: ~ all restartServer restartClient.
In sbt 0.13.2-M2 (and I think sbt 0.13.1, but not sure), there is a new all command which will run specified tasks in parallel. Combined with ~, you can ensure you run more than one task on a change.