Run project with java options via sbt - scala

I am running my fat jar with command java -Djava.security.krb5.conf=/krb5.conf -jar my.jar.
How to run my app with this option via sbt?
$ sbt -Djava.security.krb5.conf="module\\src\\main\\resources\\krb5.conf" run doesn't work. Error:
ctl-scala>sbt -Djava.security.krb5.conf="ctl-core\src\main\resources\krb5.conf" ctl-ui-backend/run
Warning: invalid system property 'java.security.krb5.conf'
[info] Loading global plugins from C:\Users\User\.sbt\0.13\plugins
[info] Loading project definition from C:\Users\User\IdeaProjects\ctl-scala\project
[info] Set current project to ctl (in build file:/C:/Users/User/IdeaProjects/ctl-scala/)
[error] No valid parser available.
[error] ctl-core\\src\\main\\resources\\krb5.conf
[error] ^

Can you try sbt -J-Djava.security.krb5.conf="module/src/main/resources/krb5.conf" run
The -J causes the sbt launcher to pass those as options to the JVM.

As sbt manual it will pass JAVA_OPTS environment variable to the java and if you can not set the variable .jvmopts will hold the java commandline arguments. so if you are in bash :
export JAVA_OPTS="-Djava.security.krb5.conf=/krb5.conf"
before sbt command will pass the argument to java runtime.

You can force sbt to fork a new JVM when running the application, and set the desired java options with the following settings in the build.sbt file:
fork := true,
javaOptions ++= Seq(
"-Djava.security.krb5.conf=/krb5.conf"
)
Simply run the run task and it should start the application in its own JVM with the required java options.

Another option, is to use .sbtopts file. It should be in the root folder, next to sbt.build. Its content should be the java options, prefixed with -J, as written in previously answers, to tell sbt to pass those options to the JVM.
For example its content can be:
-J-Djava.security.krb5.conf=/krb5.conf

Related

compile/package multiple configurations from command line sbt scala

is there a way to build/compile all configurations at once? I have a project that has a Dev configuration in addition to the default Compile and Test configuration, and i am looking for a command or a setting in my build.sbt that would allow me to compile/package all 3 configurations at once.
Basically looking for a way to avoid having to do these 3 commands to build the entire source tree:
sbt compile
sbt dev:compile
sbt test:compile
When I use sbt from IntelliJ it is able to do this on building the project, but I am looking to do this from the command line.
First, you can run multiple tasks with a single sbt invocation:
sbt compile dev:compile test:compile
Second, you could define an alias in your build which does what you want:
addCommandAlias("compileAll", "; compile; dev:compile; test:compile")
Then, just run sbt compileAll.

How to run jar generated by sbt package via sbt

I have a jar that was created by running sbt package
I've already set the main file in the jar in build.sbt
mainClass in (Compile, packageBin) := Some("com.company.mysql.Main")
addCommandAlias("updatemysql", "runMain com.company.mysql.Main")
I've tried
sbt "runMain target/scala-2.12/update-mysql_2.12-0.1-SNAPSHOT.jar"
sbt target/scala-2.12/update-mysql_2.12-0.1-SNAPSHOT.jar com.company.mysql.Main
sbt target/scala-2.12/update-mysql_2.12-0.1-SNAPSHOT.jar:com.company.mysql.Main
sbt update-mysql-assembly-0.1-SNAPSHOT.jar/run
sbt run update-mysql-assembly-0.1-SNAPSHOT.jar
^ this gives No main class detected even though main class is set in build.sbt as shown a few lines above.
I need to run the jar through sbt because it's the only way I know how to overwrite config file that is contained in the jar using -Dpath.to.config.param=new_value
In sbt run and runMain use classpath containing all the dependencies as well as folders with outputs of compilation tasks - which means that none of them takes JAR as an argument.
I think it would be possible to run this particular JAR from sbt by writing a custom task that would depend on output of package task (that is JAR filepath value) and run it as external process... though from the question it seems that this is not the actual problem.
The actual problem is running the JAR with flags passed into JVM instead of program itself which can be achieved by something like:
# clean assembly ensures that there is only 1 JAR in target
# update-mysql_2.12-*.jar picks the only JAR no matter what is its version
# -D arguments NEED to be passed before -jar to pass it to JVM and not the JAR
sbt clean assembly && \
java -Dpath.to.config.param=new_value -jar target/scala-2.12/update-mysql_2.12-*.jar

Tasks stuck with sbt 1.0

