How do I publish a scala app, if I'm asked for credentials?

Viewed 835

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 Answers

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

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

Related