Skip to content

Instantly share code, notes, and snippets.

@joecwu
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joecwu/0e6716233d0ada48ba21 to your computer and use it in GitHub Desktop.
Save joecwu/0e6716233d0ada48ba21 to your computer and use it in GitHub Desktop.
Blog Publish Own Library to Sonatype Nexus Repository (http://blog.joecwu.com/2015/07/librarysonatype-nexus-repostiroy.html)

Publish Artifacts to Sonatype Nexus Repository by using SBT

Install PGP plugin Reference

For sbt 0.13.5+: Add the following to your ~/.sbt/0.13/plugins/gpg.sbt file:

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")

PGP Gen Key Reference

> pgp-cmd gen-key
java.lang.RuntimeException: Cannot modify keyrings when in read-only mode.  Run `set pgpReadOnly := false` before running this command.

> set pgpReadOnly := false
[info] Defining */*:pgpReadOnly

> pgp-cmd gen-key
Please enter the name associated with the key: joecwu
Please enter the email associated with the key: joe623@gmail.com
Please enter the passphrase for the key: *********
Please re-enter the passphrase for the key: *********
[info] Creating a new PGP key, this could take a long time.
[info] Public key := C:\Users\Joe\.sbt\gpg\pubring.asc
[info] Secret key := C:\Users\Joe\.sbt\gpg\secring.asc
[info] Please do not share your secret key.   Your public key is free to share.

Upload public key Reference

> pgp-cmd send-key joecwu hkp://pool.sks-keyservers.net
[info] Sending PublicKeyRing(PublicKey(a4182f2a8c5e77fa, joecwu <joe623@gmail.com>, RSA@2048)) to HkpServer(http://pool.sks-keyservers.net:11371)
INF: [console logger] dispatch: pool.sks-keyservers.net POST /pks/add HTTP/1.1

Deploy to Sonatype Reference

Credentials ~/.sbt/sonatype.sbt

credentials += Credentials("Sonatype Nexus Repository Manager",
                           "oss.sonatype.org",
                           "your-sonatype-username",
                           "your-sonatype-password")

Setup build.sbt

// Your profile name of the sonatype account. The default is the same with the organization value

sonatypeProfileName := "com.joecwu"

publishTo := {
  val nexus = "https://oss.sonatype.org/"
  if (isSnapshot.value)
    Some("snapshots" at nexus + "content/repositories/snapshots")
  else
    Some("releases" at nexus + "service/local/staging/deploy/maven2")
}

organization := "com.joecwu"

publishMavenStyle := true

publishArtifact in Test := false

pomIncludeRepository := { _ => false }

pomExtra in Global := (
    <url>http://blog.joecwu.com/search/label/jcwutil</url>
      <licenses>
        <license>
          <name>Apache License, Version 2.0</name>
          <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
          <distribution>repo</distribution>
        </license>
      </licenses>
      <scm>
        <url>https://github.com/joecwu/jcwutil</url>
        <connection>scm:git:git://github.com/joecwu/jcwutil.git</connection>
        <developerConnection>scm:git:git@github.com:joecwu/jcwutil.git</developerConnection>
      </scm>
      <developers>
        <developer>
          <id>joecwu</id>
          <name>Joe Wu</name>
          <email>joecwu@gmail.com</email>
          <url>https://github.com/joecwu</url>
        </developer>
      </developers>
    )

Publish Local for testing

> publishLocalSigned
[info] Done updating.
[info] :: delivering :: default#jcwutil_2.11;0.0.1 :: 0.0.1 :: release :: Wed Jul 22 01:59:08 CST 2015
[info]  delivering ivy file to S:\IdeaWorkspace\jcwutil\target\scala-2.11\ivy-0.0.1.xml
[info] Main Scala API documentation to S:\IdeaWorkspace\jcwutil\target\scala-2.11\api...
[info] 'compiler-interface' not yet compiled for Scala 2.11.6. Compiling...
[info] Compiling 1 Scala source to S:\IdeaWorkspace\jcwutil\target\scala-2.11\classes...
[info]   Compilation completed in 10.787 s
[warn] there were two feature warnings; re-run with -feature for details
model contains 7 documentable templates
[warn] there were two feature warnings; re-run with -feature for details
[warn] one warning found
[warn] No main class detected
[info] Packaging S:\IdeaWorkspace\jcwutil\target\scala-2.11\jcwutil_2.11-0.0.1.jar ...
[info] Done packaging.
[warn] one warning found
[info] Main Scala API documentation successful.
[info] Packaging S:\IdeaWorkspace\jcwutil\target\scala-2.11\jcwutil_2.11-0.0.1-javadoc.jar ...
[info] Done packaging.
Please enter PGP passphrase (or ENTER to abort): *********
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\poms\jcwutil_2.11.pom.asc
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\jars\jcwutil_2.11.jar
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\docs\jcwutil_2.11-javadoc.jar.asc
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\jars\jcwutil_2.11.jar.asc
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\srcs\jcwutil_2.11-sources.jar
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\srcs\jcwutil_2.11-sources.jar.asc
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\poms\jcwutil_2.11.pom
[info]  published jcwutil_2.11 to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\docs\jcwutil_2.11-javadoc.jar
[info]  published ivy to C:\Users\Joe\.ivy2\local\default\jcwutil_2.11\0.0.1\ivys\ivy.xml

Publish

    > publishSigned
    [info] Wrote S:\IdeaWorkspace\jcwutil\target\scala-2.11\jcwutil_2.11-0.0.1.pom
    [info] :: delivering :: default#jcwutil_2.11;0.0.1 :: 0.0.1 :: release :: Wed Jul 22 03:05:00 CST 2015
    [info]  delivering ivy file to S:\IdeaWorkspace\jcwutil\target\scala-2.11\ivy-0.0.1.xml
    [warn] No main class detected
    Please enter PGP passphrase (or ENTER to abort): *********
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1.pom.asc
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1.jar
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1-javadoc.jar.asc
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1.jar.asc
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1-sources.jar
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1-sources.jar.asc
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1.pom
    [info]  published jcwutil_2.11 to https://oss.sonatype.org/service/local/staging/deploy/maven2/default/jcwutil_2.11/0.0.1/jcwutil_2.11-0.0.1-javadoc.jar
    [success] Total time: 15 s, completed 2015/7/22 上午 03:05:16

Release by using sbt-sonatype plugin (https://github.com/xerial/sbt-sonatype)

    > sonatypeRelease
    [info] Nexus repository URL: https://oss.sonatype.org/service/local
    [info] Reading staging repository profiles...
    [info] Reading staging profiles...
    [info] Closing staging repository [comjoecwu-1001] status:open, profile:com.joecwu(23f357c5fbaeb2)
    [info] Activity open started:2015-07-21T19:58:00.527Z, stopped:2015-07-21T19:58:02.379Z
    [info] repositoryCreated: id:comjoecwu-1001, user:joecwu, ip:122.116.184.156
    [info] Activity close started:2015-07-21T19:58:28.109Z, stopped:
    [info]   Evaluate: id:5e9e8e6f8d20a3, rule:sources-staging
    [info]   Evaluate: javadoc-staging
    [info]     Passed: javadoc-staging
    [info]   Evaluate: signature-staging
    [info]     Passed: signature-staging
    [info]   Evaluate: checksum-staging
    [info]     Passed: checksum-staging
    [info]   Evaluate: sources-staging
    [info]     Passed: sources-staging
    [info]   Evaluate: pom-staging
    [info]     Passed: pom-staging
    [info]     Passed: id:5e9e8e6f8d20a3
    [info]      email: to:joe623@gmail.com
    [info] repositoryClosed: id:comjoecwu-1001
    [info] Closed successfully
    [info] Promoting staging repository [comjoecwu-1001] status:closed, profile:com.joecwu(23f357c5fbaeb2)
    [info] Activity release started:2015-07-21T19:58:37.359Z, stopped:
    [info]   Evaluate: id:5e9e8e6f8d20a3, rule:sources-staging
    [info]   Evaluate: javadoc-staging
    [info]     Passed: javadoc-staging
    [info]   Evaluate: pom-staging
    [info]     Passed: pom-staging
    [info]   Evaluate: checksum-staging
    [info]     Passed: checksum-staging
    [info]   Evaluate: sources-staging
    [info]     Passed: sources-staging
    [info]   Evaluate: signature-staging
    [info]     Passed: signature-staging
    [info]     Passed: id:5e9e8e6f8d20a3
    [info]   Evaluate: id:nx-internal-ruleset, rule:RepositoryWritePolicy
    [info]   Evaluate: RepositoryWritePolicy
    [info]     Passed: RepositoryWritePolicy
    [info]     Passed: id:nx-internal-ruleset
    [info]  copyItems: source:comjoecwu-1001, target:releases
    [info]      email: to:joe623@gmail.com
    [info] repositoryReleased: id:comjoecwu-1001, target:releases
    [info] Promoted successfully
    [info] Dropping staging repository [comjoecwu-1001] status:released, profile:com.joecwu(23f357c5fbaeb2)
    [info] Dropped successfully: comjoecwu-1001

Tips

Failed to run sonatypeRelease

> sonatypeRelease
[info] Nexus repository URL: https://oss.sonatype.org/service/local
[info] Reading staging repository profiles...
java.lang.IllegalStateException: No staging repository is found. Run publishSigned first
        at xerial.sbt.Sonatype$NexusRESTService.findTargetRepository(Sonatype.scala:380)
        at xerial.sbt.Sonatype$SonatypeCommand$$anonfun$5.apply(Sonatype.scala:133)
        at xerial.sbt.Sonatype$SonatypeCommand$$anonfun$5.apply(Sonatype.scala:131)
        at sbt.Command$$anonfun$applyEffect$1$$anonfun$apply$2.apply(Command.scala:58)
        at sbt.Command$$anonfun$applyEffect$1$$anonfun$apply$2.apply(Command.scala:58)
        at sbt.Command$$anonfun$applyEffect$2$$anonfun$apply$3.apply(Command.scala:60)
        at sbt.Command$$anonfun$applyEffect$2$$anonfun$apply$3.apply(Command.scala:60)
        at sbt.Command$.process(Command.scala:92)
        at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:98)
        at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:98)
        at sbt.State$$anon$1.process(State.scala:184)
        at sbt.MainLoop$$anonfun$1.apply(MainLoop.scala:98)
        at sbt.MainLoop$$anonfun$1.apply(MainLoop.scala:98)
        at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
        at sbt.MainLoop$.next(MainLoop.scala:98)
        at sbt.MainLoop$.run(MainLoop.scala:91)
        at sbt.MainLoop$$anonfun$runWithNewLog$1.apply(MainLoop.scala:70)
        at sbt.MainLoop$$anonfun$runWithNewLog$1.apply(MainLoop.scala:65)
        at sbt.Using.apply(Using.scala:24)
        at sbt.MainLoop$.runWithNewLog(MainLoop.scala:65)
        at sbt.MainLoop$.runAndClearLast(MainLoop.scala:48)
        at sbt.MainLoop$.runLoggedLoop(MainLoop.scala:32)
        at sbt.MainLoop$.runLogged(MainLoop.scala:24)
        at sbt.StandardMain$.runManaged(Main.scala:53)
        at sbt.xMain.run(Main.scala:28)
        at xsbt.boot.Launch$$anonfun$run$1.apply(Launch.scala:109)
        at xsbt.boot.Launch$.withContextLoader(Launch.scala:129)
        at xsbt.boot.Launch$.run(Launch.scala:109)
        at xsbt.boot.Launch$$anonfun$apply$1.apply(Launch.scala:36)
        at xsbt.boot.Launch$.launch(Launch.scala:117)
        at xsbt.boot.Launch$.apply(Launch.scala:19)
        at xsbt.boot.Boot$.runImpl(Boot.scala:44)
        at xsbt.boot.Boot$.main(Boot.scala:20)
        at xsbt.boot.Boot.main(Boot.scala)
[error] java.lang.IllegalStateException: No staging repository is found. Run publishSigned first
[error] Use 'last' for the full log.

Solution

I just found the groupId was default in pom on Sonatype repository. So the solution is just declare organization setting in buidl.sbt

organization := "com.joecwu"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment