scala SBT:如何访问环境变量或配置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26406013/
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 06:38:05  来源:igfitidea点击:

SBT: How to access environment variable or configuration?

scalabuildsbt

提问by user1879313

I publish to an internal Nexus repository. We have two repos, "dev" and "production". Developers use the dev repo, the build team uses the production repo which they access from machines in a secure area. I would like to add an environment variable or SBT config that defines STAGE with a default value of "dev". On the production build boxes STAGE would be overriden to "production". How can I do this? I am able to define stage in my build.sbt file and use it in the publishTo task, I just can't figure out how to get the value from the environment. Here is what I have.

我发布到内部 Nexus 存储库。我们有两个仓库,“dev”和“production”。开发人员使用 dev repo,构建团队使用他们从安全区域中的机器访问的生产 repo。我想添加一个定义 STAGE 的环境变量或 SBT 配置,默认值为“dev”。在生产构建框 STAGE 将被覆盖为“生产”。我怎样才能做到这一点?我能够在我的 build.sbt 文件中定义阶段并在 publishTo 任务中使用它,我只是不知道如何从环境中获取值。这是我所拥有的。

val stage = settingKey[String]("stage") 

stage := "dev"

publishTo <<= (version, stage) { (v: String, s: String) =>
  val nexus = "http://my-internal-nexus:8081/nexus/content/repositories/"
  if (v.trim.endsWith("SNAPSHOT"))
    Some("snapshots" at nexus + s + "-snapshots-m2")
  else
    Some("releases"  at nexus + s + "-releases-m2")
}

回答by Eugene Zhulenev

You can pass stage in a system property and read it into a setting:

您可以在系统属性中传递 stage 并将其读入设置:

stage := sys.props.getOrElse("stage", default = "dev")

Use sbt -Dstage=productionto pass this in your build environment.

用于sbt -Dstage=production在您的构建环境中传递它。