Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Environment Variables in build.sbt?

Tags:

scala

sbt

I would like to use environment variables to pass my repo credentials when using built.sbt.

I have tried things like this but the details arent picked up.

credentials += Credentials("Some Nexus Repository Manager", "my.artifact.repo.net", System.getenv("USERNAME"), System.getenv("PASSWORD")) 

I have also tried a credentials file under ~/.sbt/ but I'm not sure how to add environment variables to that.

If I just type my username and password normally in the credentials file it works so I know that the log in details are ok.

Additional: I source the environment vars in a shell before running sbt compile.

Running

credentials += Credentials("Realm", "my.artifact.repo.net", sys.env("USERNAME"), sys.env("PASSWORD")) 

results in a forbidden url error.

I should say I'm stuck trying to resolve dependencies

UPDATE: The Following returns the correct value

eval scala.sys.env("ARTIFACTORY_USERNAME") 

But when I add this into my script

val username = scala.sys.env("ARTIFACTORY_USERNAME") val password = scala.sys.env("ARTIFACTORY_PASSWORD") credentials += Credentials("Artifactory Realm", "artifactory.link.io", username, password)  resolvers ++= Seq( "Artifactory Snapshot" at "https://artifactory.link.io/art/libs-snapshot" ) 

or

credentials += Credentials("Artifactory Realm", "my.artifact.repo.net", sys.env("ARTIFACTORY_USERNAME"), sys.env("ARTIFACTORY_PASSWORD"))    

I get a FORBIDDEN URL error which suggests that the scala part runs ok but for some reason the credentials are still incorrect. If I explicitly set the credentials in the build.sbt it works.

like image 966
Glef Avatar asked Apr 27 '17 14:04

Glef


People also ask

What is provided in build sbt?

In that case, we can mark that dependency as “provided” in our build. sbt file. The “provided” keyword indicates that the dependency is provided by the runtime, so there's no need to include it in the JAR file. When using sbt-assembly, we may encounter an error caused by the default deduplicate merge strategy.

Is sbt a build tool?

sbt is an open-source build tool for Scala and Java projects, similar to Apache's Maven and Ant. Its main features are: Native support for compiling Scala code and integrating with many Scala test frameworks. Continuous compilation, testing, and deployment.

Can sbt build Java?

sbt has support for compiling Java sources with the limitation that dependency tracking is limited to the dependencies present in compiled class files.


2 Answers

You can get env variables with the commands mentioned other responses like:

sys.env.get("USERNAME") sys.env.get("PASSWORD") 

but they return an option of type Option[String]

To turn this into string you need to either do a match or simply use

sys.env.get("USERNAME").get sys.env.get("USERNAME").getOrElse("some default value") 

if you need to set some default value. Warning! calling .get of an option that does not have value will give you a runtime error.

like image 199
Pietrotull Avatar answered Oct 14 '22 07:10

Pietrotull


You can use anything that works in Scala in sbt, for instance:

sys.env.get("PASSWORD") 
like image 36
OlivierBlanvillain Avatar answered Oct 14 '22 05:10

OlivierBlanvillain