发布jar包到maven中央仓库
# 发布jar包到maven中央仓库
# sonatype准备工作
中央仓库是由sonatype管理的,你需要先进行一系列操作才能发布。
- 在Sonatype注册账户
- 创建issues,类型为新项目
- 按照工作人员的要求证明group_id域名的所有权,如果你没域名的话,也可以使用xxx.github.io 这样的二级域名
- 证明完成之后就可以开始下一步了
# 创建项目
# 沟通过程
由于我的注册邮箱是我域名下的邮箱,可能是这个原因,所以证明域名所有权的过程省掉了
# gpg签名
接下来你还需要创建gpg签名文件来证明发布者的身份
GPG入门教程阮一峰
# 大致流程
# 生成一个密钥
gpg --gen-key
# 列出密钥
gpg --list-keys
# 发布公钥
gpg --send-keys [用户ID] --keyserver hkp://subkeys.pgp.net
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# gradle配置
vim ~/.gradle/gradle.properties
NEXUS_USERNAME= xxxx
NEXUS_PASSWORD= xxxx
signing.keyId= gpg的秘钥指纹后八位
signing.password= gpg密钥对的设置的密码
signing.secretKeyRingFile= /Users/linkji/.gnupg/secring.gpg
1
2
3
4
5
6
2
3
4
5
6
注意: signing.secretKeyRingFile 这个配置对应的.gpg文件,你要在 ~/.gnupg/ 目录下寻找,如果没有,则需参考下面的链接
Signing plugin doesn't work with GPG 2.1bryant1410
# 项目配置
# build.gradle
这里列出主要配置,完整配置请参考blzo-ex
apply plugin: 'maven-publish' // maven 发布插件
apply plugin: 'signing' // 签名插件
version '0.3.1.RELEASE'
group 'com.jdkhome.blzo'
sourceCompatibility = 1.8
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
[compileJava]*.options*.encoding = 'UTF-8'
}
compileTestJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
[compileTestJava]*.options*.encoding = 'UTF-8'
}
//这里一定得要。在多模块下,不然编译失败,因为不会把信赖模块给打包。
jar {
enabled = true
}
// 后面的都是打包的配置
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allJava
}
// 生成 javadoc jar
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc.destinationDir
}
// javadoc 配置,这里是自定义了 java doc 的一些配置
javadoc {
description = project.name
options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
options.author = true
options.version = true
options.header = project.name
options.addStringOption('Xdoclint:none', '-quiet')
// suppress warnings due to cross-module @see and @link references;
// note that global 'api' task does display all warnings.
logging.captureStandardError LogLevel.INFO
logging.captureStandardOutput LogLevel.INFO // suppress "## warnings" message
options.encoding = "UTF-8" //编码一定要配置否则直接出错
options.charSet = 'UTF-8'
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId project.group
artifactId project.name
version "${version}"
from components.java
artifact sourcesJar
artifact javadocJar
// https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html
pom {
name = project.name
description = project.name
url = "https://github.com/jdkhome/blzo-ex/tree/master/" + project.name
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "jdkhome"
name = "linkji"
email = "main@jdkhome.com"
}
}
scm {
connection = "scm:git:https://github.com/jdkhome/blzo-ex.git"
developerConnection = "scm:git:https://github.com/jdkhome/blzo-ex.git"
url = "https://github.com/jdkhome/blzo-ex"
}
}
}
}
repositories {
maven {
name 'sonatypeRepository'
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
maven {
name = 'sonatypeSnapshotRepository'
url = 'https://oss.sonatype.org/content/repositories/snapshots/'
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
}
}
// 必须在 publishing 配置之后
signing {
sign publishing.publications.mavenJava
}
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Push
对于发布来说,有2个地址,对应前面build.gradle中,我们配置的2个可以发布的仓库
repositories {
maven {
name 'sonatypeRepository'
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
maven {
name = 'sonatypeSnapshotRepository'
url = 'https://oss.sonatype.org/content/repositories/snapshots/'
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- release:
- 表示正式版,稳定版本
- 需要 gpg 签名才可以发布成功
- snapshot:
- 快照版, 不稳定的,开发的时候常用
- 必须携带 -SNAPSHOT 后缀
发布命令为
# 发快照
gradle publishMavenJavaPublicationToSonatypeSnapshotRepository
# 发release
gradle publishMavenJavaPublicationToSonatypeRepository
1
2
3
4
5
2
3
4
5
# 发布
登录到
https://oss.sonatype.org
选择"Staging Repositories",找到刚才推得jar包(一般在列表最下面)。确认没问题的话点击close,然后点击release即可
# 参考链接
将Gradle项目发布到Maven Central库中HamilFei
发布包到 maven 仓库zq99299
上次更新: 2020/06/10, 23:06:00