2

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,

Pavel
  • 1,519
  • 21
  • 29

1 Answers1

4

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.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
  • So, is there are any way to keep that parameter as part of scala sbt project? instead of specifying command line parameter etc – Pavel Jan 11 '19 at 08:37
  • 1
    Not as a part of the build definition (i.e. `*.sbt` and `project/*.scala` files), but as I said, you can define memory options in a `.sbtopts` file. – Vladimir Matveev Jan 11 '19 at 17:18