Change sbt boot and ivy dirs via environment variables - scala

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

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.

Run project with java options via sbt

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

running sbt class using only a jar

Is there any way to run sbt commands with only a jar instead of a project?
I've been having issues using scopt with java or scala commands, and it only seems to work with sbt.
Ideally something like
sbt --jar <jar name>/"run-main <options"
You'd probably want to package everything up into something you can execute. One possibility would be to create a fat jar using something like sbt-assembly.
Once you've built your jar, you can then:
java -jar /path/to.jar --your-options
Take note that at this point you can only do what would have been the equivalent of sbt run-main with the jar. You cannot of course invoke any of the other sbt commands on the jar created.

How can I let sbt download the source of scala-library.jar?

I know if I add withSources when I define one dependency, sbt can download that sources jar file automatically.
For example,
val specs = "org.scala-tools.testing" % "specs_2.8.1" % "1.6.6" % "test" withSources ()
But for the scala-library.jar and scala-compiler.jar, I don't need define them explicitly, how can I get sbt download their sources for me? So, I don't need config it manually after generate idea project using sbt-idea-plugin.
You have to change the boot properties. There is a nice description in the recent blog decodified from Mathias:
"How to make SBT download scala library sources" (started from #hseeberger key starting points)
Here is the relevant part (in case that link ever goes stale)
First, forget about trying to find some “hidden” setting in your SBT project definition enabling Scala library source download! It does not exist (at least not in SBT version 0.7.x).
Rather, there are these two things you need to do in order to whip SBT into submission:
Create an alternative configuration file for your SBT launcher.
Make the SBT launcher use it.
These are the steps in detail:
Find your sbt-launcher-0.7.x.jar file.
Since I’m on OS/X and use SBT via Homebrew mine lives at /usr/local/Cellar/sbt/0.7.5.RC0/libexec/sbt-launch-0.7.5.RC0.jar.
Extract the sbt.boot.properties from the sbt sub directory in the launcher jar
Fire up your favorite editor and change line 3 to classifiers: sources (uncomment the line)
Find the sbt script file you created during your SBT setup (e.g. ~/bin/sbt, or, when using Homebrew, /usr/local/Cellar/sbt/0.7.x/bin/sbt)
Add the path to your tweaked sbt.boot.properties file, prepended with an ’#’ character and in double quotes, as the second-to-last argument of the java call.
This is what my sbt script file looks like:
#!/bin/sh
java -Xmx768M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m \
-jar /usr/local/Cellar/sbt/0.7.5.RC0/libexec/sbt-launch-0.7.5.RC0.jar \
"#/usr/local/Cellar/sbt/0.7.5.RC0/libexec/sbt.boot.properties" \
"$#"
Once you have completed these steps SBT should happily download the scala-...-sources.jar files for the Scala compiler and standard library for any new project you create.
To have SBT do this for an existing project, you have to manually delete the project/boot/scala-{version} directory before performing an ‘sbt update’ (SBT does not fetch additional source artifacts if the main jar is already present).
Once you have a custom sbt.boot.properties file, there are also other ways to supply it to the SBT launcher.
See SO question "how do I get sbt to use a local maven proxy repository (Nexus)?"
Based on Michael Slinn comments:
If you are using sbt 0.11.x and above, use this command:
sbt update-sbt-classifiers
Two pieces of information.
(1) SBT Documentation
http://www.scala-sbt.org/0.13.5/docs/Detailed-Topics/Library-Management.html
and I quote:
"To obtain particular classifiers for all dependencies transitively, run the updateClassifiers task. By default, this resolves all artifacts with the sources or javadoc classifier."
This means you should not need to do anything, but you can make it explicit and put in you build.sbt:
transitiveClassifiers := Seq("sources", "javadoc")
To actually get the sources downloaded by SBT then you do:
"updateClassifiers"
(2) If you are working with Eclipse scala IDE - most likely you are as development of plugins for Eclipse/Netebeans is a lot more active for eclipse - then you should configure your ecplise to find out the sources if you do the following.
EclipseKeys.withSource := true
Here is the documentation you should read,
https://github.com/typesafehub/sbteclipse/wiki/Using-sbteclipse

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).