scala 如何在 build.sbt 中使用环境变量?

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

How to use Environment Variables in build.sbt?

scalasbt

提问by Glef

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

我想在使用 built.sbt 时使用环境变量来传递我的 repo 凭据。

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.

我还尝试了 ~/.sbt/ 下的凭据文件,但我不确定如何向其中添加环境变量。

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.

附加:我在运行 sbt compile 之前在 shell 中获取环境变量。

Running

跑步

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

results in a forbidden url error.

导致禁止的 url 错误。

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.

我收到一个 FORBIDDEN URL 错误,这表明 Scala 部分运行正常,但由于某种原因,凭据仍然不正确。如果我在 build.sbt 中明确设置凭据,它就可以工作。

回答by Pietrotull

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

您可以使用提到的其他响应的命令获取 env 变量,例如:

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

but they return an option of type Option[String]

但它们返回一个 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.

如果您需要设置一些默认值。警告!调用没有值的选项的 .get 会给你一个运行时错误。

回答by OlivierBlanvillain

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

你可以在 sbt 中使用任何在 Scala 中有效的东西,例如:

sys.env.get("PASSWORD")

回答by Don Branson

Adding your code to the example, sending Credentials strings for user id and password:

将您的代码添加到示例中,发送用户 ID 和密码的凭据字符串:

(sys.env.get("USERNAME"), sys.env.get("PASSWORD")) match {
  case (Some(username), Some(password)) => 
    println(s"Do my thing $username/$password")
    credentials += Credentials("Realm", "my.artifact.repo.net", username, password)
  case _ => 
    println("USERNAME and/or PASSWORD is missing")
    credentials ++= Seq()
}

回答by Justin Kaeser

Getting the environment variables via sys.env("VARIABLE")or System.getenv("VARIABLE")will both work in build.sbt if they are correctly exported.

如果正确导出,通过sys.env("VARIABLE")或获取环境变量System.getenv("VARIABLE")将在 build.sbt 中工作。

You may need to setup your credentials correctly (general instructions).

您可能需要正确设置您的凭据(一般说明)。

For Artifactory, it should look like this:

对于 Artifactory,它应该是这样的:

publishTo := Some("Artifactory Realm" at "http://<host>:<port>/artifactory/<repo-key>")
credentials += Credentials("Artifactory Realm", "<host>", "<USERNAME>", "<PASS>")

Note that "Artifactory Realm" here is not an arbitrary string and must actually be exactly as stated.

请注意,此处的“Artifactory Realm”不是任意字符串,实际上必须与所述完全相同。

Full instructions: https://www.jfrog.com/confluence/display/RTF/SBT+Repositories

完整说明:https: //www.jfrog.com/confluence/display/RTF/SBT+Repositories

回答by Raman Mishra

You can also use

你也可以使用

val config :Config = ConfigFactory.load()
val username=config.getString(“USERNAME”)
val password = config.getString(“PASSWORD”)

回答by Michel Hua

(sys.env.get("USERNAME"), sys.env.get("PASSWORD")) match ...didn't work with sbt 1.1.1.

(sys.env.get("USERNAME"), sys.env.get("PASSWORD")) match ...不适用于 sbt 1.1.1。

Alternative solution from JFrog, here is a working example on github

JFrog 的替代解决方案,这是 github 上的一个工作示例

https://github.com/jfrog/project-examples/tree/master/circleci-example/circleci-sbt-artifactory

https://github.com/jfrog/project-examples/tree/master/circleci-example/circleci-sbt-artifactory

Create a credentials.propertiesfile at the root of your project instead of ~/.sbt/credentials. Add it to .gitignore.

credentials.properties在项目的根目录创建一个文件而不是~/.sbt/credentials. 将其添加到.gitignore.

cat <<EOF > credentials.properties
realm=Artifactory Realm
host=gcartifactory-us.jfrog.info
user=$ARTIFACTORY_USERNAME
password=$ARTIFACTORY_PASSWORD
EOF

Generate the file during before build time and make a reference to it in your build.sbt

在构建时间之前生成文件并在您的 build.sbt

credentials += Credentials(new File("credentials.properties"))