scala 如何在 sbt 中为我的项目设置系统属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3127007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:15:57  来源:igfitidea点击:

How do I set a system property for my project in sbt?

scalaprojectsbtsystem-properties

提问by pr1001

I'm sure I'm missing something really simple... I want to set the system property java.awt.headlessto truefor my sbt project. Reading the page on propertiesI think that I need to use systemor systemOptional. In my project file I've tried things like:

我敢肯定,我失去了一些东西很简单......我想设置系统属性java.awt.headlesstrue为我的SBT项目。阅读有关属性页面,我认为我需要使用systemsystemOptional。在我的项目文件中,我尝试了以下操作:

lazy val javaAwtHeadless = system[Boolean]("java.awt.headless")

Setting it as a user property (e.g. lazy val javaAwtHeadless = property[Boolean]) and setting the accompanying value in build.propertiesmade the property visible in the sbt console but not within sbt's Scala console (via System.getProperty("java.awt.headless")).

将其设置为用户属性(例如lazy val javaAwtHeadless = property[Boolean])并设置随附的值build.properties使该属性在 sbt 控制台中可见,但在 sbt 的 Scala 控制台中不可见(通过System.getProperty("java.awt.headless"))。

set java.awt.headless truefrom the sbt console works, including being set in the Scala console, but it doesn't persist to the next time I launch sbt.

set java.awt.headless true从 sbt 控制台工作,包括在 Scala 控制台中设置,但它不会持续到我下次启动 sbt 时。

采纳答案by retronym

A straightforward method would be to edit the batch file or shell script that you use to run sbtand add -Dprop=val

一种直接的方法是编辑用于运行sbt和添加的批处理文件或 shell 脚本-Dprop=val

回答by jazmit

If I needed this option for all sbt tasks, I'd set it as follows in build.sbt

如果我对所有 sbt 任务都需要这个选项,我会在 build.sbt 中如下设置

javaOptions += "-Djava.awt.headless=true" 

If it was just for one task, eg: run, you can scope that:

如果它仅用于一项任务,例如:运行,您可以确定:

javaOptions in Runtime += "-Djava.awt.headless=true" 

回答by Joseph Lust

If you're trying to set SBT properties, like plugin settings, then the following worked for me with 0.13+. The following however did work, when trying to pass in Liquibase settings, like password, from our CI frameworks.

如果您尝试设置 SBT 属性,例如插件设置,那么以下内容适用于0.13+. 然而,当尝试从我们的 CI 框架传递 Liquibase 设置(如密码)时,以下确实有效。

In your build.sbt

在你的 build.sbt

Ugly, but supplies defaults, and optionally grabs from System.properties. This way you've got your default and override cases covered.

丑陋,但提供默认值,并可选择从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")

From the commandline

从命令行

Now just override via -Dprop=valuelike you would with Maven or other JVM programs. Note props appear before SBT task.

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

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

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