Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT: How to access environment variable or configuration?

Tags:

scala

build

sbt

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.

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")
}
like image 528
Christopher Helck Avatar asked Oct 16 '14 13:10

Christopher Helck


People also ask

How do you set environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do I set an environment variable in terminal?

To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.


1 Answers

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

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

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

like image 87
Eugene Zhulenev Avatar answered Sep 16 '22 16:09

Eugene Zhulenev