Christopher Helck
Christopher Helck

Reputation: 1140

SBT: How to access environment variable or configuration?

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")
}

Upvotes: 16

Views: 13788

Answers (1)

Eugene Zhulenev
Eugene Zhulenev

Reputation: 9734

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.

Upvotes: 30

Related Questions