Set javaOptions in Test for Play/SBT - scala

I configured build.sbt for the unit test to use a different Play (2.3.9 for Scala and SBT 0.13.5) configuration via,
javaOptions in Test ++= Seq("-Dconfig.file=/home/kitty/acme/test/resources/test-application.conf")
Play did not pick up test-application.conf and used application.conf in conf instead. AFAIK, there is no scalaOption in this case. However, if I include -Dconfig.file in the command line, it works fine,
sbt test -Dconfig.file=/home/kitty/acme/test/resources/test-application.conf
How do I fix this? Thanks.

javaOptions in Test ++= Seq("-Dconfig.file=/home/kitty/acme/test/resources/test-application.conf") didn't work because my fork in Test was false. Therefore, set fork to true and it will work. -Dconfig.resource like -Dconfig.file works the same way too. SBT will not pick it up if it is not forked. Strictly, javaOptions only work with fork is true as mentioned here

you're almost there, you can force JVM options like this
javaOptions in Test ++= Seq("-Dconfig.file=/home/kitty/acme/test/resources/test-application.conf")
config.file also takes a relative path e.g conf/test-application.conf

Related

Running application using sbt run and some flags

I am currently running a akka application with the below command after I run
sbt assembly
java -Dconfig.resource=/application.test.conf -cp /path/to/folder:./target/scala-2.11/app-name.jar ca.path.to.main
Is there a way I can pass this information using sbt and some flags so I don't have to run the sbt assembly task everytime just to run the application?
sbt run config=/application.test.conf cp=/path/to/folder:
(something like the above)
Options that are passed to JVM are read by sbt from the javaOptions setting. So you can configure this setting to have the options you want and then tell sbt to fork new JVM process everytime you run your app from sbt so these options are applied. You can do this from sbt console:
set javaOptions += "-Dconfig.resource=/application.test.conf"
set fork := true
run
Or in your build.sbt file:
javaOptions += "-Dconfig.resource=/application.test.conf"
fork := true
However this might not be the most idiomatic approach to reach your underlying end goal.

SBT increase compilation memory

How can I increase the compilation memory for the project in the build.sbt? Not in the general SBT config.
I want the config to be committed into my Git repo.
Cheers
Create .sbtopts file in root of your SBT project and put in -J-Xmx4G (and similarly -J<JVM option>. Unfortunately, it doesn't seem to work on Windows.
the apparent solution would be to do it this way:
scalacOptions in ThisBuild ++= Seq ("-JXss512m","-JXmx2G")
while I see these values display when I run:
sbt
show scalacOptions
the settings do not seem to be honored by the actual compiler

define custom configuration in sbt

I want to set another set of options for running tests in integration server and in dev environment.
Let's have this option:
testOptions := Seq(Tests.Filter(s => Seq("Spec", "Unit").exists(s.endsWith(_))))
How can change the testOptions, so it is only applied, when the test command is prefixed with some scope like teamcity:test?
I expect, that the testOptions would be modified with similar syntax:
testOptions in Teamcity := ...
I also would like to know, how to define the custom scope, preferable in simple *.sbt build, not in project/*.scala build.
The scope could be either project, configuration, or task. In this case, I think you're looking to define a custom configuration.
using itSettings
There's a built-in configuration called IntegrationTest already. You can define it in your build definition by writing:
Defaults.itSettings
This will use completely different setup from normal tests including the test code (goes into src/it/scala/) and libraries, so this may not be what you want.
defining your own configuration
Using sbt 0.13, you can define a custom configuration as follows in build.sbt:
val TeamCity = config("teamcity") extend(Test)
val root = project.in(file(".")).
configs(TeamCity).
settings(/* your stuff here */, ...)
defining teamcity:test
Now you have to figure out how to define teamcity:test.
Edit: Mark Harrah pointed out to me that there's a documentation for this. See Additional test configurations with shared sources.
An alternative to adding separate sets of test sources (and compilations) is to share sources. In this approach, the sources are compiled together using the same classpath and are packaged together.
putting it all together
val TeamCity = config("teamcity") extend(Test)
val root = project.in(file(".")).
configs(TeamCity).
settings(
name := "helloworld",
libraryDependencies ++= Seq(
"org.specs2" %% "specs2" % "2.2" % "test"
)
).
settings(inConfig(TeamCity)(Defaults.testTasks ++ Seq(
testOptions := Seq(Tests.Argument("nocolor"))
)): _*)
When you run teamcity:test the Specs2 output displays without color.

Running SBT with -deprecation

I seem to have warnings in my project/build.scala file (NOT IN MY SCALA PROJECT). How do I configure SBT to run with the -deprecation flag.
// Does not help so do not suggest it!
scalacOptions ++= Seq("-unchecked", "-deprecation")
I know that SBT has the sbt.boot.properties files, but can't figure out if the flag should go in there or not. And if it is an example would be nice. Thx in advance.
BTW
I use SBT launcher for 0.12.2 and have the issue both with SBT 0.12.2 and 0.11.3. And I'm on Ubuntu in case that matters.
Simply put the scalacOptions setting in project/build.sbt. Settings for your project and your build definition go in different files, because they have to be compiled before they can be used and as you want to change compiler settings, this is not possible to handle in the same file.
edit: Just to prevent confusion, ./build.sbt, project/build.scala and project/build.sbt are different. In the first one you put your normal settings for the project and in the latter two (never both used together) you can put settings that affect the compilation of your project files.

How can I pass JVM options to SBT to use when running the app or test cases?

I would like to specify JVM options when running my app or the tests for the app through SBT. Specifically, I need to be able to give the JVM the -Djava.security.policy parameter so that my policy is loaded and used for the test.
How can I do this with SBT?
With xsbt, you could run your test in a forked JVM (because of one of the reasons mentioned in "Running Project Code".
If you are using a forked jvm:
specify the configuration to affect only the main or test run tasks:
scala javaOptions in (Test,run) += "-Xmx8G"
You should be able to specify any other options to that JVM through javaOptions.
The OP David Eagen reports that the following configuration didn't work at first, not because of the sbt options, but because of the path:
lazy val escacheServer =
Project( "escache-server",
file("server"),
settings = buildSettings ++ Seq(resolvers ++=
Seq(scala_tools_snapshots, typesafe_repo),
libraryDependencies ++= escacheServerDeps,
javaOptions in run += "-Djava.security.policy=jini.policy",
fork in run := true
)
).dependsOn(escache) }
It looks like my problem was that jini.policy wasn't found in the current directory.
I set the full path and now it runs.