简体   繁体   English

使用“sbt run”设置系统属性

[英]Setting system properties with "sbt run"

I'm using a pretty recent version of SBT (seems to be hard to figure out what the version is).我正在使用一个相当新的 SBT 版本(似乎很难弄清楚这个版本是什么)。 I want to pass system properties to my application with sbt run<\/code> as follows:我想通过sbt run<\/code>将系统属性传递给我的应用程序,如下所示:

sbt -Dmyprop=x run

SBT's runner doesn't normally create new processes, so you also have to tell it to do this if you want to set the arguments that are passed. SBT的运行器通常不会创建新进程,因此如果要设置传递的参数,还必须告诉它执行此操作。 You can add something like this to your build settings: 您可以在构建设置中添加以下内容:

fork := true

javaOptions := Seq("-Dmx=1024M")

There's more detail on forking processes in the SBT documentation . 有关SBT文档中分叉过程的更多详细信息

I found the best way to be adding this to build.sbt : 我找到了将其添加到build.sbt的最佳方法:

// important to use ~= so that any other initializations aren't dropped
// the _ discards the meaningless () value previously assigned to 'initialize'
initialize ~= { _ =>
  System.setProperty( "config.file", "debug.conf" )
}

Related: When doing this to change the Typesafe Configuration that gets loaded (my use case), one needs to also manually include the default config. 相关:执行此操作以更改已加载的Typesafe配置(我的用例)时,还需要手动包含默认配置。 For this, the Typesafe Configuration's suggested include "application" wasn't enough but include classpath("application.conf") worked. 为此,Typesafe配置的建议include "application"是不够的,但include classpath("application.conf")工作。 Thought to mention since some others may well be wanting to override system properties for precisely the same reason. 考虑提及,因为其他一些人可能想要以完全相同的原因覆盖系统属性。

Source: discussion on the sbt mailing list 来源:关于sbt邮件列表的讨论

Thanks for the pointer, this actually helped me solve a somewhat related problem with Scala Tests. 感谢指针,这实际上帮助我解决了与Scala测试有关的问题。

It turned out that sbt does fork the tests when there are sub-projects (see my code ) and some of the tests fail to pick up the system property. 事实证明,当存在子项目时, sbt 会对测试进行分支(参见我的代码 ),并且一些测试无法获取系统属性。

So in sbt -Dsomething="some value" test , some of the tests would fail when failing to find something in the system properties (that happened to be my DB URI, so it kinda mattered!) 所以在sbt -Dsomething="some value" test ,一些测试会在找不到系统属性中的something时失败(这恰好是我的数据库URI,所以它有点重要!)

This was driving me nuts, so I thought I'd post it here for future reference for others (as @akauppi correctly noted, chances are high that "others" may well be me in a few weeks!). 这让我疯了,所以我想我会把它发布在这里供其他人参考(正如@akauppi正确指出的那样,很有可能“其他人”很可能在几周内成为我!)。

The fix was to add the following to build.st : 修复是将以下内容添加到build.st

fork in Test := false

I think the best is to use the JAVA_OPTS environment variable: 我认为最好是使用JAVA_OPTS环境变量:

#update the java options (maybe to keep previous options)
export JAVA_OPTS="${JAVA_OPTS} -Dmyprop=x"
#now run without any extra option
sbt run

You can pass system properties at the end of the sbt command:您可以在 sbt 命令的末尾传递系统属性:

sbt run -Dmyprop=x

If you have to pass program parameters into a stage, just pass system properties after the quotes again:如果您必须将程序参数传递到阶段,只需在引号后再次传递系统属性:

sbt "runMain com.example.MyClass -p param-value" -Dmyprop=x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM