掘金 后端 ( ) • 2021-12-04 10:41

什么是 Central Repository?

The Central Repository is the largest collection of Java and other open-source components. It provides the easiest way to access and distribute your software components to millions of developers. It is the default repository for Apache Maven, SBT, and other build systems and can be easily used from Apache Ant/Ivy, Gradle, and many other tools.

Central Repositroy是最大的一个Java包仓库,还是Apache Maven, SBT的默认仓库。

什么是 Sonatype?

Sonatype is a Fulton, Maryland-based cybersecurity company helping enterprises get precise intelligence about open source components and software security

Sonatype是一家技术服务公司

Sonatype和Central Repository之间是什么关系?

Sonatype is the steward, maintainer, and financial sponsor of the Central Repository and is determined to continue to provide it to the community of consumers and providers of components.

简单来说Sonatype是Central Repository的管理者和投资者

发布前要准备什么?

  1. 注册一个 Sonatype JIRA 账号

  2. 提交新的工程申请

    ticket form

    这里最重要的字段是Group Id,通常它是一个域名,你必须要证明你拥有这个域名。如果你没有注册域名的话,也可以使用GitHub提供的域名io.github.(account),下面是我收到的关于域名的要求

    sonatype reply

如何发布?

添加插件

project/plugins.sbt中添加下面的插件

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.9") // 管理Sonatype
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.1") // 管理gpg
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0") // 管理发布流程

重写 release 任务

build.sbt中添加下面的代码

    releaseProcess := Seq[ReleaseStep](
      checkSnapshotDependencies,
      inquireVersions,
      runClean,
      runTest,
      setReleaseVersion,
      commitReleaseVersion,
      tagRelease,
      releaseStepCommandAndRemaining("+publishSigned"),
      releaseStepCommand("sonatypeBundleRelease"),
      setNextVersion,
      commitNextVersion,
      pushChanges
    )

创建 GPG 密钥

  1. 运行如下命令生成密钥
    gpg --gen-key
    
    generate key process
  2. 将公钥发送到 Sonatype支持的密钥服务器
    gpg --keyserver keyserver.ubuntu.com --send-keys 
    

添加鉴权信息和密钥信息

  1. 新建文件 ~/.sbt//sonatype.sbt
  2. 在新建文件中添加如下代码
    credentials += Credentials("Sonatype Nexus Repository Manager",
        "s01.oss.sonatype.org",
        "",
        "")
    credentials += Credentials(
      "",
      "gpg",
      "", // key identifier
      "ignored" // this field is ignored; passwords are supplied by pinentry
    )
    

执行release进行发布

在sbt中执行 release

sbt> release

chose version screen shot

发布成功后我们就可以在 Central Repository看到我们的包啦

lib in central repository