Following the "https://www.coursera.org/learn/scala-spark-big-data/supplement/R7FJ9/eclipse-tutorial" course, I'm trying to create a eclipse project from sbt, by adding sbteclipse plugin.
sbt new scala/hello-world.g8
cd hello-world
echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")' > project/plugins.sbt
sbt> eclipse
It should last a minute and return several successful, but instead it stay still, it gets stuck. The message I've received is
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
I've installed sbt 1.0.2, it fails, then I'd install sbt 0.13.16 and still fails. Checking the sbt version it says it's 1.0.2 (although I'd uninstalled sbt 1.0.2 to install the older version).
checking https://github.com/typesafehub/sbteclipse/issues/346 it seen like this is a brand new issue, it's supose the gets fix by typing sbt.version=0.13.16 in project/build.properties, but even after that the problem persist. I don't know if problem persist because is a fix for Mac and I'm working on windows 10, or if the solution saids is for sbt.version=0.13.15 and I'm using sbt.version=0.13.16.
Checking the \scala hello world\hello-world\eclipse file it says:
[error] [C:\Users\israel.rodriguez\Documents\eclipse\scala hello world\hello-world\project\plugins.sbt]:1: unclosed character literal
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
Terminate batch job (Y/N)?
checking the \scala hello world\hello-world\project\plugins.sbt I found :
'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")'
when removing the ' ' at the beginning and the end. the \scala hello world\hello-world\eclipse file saids :
[info] Loading project definition from
C:\Users\israel.rodriguez\Documents\eclipse\scala hello
world\hello-world\project
[info] Set current project to hello-world (in build file:/C:/Users/israel.rodriguez/Documents/eclipse/scala%20hello%20world/hello-world/)
and it remain there for eternity. with the same message on windows shell :
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option
MaxPermSize=256m; support was removed in 8.0
My question is, what's wrong over here? ,and how can I solve or work around this. Sorry for the rookie question, is my firt time with sbt, scala or spark.
I was in exact same scenario with the same problem as mentioned above. Here is how I overcame it.
Firstly there is no problem in steps that you follow everything is correct except:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")
Use latest version for the sbt plugin form, which is:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4")
The latest version handles the hanging console problem as described here, also there is no need to use '' at the beginning and the end.
Step: 1. Download and install sbt from here (windows sbt-1.0.4.msi)
Step 2. Go to cmd prompt and type sbt about, it will display something like this
C:\Users\rajnish.kumar>sbt about
"C:\Users\rajnish.kumar\.sbt\preloaded\org.scala-sbt\sbt\"1.0.4"\jars\sbt.jar"
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[warn] Executing in batch mode.
[warn] For better performance, hit [ENTER] to switch to interactive mode, or
[warn] consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from C:\Users\rajnish.kumar\project
[info] Set current project to rajnish-kumar (in build file:/C:/Users/rajnish.kumar/)
[info] This is sbt 0.13.15
[info] The current project is {file:/C:/Users/rajnish.kumar/}rajnish-kumar 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.6
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbt.plugins.Giter8TemplatePlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.6
I'm not sure why it says "This is sbt 0.13.15"; if anybody knows please answer it here.
Step 3. Go to your workspace form cmd prompt and type and hit sbt new sbt/scala-seed.g8, type project name as (e.g. hello) when asked.
Step 4. Now move to your project directory i.e cd hello
Step 5. Now type echo addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4") > project/plugins.sbt
Step 6. Make sure you are inside the hello project and then instead of calling sbt > eclipse (if you will do like this way then it will get stuck in the same infinite loop as you're getting) only call sbt and it will start the server on default port.
Step 7. Now type eclipse, now it will start downloading all dependency, eventually it will show Successfully created Eclipse project files for project(s):
Step 8. Import project (hello) to Eclipse as normal and it will work. Please not do not call sbt > eclipse in single go call them one by one first call sbt and when server is started then only call Eclipse.
I had the same issue when I start reading about scala.
Concerning the problem of [\plugins.sbt]:1: unclosed character literal] use :
echo addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0") > project/plugins.sbt
Remove the simple quote from the begining and the end of the echo first parameter
I had the very same issue as you describe. I use Windows 10 and wonder if it just occurs on that system.
It works if I use sbteclipse 5.2.4 and some other small modifications:
cd hello-world
echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4")' > project/plugins.sbt
Next, I need to go to project/plugins.sbt and remove the ' at the beginning and end of the line.
Now, do NOT enter sbt > eclipse! This will just pipe the sbt console into a the newly created file and you won't execute eclipse. Instead, typ the following into the command line:
sbt eclipse

Change sbt boot and ivy dirs via environment variables

