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

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

我想能够运行以下命令:

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

无论fork是否为true,是否将它放在SBT_OPTS ,或者如何将其传入,我都无法完成此操作。 我不熟悉在build中定义的默认值时,在命令行设置设置值吗? 并设置系统属性与“sbt运行”,但既不回答我的问题。

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

这工作:

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

如果这不是一个“友好的”足够的语法,把它包装在一个小的shell脚本。

(请注意,假设你的fork已经设置为true,否则请参阅akauppi的评论。)

你可以使用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 

runMain在这种情况下run没有什么不同。

 $ 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 

如果你正在尝试设置SBT属性,比如插件设置,那么根据我的经验,上述内容将不起作用(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") 

从命令行

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

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