4

I issue the sbt publish command, and get a prompt asking me to enter a username and a password. Can I provide them in build.sbt or some place else, so I don't have to manually enter them?

In my build.sbt file I have this:

publishTo := Some(Resolver.sftp("Server", "url", "port"))

2 Answers2

6

You can put credentials in a file and reference in credentials.sbt so that sbt will load it, and use it while publishing or dependency download,

STEP1: setup creds file path in ~/.sbt/1.0/plugins/credentials.sbt

echo 'credentials += Credentials(Path.userHome / ".sbt" / ".credentials")' >  ~/.sbt/1.0/plugins/credentials.sbt

note: echo some-stuff > some-file will redirect contents to a file. echo is a linux command

STEP2: Your ~/.sbt/.credentials would look like,

realm=Artifactory Realm          // or Sonatype Nexus Repository Manager
host=server.com                  // don't put in http:// or https:// protocal
user=your.username.for.server.com
password=password.for.server.com

STEP3: setup publish config in build.sbt something like below:

publishTo in ThisBuild := {
  if (isSnapshot.value)
    Some("Artifactory Realm" at "server.com" +  "/artifactory/libs-snapshot-local")
  else
    Some("Artifactory Realm" at "server.com" + "/artifactory/libs-release-local")
}

STEP4: you can verify credentials.sbt is picked up by sbt, just by running sbt clean compile

$ sbt clean compile
[info] Loading settings for project global-plugins from idea.sbt,credentials.sbt ...

Related resources:

Official documentation: https://www.scala-sbt.org/1.0/docs/Publishing.html

How to access a secured Nexus with sbt?

SBT publish to JFrog artifactory

prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • "load it while publishing" how can I load white publishing, I don't understand that, sorry. Are you suggesting I enter echo 'credentials += ....' in the console, or what? –  Oct 30 '18 at 17:35
  • re-read the answer please. In STEP1: I m suggesting to create a file `~/.sbt/1.0/plugins/credentials.sbt` with content `credentials += Credentials(Path.userHome / ".sbt" / ".credentials")` – prayagupa Oct 30 '18 at 18:56
  • Sorry, long work day, brain isn't working, I will try once again tomorrow :) Thanks for the answer! –  Oct 30 '18 at 19:05
  • Okay, this doesn't work, it still asks me for credentials. I put all these files in the right place and tried writing values for realm, host, etc... in quotation marks and without them. Still no avail. –  Oct 31 '18 at 09:06
  • Realm I put is for jfrog artifactory. You might want to check your publish repo realm. And host only has server.com no http or other protocal at the beginning. Also make sure you can server.com with those username and password on browser – prayagupa Oct 31 '18 at 12:34
0

Look in the logs closely for any location where sbt might be looking for the credentials file. Ex: Unable to find credentials file /root.ivy2/.credentials. Notice that root and ivy2 are separated by a dot and not a slash. Create the above directory if it does not exist and place the credentials as shown above in the .credentials file probably using cat if the OS is linux or macos.

sbt version : 0.13