How to “publish” a Scala/SBT library

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.15, “Publishing an SBT Library.”

Problem

You’ve created a Scala project or library with SBT that you want to share with other users, creating all the files you need for an Ivy repository.

Solution

Define your repository information, then publish your project with sbt publish or sbt publish-local.

For my SoundFilePlayer library, I added this setting to my build.sbt file to define the location of my local repository:

publishTo := Some(Resolver.file("file", new File("/Users/al/tmp")))

I then ran sbt publish, and SBT generated the following files:

$ sbt publish

[info] Wrote 
/Users/al/SoundFilePlayer/target/scala-2.10.0/sounds_2.10.0-1.0.pom
[info] :: delivering :: default#sounds_2.10.0;1.0 :: 1.0 :: release :: 
[info]   delivering ivy file to 
/Users/al/SoundFilePlayer/target/scala-2.10.0/ivy-1.0.xml
[info]   published sounds_2.10.0 to 
/Users/al/tmp/default/sounds_2.10.0/1.0/sounds_2.10.0-1.0.pom
[info]   published sounds_2.10.0 to 
/Users/al/tmp/default/sounds_2.10.0/1.0/sounds_2.10.0-1.0.jar
[info]   published sounds_2.10.0 to 
/Users/al/tmp/default/sounds_2.10.0/1.0/sounds_2.10.0-1.0-sources.jar
[info]   published sounds_2.10.0 to 
/Users/al/tmp/default/sounds_2.10.0/1.0/sounds_2.10.0-1.0-javadoc.jar
[success] Total time: 1s

Without doing anything to define a “local Ivy repository,” I get the following results when running the publish-local task:

$ sbt publish-local

[info] Wrote /Users/al/SoundFilePlayer/target/scala-2.10.0/sounds_2.10.0-1.0.pom
[info] :: delivering :: default#sounds_2.10.0;1.0 :: 1.0 :: release :: 
[info]   delivering ivy file to 
/Users/al/SoundFilePlayer/target/scala-2.10.0/ivy-1.0.xml
[info]   published sounds_2.10.0 to 
/Users/al/.ivy2/local/default/sounds_2.10.0/1.0/poms/sounds_2.10.0.pom
[info]   published sounds_2.10.0 to 
/Users/al/.ivy2/local/default/sounds_2.10.0/1.0/jars/sounds_2.10.0.jar
[info]   published sounds_2.10.0 to 
/Users/al/.ivy2/local/default/sounds_2.10.0/1.0/srcs/sounds_2.10.0-sources.jar
[info]   published sounds_2.10.0 to 
/Users/al/.ivy2/local/default/sounds_2.10.0/1.0/docs/sounds_2.10.0-javadoc.jar
[info]   published ivy to 
/Users/al/.ivy2/local/default/sounds_2.10.0/1.0/ivys/ivy.xml
[success] Total time: 1 s,

The “SBT Publishing” documentation provides these descriptions of the publish and publish-local tasks:

  • The publish action is used to publish your project to a remote repository. To use publishing, you need to specify the repository to publish to and the credentials to use. Once these are set up, you can run publish.
  • The publish-local action is used to publish your project to a local Ivy repository. You can then use this project from other projects on the same machine.

For more information on publishing to remote servers, repositories, and artifacts, see the SBT “Publishing” documentation.