如何在命令行上为runMain设置系统属性?

时间:2014-01-27 19:02:41

标签: sbt

如何在Windows上从命令行执行runMain时设置系统属性?

我希望能够运行以下命令:

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

无论fork是否属实,我是否将其放入SBT_OPTS,或者如何将其传递给我,我无法做到这一点。我熟悉Setting value of setting on command line when no default value defined in build?Setting system properties with "sbt run",但都没有回答我的问题。

其他问题似乎表明您甚至无法在SBT中轻松查看Java调用参数。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:41)

这有效:

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

如果这不是一个“友好”的语法,请将其包装在一个小的shell脚本中。

(注意这假设您将fork设置为true以进行运行。如果不这样做,请参阅akauppi的评论。)

答案 1 :(得分:17)

您可以使用envVars设置。不过,我不确定它在SBT中是多么惯用。

> help envVars
Environment variables used when forking a new JVM

以下(极简主义)build.sbt工作正常。

fork := true

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

运行后,将envVars设置为set的任何值即可。

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

我有一个简单的应用程序可以运行。

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

在命令行中更改envVars设置后,输出将更改如下:

$ 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
在这种情况下,

runMainrun没有区别。

$ 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

答案 2 :(得分:6)

如果您尝试设置SBT属性,例如插件设置,那么根据我的经验,上述内容将不起作用(AFAICT)0.13+。然而,当我们尝试从CI框架传递Liquibase设置(如密码)时,以下工作确实有效。

在你的build.sbt

很丑,但提供默认设置,并可选择从 System.properties 获取。通过这种方式,您可以覆盖默认和覆盖案例。

def sysPropOrDefault(propName:String,default:String):String = Option(System.getProperty(propName)).getOrElse(default)

liquibaseUsername := sysPropOrDefault("liquibase.username","change_me")
liquibasePassword := sysPropOrDefault("liquibase.password","chuck(\)orris")

从命令行

现在只需通过-Dprop=value覆盖,就像使用Maven或其他JVM程序一样。注意道具出现在SBT任务之前。

sbt -Dliquibase.password="shh" -Dliquibase.username="bob" liquibase:liquibase-update