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

Cha*_*etz 41 sbt

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

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

sbt -Dconfig.resource=../application.conf "runMain akka.Main com.my.main.Actor"
Run Code Online (Sandbox Code Playgroud)

无论是否fork属实,我是否把它放入SBT_OPTS,或者我如何通过它我无法做到这一点.在构建中没有定义默认值时,我熟悉命令行设置的设置值吗?使用"sbt run"设置系统属性,但都没有回答我的问题.

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

Set*_*sue 43

这有效:

sbt '; set javaOptions += "-Dconfig.resource=../application.conf" ; runMain akka.Main com.my.main.Actor'
Run Code Online (Sandbox Code Playgroud)

如果这不是一个"友好"足够的语法,请将其包装在一个小的shell脚本中.

(注意这假设你已经fork设置为true来运行.如果你没有,请参阅akauppi的评论.)

  • 我认为这需要启用'fork'.没有叉子(这是我的需要)通过以下方式工作:sbt'; eval System.setProperty("config.file","fn.conf"); 测试' (6认同)
  • 在覆盖多个选项时,为了简洁起见,替换为`javaOptions ++ = Seq(...)`. (2认同)
  • 如果您使用“subproject/runMain”,那么它就是“subproject/javaOptions”。很容易忘记。 (2认同)

Jac*_*ski 17

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

> help envVars
Environment variables used when forking a new JVM
Run Code Online (Sandbox Code Playgroud)

以下(非常简约)build.sbt工作正常.

fork := true

envVars := Map("msg" -> "hello")
Run Code Online (Sandbox Code Playgroud)

一旦你运行它,设置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.
Run Code Online (Sandbox Code Playgroud)

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

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

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答.我希望Java系统属性,但我想我可以通过"set"设置javaOptions.我希望我的管理员能够以更友好的方式提供他的配置路径,但看起来我只需创建一个自定义任务或其他东西. (2认同)

Jos*_*ust 6

如果您正在尝试设置SBT属性,例如插件设置,那么根据0.13+我的经验,上述内容将无效(AFAICT).然而,当我们尝试从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")
Run Code Online (Sandbox Code Playgroud)

从命令行

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

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