Running application using sbt run and some flags - scala

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.

Related

How to run sbt assembly by excluding integration test

I am looking to create fat jar by 'sbt assembly' but i dont wan to run integration test in sbt assembly. I want only jar file.
You can either disable tests in your build.sbt using assembly / test := {} as suggested by #laughedelic, or if you're executing from a terminal and want to skip to the tests for that build use: sbt 'set test in assembly := {}' assembly.
If you have set the value in your build.sbt file you can check that its value is as expected by running sbt show assembly / test which should return something like ().

Running Scala Script in sbt project

How can i run a scala script inside a sbt project which can access all classes of the sbt project and typesafe config as well? Basically I want the script to run in a similar way as the sbt console.
one option is to assemble a jar using sbt-assembly
you would need to add the following to a .sbt file to your project directory
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")
and add a minimum of two lines to your build file.
assemblyJarName in assembly := "something.jar"
mainClass in assembly := Some("com.example.Main")
then you can run the 'assembly' task from sbt, this will build and executable "fat" jar with all your dependencies and configuration.
You can use the launcher and the command system to implement an interactive appliation with autocomplete etc.
Here is a tutorial:
http://www.scala-sbt.org/0.13/docs/Command-Line-Applications.html
You have to invoke the application separately, though; I don't think it is possible to run it directly from the sbt prompt within the application directory.

Set javaOptions in Test for Play/SBT

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

Use scalaz in console repl without creating a project

Is there any way to use scalaz by simple scala command in the terminal, without creating sbt project?
If you have sbt installed, it is relatively quick to setup a scalaz sandbox.
First run sbt:
sbt
Then issue these commands:
set scalaVersion := "2.11.2"
set libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.0"
set initialCommands += "import scalaz._, Scalaz._"
session save
console
There you go, you are in the scala REPL with scalaz auto-imported and ready to be used (sbt automatically downloaded scalaz for you).
Because of the command session save, this setup is now permanent and you can at will go back to this same folder and just do sbt console to rerun the REPL with scalaz support.
You can either manually grab the jar or use sbt to grab the jar once and put it in your classpath:
#!/bin/sh
/Users/you/apps/scala/bin/scala -cp /Users/you/.ivy2/cache/org.scalaz/scalaz-core_2.10/bundles/scalaz-core_2.10-7.0.0.jar

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.