Pavel
Pavel

Reputation: 1539

Sbt assembly JVM settings

I am just trying to understand the different between next 2 ways of settings JVM parameters while generating jar in scala project using sbt-assembly plugin:

// This works fine - no memory issues while building jar
set "JAVA_OPTS=-Xms2g -Xmx2g"

// in build.sbt 
// Getting memory issues while building jar
javaOptions in assembly += "-Xms2g -Xmx2g"

Thank you,

Upvotes: 2

Views: 1008

Answers (1)

Vladimir Matveev
Vladimir Matveev

Reputation: 127951

SBT is a JVM application, therefore, when you start an SBT shell using the sbt command, eventually it results in a JVM instance being started.

JAVA_OPTS here is an environment variable which is understood by the sbt script; these options are passed to the JVM instance in which SBT shell is running, and in which all tasks are executed. Another way to achieve this would be to pass the -mem option:

sbt -mem 2g

There are also options for storing these arguments in certain files. For example, you can create a file called .sbtopts in the root of your project with the following contents:

-mem 2g

And then each time SBT is started in this directory, the -mem argument will automatically be picked up. This is a convenient way to ensure that all people working on the project use the same SBT options (if you commit this file to the repository, of course),

The javaOptions setting is an SBT build setting, and it is completely unrelated to the options which are set for the JVM instance in which SBT is running. The javaOptions setting is used to configure tasks which start new JVM instances, e.g. you can configure SBT to start a new JVM instance when you run a main method via the runMain task, and then this setting could be used like this:

runMain / javaOptions := "..."  // same as `javaOptions in runMain` but using more modern (and recommended) syntax

The assembly task, however, does not start any new JVM instances to do its work, it runs entirely inside the SBT JVM instance. Therefore, setting javaOptions for assembly will do nothing, since this task does not even read this setting.

Upvotes: 4

Related Questions