Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set system properties for runMain on command line?

Tags:

sbt

How can I set a system property for runMain upon executing it from command line on Windows?

I'd like to be able to run the following command:

sbt -Dconfig.resource=../application.conf "runMain akka.Main com.my.main.Actor" 

Regardless of whether fork is true, whether I put it in SBT_OPTS, or how I pass it in I cannot accomplish this. I am familiar with both Setting value of setting on command line when no default value defined in build? and Setting system properties with "sbt run" but neither answer my question.

Other questions seem to indicate you can't even easily view the Java invocation arguments easily in SBT. Any help is appreciated.

like image 376
Chad Retz Avatar asked Jan 27 '14 19:01

Chad Retz


People also ask

How do I set system properties in Linux?

Go to the Registry Editor (Start > regedit.exe). To change existing properties double-click the appropriate value. To change additional properties, double-click options. Refer to the list of parameters in Recognized System Properties.

How do I set system properties in environment variables?

To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.

How do I find System Properties in Unix?

To view information about your CPU, use the lscpu command as it shows information about your CPU architecture such as a number of CPUs, cores, CPU family model, CPU caches, threads, etc from sysfs and /proc/cpuinfo.


2 Answers

This works:

sbt '; set javaOptions += "-Dconfig.resource=../application.conf" ; runMain akka.Main com.my.main.Actor' 

If this isn't a "friendly" enough syntax, wrap it in a little shell script.

(Note this assumes you have fork set to true for running. If you don't, see akauppi's comment.)

like image 124
Seth Tisue Avatar answered Sep 23 '22 15:09

Seth Tisue


You could use envVars setting. I'm unsure how idiomatic it is in SBT, though.

> help envVars Environment variables used when forking a new JVM 

The following (very minimalistic) build.sbt worked fine.

fork := true  envVars := Map("msg" -> "hello") 

Once you get it running, setting envVars to any value with set does the trick.

> help set set [every] <setting-expression>          Applies the given setting to the current project:           1) Constructs the expression provided as an argument by compiling and loading it.           2) Appends the new setting to the current project's settings.           3) Re-evaluates the build's settings.          This command does not rebuild the build definitions, plugins, or configurations.         It does not automatically persist the setting(s) either.         To persist the setting(s), run 'session save' or 'session save-all'.          If 'every' is specified, the setting is evaluated in the current context         and the resulting value is used in every scope.  This overrides the value         bound to the key everywhere. 

I've got a simple app to run.

$ sbt run [info] Set current project to fork-testing (in build file:/C:/dev/sandbox/fork-testing/) [info] Running Hello [info] hello 

With the envVars setting changed on the command line the output would change as follows:

$ sbt 'set envVars := Map("msg" -> "Hello, Chad")' run [info] Set current project to fork-testing (in build file:/C:/dev/sandbox/fork-testing/) [info] Defining *:envVars [info] The new value will be used by *:runner, compile:run::runner and 1 others. [info]  Run `last` for details. [info] Reapplying settings... [info] Set current project to fork-testing (in build file:/C:/dev/sandbox/fork-testing/) [info] Running Hello [info] Hello, Chad 

runMain is no different from run in this case.

$ sbt 'set envVars := Map("msg" -> "Hello, Chad")' 'runMain Hello' [info] Set current project to fork-testing (in build file:/C:/dev/sandbox/fork-testing/) [info] Defining *:envVars [info] The new value will be used by *:runner, compile:run::runner and 1 others. [info]  Run `last` for details. [info] Reapplying settings... [info] Set current project to fork-testing (in build file:/C:/dev/sandbox/fork-testing/) [info] Running Hello [info] Hello, Chad 
like image 45
Jacek Laskowski Avatar answered Sep 20 '22 15:09

Jacek Laskowski