Publishing from Scala SBT to Sonatype Open Source Repository
Publishing your open source library to the Maven repository (in particular Sonatype Central) has never been easier. The reference process is described by Sonatype in their OSSRH Guide and also by Lightbend in sbtâs Manual - Using Sonatype. This post intends to enhance these guides to provide you with a more complete process, as each of the guides cover only the basic integration scenario. Ultimately, we all want to have the release process automatic and solid as possible. Let us show you how to get there in few easy steps.
Setting up Sonatype
Both requirements for creating a new project in Sonatype repository are satisfied out-of-the-box if you have a public repository on BitBucket, GitHub, GitLab, or other.
SCM URL - publicly available repository with source code of your library - you need to be able to provide the URL pointing at this repository,
Project URL - publicly available homepage of the library - again, the URL is needed.
First of all, the OSSRH Guide walks you through the required process of setting up the account with Sonatype. Itâs as simple as creating a Sonatypeâs JIRA account and afterwards creating a New Project ticket. When creating the account try to use the same domain in your email address as the project is hosted on. I guess it makes it easier to validate the relationship with the groupId requested in the ticket. Creation of the New Project ticket is as simple as:
providing the name of the library in the ticketâs subject,
naming the groupId you want to use for distributing the library (make sure it is matching the root package of your code). Sonatype provides you with additional hints on choosing the right groupId for publishing your library in Choosing your coordinates guide.
providing the SCM and Project URLs to the source code and homepage of the library.
After creating yourself the Sonatype account (on their JIRA) you can login using the same credentials to the Nexus Repository Manager which is not required to be used in this guide, but can be used later to check on the published artifacts.
Notice that Sonatype advises that responding to the New Project ticket might take up to two business days, but in our case it was ⌠3 minutes. So donât expect much delay before moving on to...
Setting up SBT
Following the OSSRH Guide, you now want to review the requirements of publishing to the central Sonatype repository. These are relatively detailed, but describe (in 2017) the configuration of Maven, which is only partially useful for SBT. Another not-very-helpful page of deployment with sbt provides some rope-ends but lacks useful information on how to proceed with sbt (hence this post). Sonatypeâs recommendation here is to follow the Lightbend in sbtâs Manual - Using Sonatype, which indeed is helpful at this stage, but does not address the requirements (addresses only signing the package) and is missing information on the process to release (not just stage) the library.
The easiest path weâve found to build a complete release process with sbt is as follows:
Install the GNU Privacy Guard and:
use it to generate the keypair you will use to sign your library,
publish your certificate to enable code signature verification,
make sure that the âgpgâ command is in PATH (available to the sbt).
Once you have your public certificate install the sbt-pgp plugin in your userâs local sbt installation and tell it to use the installed GnuPG. In build.sbt:
useGpg := true
Install the sbt-sonatype plugin and configure it (notice that you can skip the sbt-pgp plugin as it is already installed). make sure to set the organization mapping in build.sbt to the groupId that you used in the New Project ticket. In build.sbt:
organization := "some groupId"
Install the sbt-release plugin. The configuration example provided in the sbt-sonatype plugin is pretty comprehensive but does not include opening of the repository, which results in the demo not working as (at least myself) expected. Fortunately this can be achieved by adding a ReleaseStep calling the sonatypeOpen command of the sbt-sonatype plugin.
Here is the modified example of changes to build.sbt that address this issue into consideration:
import sbtrelease.ReleasePlugin.autoImport.ReleaseTransformations._ releaseProcess := Seq[ReleaseStep]( checkSnapshotDependencies, inquireVersions, runClean, runTest, setReleaseVersion, commitReleaseVersion, tagRelease, ReleaseStep(action = Command.process("sonatypeOpen \"your groupId\" \"Some repository name\"", _)), ReleaseStep(action = Command.process("publishSigned", _)), setNextVersion, commitNextVersion, ReleaseStep(action = Command.process("sonatypeReleaseAll", _)), pushChanges )
Releasing
To get your project built, tested, tagged, signed, published, and pushed (all in one) execute:
sbt release
But... if things go awry you can try to manually replicate the publishing process (weâll skip the building, testing, tagging, and pushing as obvious ones). There are many things that can go wrong, but you should be able to start debugging following these simple steps:
Open the staging repository:
sonatypeOpen "your groupId" "Some staging name"
Sign and publish to staging repository
publishSigned
Check the published artifact in the Nexus Repository Manager (same login as Sonatype Jira)
Close the staging repository and promote the release to central
sonatypeRelease
Each of the above steps are essentially sbt-sonatype pluginâs wrappers of the Sonatype API so if you want to dig even deeper here is the documentation of the publishing workflows to assist you.







