发布Scala构件至Maven中央库

背景

本文接上文发布构件至Maven中央库, 详细讲解SBT如何发构件至本地, 如何发布构件至Maven中央库.

样例项目

该项目的项目目录结构, build.sbt相关配置符合发布要求, 且已发布成功, 并会进行不定期更新, 首次发布的同学可参考对比.

注册Sonatype账号

略.

详见: 发布构件至Maven中央库

发布至本地

配置

修改build.sbt文件, 增加三行配置即可

1
2
3
4
5
6
7
8
// 本地maven仓库位置
publishTo := Some(
Resolver.file("file", new File(Path.userHome.absolutePath + "/repo")))

publishMavenStyle := true

// 移除生成构件的 scala version
crossPaths := false

完整配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
name := "rest-api-protocol"

version := "0.0.2"

organization := "net.arccode"

scalaVersion := "2.12.2"

// 本地maven仓库位置
publishTo := Some(
Resolver.file("file", new File(Path.userHome.absolutePath + "/repo")))

publishMavenStyle := true

// 移除生成构件的 scala version
crossPaths := false

发布指令

1
sbt publish

发布至Maven中央库

配置插件和仓库等

配置plugins.sbt,

1
2
3
4
5
logLevel := Level.Info

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.1")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.4")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")

配置build.sbt,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
name := "rest-api-protocol"

version := "0.0.2"

organization := "net.arccode"

scalaVersion := "2.12.2"

// 本地maven仓库位置
//publishTo := Some(
// Resolver.file("file", new File(Path.userHome.absolutePath + "/repo")))
//

publishMavenStyle := true

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

// 移除生成构件的 scala version
crossPaths := false

publishArtifact in Test := false

pomIncludeRepository := { _ ⇒
false
}

pomExtra in Global := {
<url>https://github.com/arccode/rest-api-protocol</url>
<licenses>
<license>
<name>MIT</name>
<url>https://opensource.org/licenses/MIT</url>
</license>
</licenses>
<scm>
<url>git@github.com:arccode/rest-api-protocol.git</url>
<connection>scm:git:git@github.com:arccode/rest-api-protocol.git</connection>
</scm>
<developers>
<developer>
<id>AC</id>
<name>arccode</name>
<url>http://www.arccode.net/</url>
</developer>
</developers>
}

生成pgp

进入项目根目录, 输入sbt指令进入交互模式,

1
2
3
> compile // 编译项目
> pgp-cmd gen-key // 生成key, 记住后续需要使用
> pgp-cmd send-key keyname hkp://pool.sks-keyservers.net //将公钥发到服务器,其中keyname是生成key时使用的用户名。

生成上传凭证

新建文件: ~/.sbt/0.13/sonatype.sbt, 内容为:

1
2
3
4
5
6
credentials += Credentials(
"Sonatype Nexus Repository Manager",
"oss.sonatype.org",
"<your username>",
"<your password>"
)

笔者重新定义过相关参数(在repositories中),

1
2
3
# Path to global settings/plugins directory (default: ~/.sbt)
#
-sbt-dir /Users/path-to/

因此需要在该目录下新建上述授权文件.

发布至中央库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> publishSigned
[info] Wrote /Users/ac/workspace/github/rest-api-protocol/target/rest-api-protocol-0.0.2.pom
[info] :: delivering :: net.arccode#rest-api-protocol;0.0.2 :: 0.0.2 :: release :: Wed Apr 18 20:40:48 CST 2018
[info] delivering ivy file to /Users/ac/workspace/github/rest-api-protocol/target/ivy-0.0.2.xml
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2-sources.jar.asc
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2-javadoc.jar.asc
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2.jar

[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2.pom.asc
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2-javadoc.jar
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2.pom
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2.jar.asc
[info] published rest-api-protocol to https://oss.sonatype.org/service/local/staging/deploy/maven2/net/arccode/rest-api-protocol/0.0.2/rest-api-protocol-0.0.2-sources.jar
[success] Total time: 39 s, completed 2018-4-18 20:41:27
>

在OSS中发布构件

详见: 发布构件至Maven中央库

本序列文章

参考

转载

本文出自<<arccode>>, 欢迎转载, 转载请注明出处.