Is it possible to set .sbt and .ivy2 directories via environment variables?
I can override these parameters when i run sbt like that:
sbt -Dsbt.boot.directory=/tmp/.sbt/boot -Dsbt.ivy.home=/tmp/.ivy2 version
I thought this would work:
export sbt.boot.directory=/tmp/.sbt/boot
export sbt.ivy.home=/tmp/.ivy/home
sbt version
but it doesn't work...
Second question: how can i bypass sbt config launcher? (http://www.scala-sbt.org/0.13/docs/Launcher-Configuration.html) when running sbt?
You can set JAVA_OPTS or SBT_OPTS environment variables with the runtime parameters for the SBT. The difference is that JAVA_OPTS effects also other Java based applications while SBT_OPTS only effects the SBT. For example:
export SBT_OPTS="-Dsbt.ivy.home=/tmp/.ivy/home -Dsbt.boot.directory=/tmp/.sbt/boot"
When you run the SBT from the command-line, it will use these parameters. However, if you launch it from IntelliJ IDEA, it will not use them. IDEA launches the SBT JAR directly and SBT's VM parameters have to be configured in the IDEA sbt settings per project.
For the second question, it is unclear what you mean by bypass. It is possible to run the SBT jar directly instead of the sbt launcher script. For example:
java -server -Xmx1536M -Dsbt.ivy.home=/tmp/.ivy/home -jar /<install-path>/sbt/bin/sbt-launch.jar

How to specify JVM maximum heap size "-Xmx" for running an application with "run" action in SBT?

My application does large data arrays processing and needs more memory than JVM gives by default. I know in Java it's specified by "-Xmx" option. How do I set SBT up to use particular "-Xmx" value to run an application with "run" action?
For forked processes you should look at Build.scala
To modify the java options for forked processes you need to specify them in the Build.scala (or whatever you've named your build) like this:
val buildSettings = Defaults.defaultSettings ++ Seq(
//…
javaOptions += "-Xmx1G",
//…
)
This will give you the proper options without modifying JAVA_OPTS globally, and it will put custom JAVA_OPTS in an sbt generated start-script
For non forked processes it's most convenient to set the config via sbtopts or sbtconfig depending on your sbt version.
Since sbt 0.13.6 .sbtconfig is deprecated. Modify /usr/local/etc/sbtopts along these lines:
-J-Xms512M
-J-Xmx3536M
-J-Xss1M
-J-XX:+CMSClassUnloadingEnabled
-J-XX:+UseConcMarkSweepGC
-J-XX:MaxPermSize=724M
-J-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
You can also create an .sbtopts file in the root of your SBT project using the same syntax as in the /usr/local/etc/sbtopts file. This makes the project self-contained.
Before sbt 0.13.6 you could set the options in .sbtconfig for non forked processes:
Check where sbt is:
$ which sbt
/usr/local/bin/sbt
Look at the contents:
$ cat /usr/local/bin/sbt
#!/bin/sh
test -f ~/.sbtconfig && . ~/.sbtconfig
exec java ${SBT_OPTS} -jar /usr/local/Cellar/sbt/0.12.1/libexec/sbt-launch.jar "$#"
Set the correct jvm options to prevent OOM (both regular and PermGen):
$ cat ~/.sbtconfig
SBT_OPTS="-Xms512M -Xmx3536M -Xss1M
-XX:+CMSClassUnloadingEnabled
-XX:+UseConcMarkSweepGC -XX:MaxPermSize=724M"
If you want to set SBT_OPTS only for the current run of sbt you can use env SBT_OPTS=".." sbt as suggested by Googol Shan. Or you can use the option added in Sbt 12: sbt -mem 2048. This gets unwieldy for longer lists of options, but it might help if you have different projects with different needs.
Note that CMSClassUnloadingEnabled in concert with UseConcMarkSweepGC helps keep the PermGen space clean, but depending on what frameworks you use you might have an actual leak on PermGen, which eventually forces a restart.
In sbt version 12 onwards there is an option for this:
$sbt -mem 2048
If you run sbt on linux shell, you can use:
env JAVA_OPTS="-Xmx512m" sbt run
This is my usually used command to run my sbt project.
.sbtconfig is deprecated starting with SBT 0.13.6. Instead, I configured these options in /usr/local/etc/sbtopts in the following way:
-J-Xms512M
-J-Xmx3536M
-J-Xss1M
-J-XX:+CMSClassUnloadingEnabled
-J-XX:+UseConcMarkSweepGC
-J-XX:MaxPermSize=724M
-J-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
Try this:
class ForkRun(info: ProjectInfo) extends DefaultProject(info) {
override def fork = Some(new ForkScalaRun {
override def runJVMOptions = super.runJVMOptions ++ Seq("-Xmx512m")
override def scalaJars = Seq(buildLibraryJar.asFile, buildCompilerJar.asFile)
})
}
There's one way I know of. Set the environment variable JAVA_OPTS.
JAVA_OPTS='-Xmx512m'
I have not found a way to do this as a command parameter.
Use JAVA_OPTS for setting with environment variable.
Use -J-X options to sbt for individual options, e.g. -J-Xmx2048 -J-XX:MaxPermSize=512
Newer versions of sbt have a "-mem" option.
The javaOptions += "-XX:MaxPermSize=1024" in our build.sbt as referenced by #iwein above worked for us when we were seeing a java.lang.OutOfMemoryError thrown while running Specs2 tests through sbt.
The environment variable is _JAVA_OPTIONS, which needs to be set.
Once you set _JAVA_OPTIONS, and when you sbt, sbt will show the message using JAVA_OPTIONS and the values.
Alternatively you could set javaOption in the sbt or .scala file
e.g
javaOptions += "-Xmx1G"
From sbt shell you could run show javaOptions to see the values that are set.
sbt lets you list the JVM options you need to run your project on a file named
.jvmopts
in the root of your project.
then add the java options that you want
cat .jvmopts
-Xms512M
-Xmx4096M
-Xss2M
-XX:MaxMetaspaceSize=1024M
it is tested and works in windows 10
https://www.lagomframework.com/documentation/1.4.x/scala/JVMMemoryOnDev.html
javaOptions in Test += "-Xmx1G"
This sets the JVM options for tests. Works also with jvm forking (fork in Test := true).