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.
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.
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.
sbt has support for compiling Java sources with the limitation that dependency tracking is limited to the dependencies present in compiled class files.
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.
You can use anything that works in Scala in sbt, for instance:
sys.env.get("PASSWORD")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With