A sbt plugin for publishing Scala/Java projects to the Maven central.
Scala Shell
Latest commit 20d1c78 Feb 6, 2017 @xerial committed on GitHub Merge pull request #36 from raboof/feature/improve-staging-profile-no…
…t-found-error

Better error when staging profile is missing (#35)

README.md

sbt-sonatype plugin

A sbt plugin for publishing your project to the Maven central repository through the REST API of Sonatype Nexus. Deploying artifacts to Sonatype repository is a requirement for synchronizing your projects to the Maven central repository. sbt-sonatype plugin enables two-step release of Scala/Java projects.

  • First publishSigned (with sbt-pgp plugin)
  • Next sonatypeRelease to perform the close and release steps in the Sonatype Nexus repository.
  • Done. Your project will be synchronized to the Maven central within tens of minutes. No longer need to enter the web interface of Sonatype Nexus repository.

Prerequisites

Configurations

project/plugins.sbt

Import sbt-sonatype plugin and sbt-pgp plugin to use sonatypeRelease and publishSigned commands:

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") // fot sbt-0.13.5 or higher

$HOME/.sbt/(sbt-version)/sonatype.sbt

Set Sonatype account information (user name and password) in the global sbt settings. To protect your password, never include this file within your project.

credentials += Credentials("Sonatype Nexus Repository Manager",
        "oss.sonatype.org",
        "(Sonatype user name)",
        "(Sonatype password)")

(project root)/sonatype.sbt

sbt-sonatype is an auto-plugin, it will automatically configure your build. There are a few settings though that you need to define yourself:

  • sonatypeProfileName
    • This is your Sonatype acount profile name, e.g. org.xerial. If you do not set this value, it will be the same with the organization value.
  • pomExtra
// Your profile name of the sonatype account. The default is the same with the organization value
sonatypeProfileName := "org.xerial"

// To sync with Maven central, you need to supply the following information:
pomExtra in Global := {
  <url>(your project URL)</url>
  <!-- License of your choice -->
  <licenses>
    <license>
      <name>Apache 2</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    </license>
  </licenses>
  <!-- SCM information. Modify the following URLs -->
  <scm>
    <connection>scm:git:github.com/(your repository URL)</connection>
    <developerConnection>scm:git:git@github.com:(your repository URL)</developerConnection>
    <url>github.com/(your repository url)</url>
  </scm>
  <!-- Developer contact information -->
  <developers>
    <developer>
      <id>(your favorite id)</id>
      <name>(your name)</name>
      <url>(your web page)</url>
    </developer>
  </developers>
}

Publishing Your Artifact

The general steps for publishing your artifact to the Central Repository are as follows:

  • publishSigned to deploy your artifact to staging repository at Sonatype.
  • sonatypeRelease do sonatypeClose and sonatypePromote in one step.
    • sonatypeClose closes your staging repository at Sonatype. This step verifies Maven central sync requirement, GPG-signature, javadoc and source code presence, pom.xml settings, etc.
    • sonatypePromote command verifies the closed repository so that it can be synchronized with Maven central.

Note: If your project version has "SNAPSHOT" suffix, your project will be published to the snapshot repository of Sonatype, and you cannot use sonatypeRelease command.

Command Line Usage

Publish a GPG-signed artifact to Sonatype:

$ sbt publishSigned

Do close and promote at once:

$ sbt sonatypeRelease

This command accesses Sonatype Nexus REST API, then sends close and promote commands.

Available Commands

  • sonatypeList
    • Show the list of staging repositories.
  • sonatypeOpen (description | sonatypeProfileName description) (since sbt-sonatype-1.1)
    • Create a staging repository and set sonatypeStagingRepositoryProfile and publishTo.
    • Although creating a staging repository does not result in email notifications, the description will be reused for across lifecycle operations (Close, Promote, Drop) to facilitate distinguishing email notifications sent by the repository by description.
  • sonatypeClose (repositoryId)?
    • Close an open staging repository and set sonatypeStagingRepositoryProfile and clear publishTo if it was set by sonatypeOpen.
    • The Staging Completed email notification sent by the repository only includes the description (if created with sonatypeOpen); it does not include the staging repository ID.
  • sonatypePromote (repositoryId)?
    • Promote a closed staging repository and set sonatypeStagingRepositoryProfile and clear publishTo if it was set by sonatypeOpen.
    • The Promotion Completed email notification sent by the repository only includes the description (if created with sonatypeOpen); it does not include the staging repository ID.
  • sonatypeDrop (repositoryId)?
    • Drop an open or closed staging repository and set sonatypeStagingRepositoryProfile and clear publishTo if it was set by sonatypeOpen.
    • The email notification sent by the repository includes both the description (if created with sonatypeOpen) and the staging repository ID.
  • sonatypeRelease (repositoryId)?
    • Close (if needed) and promote a staging repository and set sonatypeStagingRepositoryProfile and clear publishTo if it was set by sonatypeOpen.
    • The email notifications are those of sonatypeClose (if applicable) and sonatypePromote.
  • sonatypeReleaseAll (sonatypeProfileName)?
    • Close and promote all staging repositories (Useful for cross-building projects)
  • sonatypeStagingProfiles
    • Show the list of staging profiles, which include profileName information.
  • sonatypeLog
    • Show the staging activity logs

Advanced Usage

Using with sbt-release plugin

To perform publishSigned and sonatypeReleaseAll with sbt-release plugin, define your custom release process as follows:

import ReleaseTransformations._

releaseProcess := Seq[ReleaseStep](
  checkSnapshotDependencies,
  inquireVersions,
  runClean,
  runTest,
  setReleaseVersion,
  commitReleaseVersion,
  tagRelease,
  ReleaseStep(action = Command.process("publishSigned", _)),
  setNextVersion,
  commitNextVersion,
  ReleaseStep(action = Command.process("sonatypeReleaseAll", _)),
  pushChanges
)

To enable cross building, set enableCrossBuild = true in publishSigned and sonatypeReleaseAll release steps:

  ReleaseStep(action = Command.process("publishSigned", _), enableCrossBuild = true)
  ...
  ReleaseStep(action = Command.process("sonatypeReleaseAll", _), enableCrossBuild = true)

Publishing Maven Projects

If your Maven project (including Gradle, etc.) is already deployed to the staging repository of Sonatype, you can use sbt sonatypeReleaseAll (sonatypeProfileName) command for the synchronization to the Maven central (Since version 0.5.1).

Prepare the following two files:

$HOME/.sbt/0.13/plugins/plugins.sbt

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")

$HOME/.sbt/0.13/sonatype.sbt

credentials += Credentials("Sonatype Nexus Repository Manager",
        "oss.sonatype.org",
        "(Sonatype user name)",
        "(Sonatype password)")

Then, run sonatypeReleaseAll command by specifying your sonatypeProfileName. If this is org.xerial, run:

$ sbt "sonatypeReleaseAll org.xerial"