发布Jar到Maven中央仓库--Gradle版(最新方式)

摘要

一、将项目推送到远程仓库,如 Github或者Gitee

二、注册 Sonatype 账户

三、登录 Sonatype 创建Namespace

前三个步骤与 发布Jar到Maven中央仓库--Maven版(最新方式) 相同,不在赘述。

四、发布

  • 签名
    我是mac电脑,于是签名工具使用的是https://gpgtools.org,gradle签名时需要使用到.gpg证书文件,这个工具不支持直接导出.gpg,其导出的证书文件是.asc格式的,asc其实就是字符串,可以用记事本打开查看。

    使用如下命令导出.gpg格式的证书:

    1
    2
    3
    4
    5
    6
    7
    8
    # 列出当前全部证书
    gpg -k

    # 导出私钥,qunfeng_han@126.com是创建证书时使用的邮箱,会要求你输入创建证书时的密码
    gpg --output private.pgp --armor --export-secret-key qunfeng_han@126.com

    # 导出公钥
    gpg --output public.pgp --armor --export qunfeng_han@126.com
  • build.gradle
    官方没有出gradle发布插件,但是官网推荐使用第三方的jreleaser插件。

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
plugins {
// id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'io.spring.dependency-management' version '1.1.6'
//此处必须是java-library,如果是java则api方法不可用,api可以理解为就是compile,支持传递依赖
id 'java-library'
//发布插件
id 'maven-publish'
//自动发布到maven中央仓库插件
//https://jreleaser.org/guide/latest/examples/maven/maven-central.html#_gradle
id 'org.jreleaser' version '1.13.1'
}

group = 'io.github.hanqunfeng'
//version = '1.0.0-SNAPSHOT'
version = '1.0.1'
sourceCompatibility = '1.8'


java {
withJavadocJar()
withSourcesJar()
}

repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenLocal()
mavenCentral()
}

dependencyManagement {
imports { mavenBom("software.amazon.awssdk:bom:2.23.10") }
}

dependencies {
//lombok
compileOnly 'org.projectlombok:lombok:1.18.32'
annotationProcessor 'org.projectlombok:lombok:1.18.32'
api 'software.amazon.awssdk:s3'
api 'software.amazon.awssdk:apache-client'
api 'software.amazon.awssdk:s3-transfer-manager'
api 'software.amazon.awssdk:aws-crt-client'
api 'org.springframework:spring-core:5.3.29'


}

// java编译的时候缺省状态下会因为中文字符而失败
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'


/**
* 发布插件
* 参考:https://docs.gradle.org/6.6.1/userguide/publishing_maven.html#publishing_maven:resolved_dependencies
*
* 发布为bom,参考:https://zhuanlan.zhihu.com/p/195678201
* 注意:java-platform不能与java和java-library同时存在
*/
publishing {
publications {
maven(MavenPublication) {
groupId = project.group
artifactId = project.name
version = project.version
//如果不定义,则会按照以上默认值执行

from components.java

pom {
name = 'aws-s3-v2-tools-gradle'
description = 'AWS S3 Tools.'
url = 'https://blog.hanqunfeng.com'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'hanqf'
name = 'han qunfeng'
email = 'qunfeng_han@126.com'
}
}
scm {
connection = 'scm:git:https://github.com/hanqunfeng/aws-s3-v2-tools-gradle.git'
developerConnection = 'scm:git:https://github.com:hanqunfeng/aws-s3-v2-tools-gradle.git'
url = 'https://github.com/hanqunfeng/aws-s3-v2-tools-gradle'
}
}

versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
repositories {
maven {
url = layout.buildDirectory.dir('staging-deploy')
}
}
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

//javadoc,如果用jdk11,默认就支持中文
//查看可以配置的属性:https://docs.gradle.org/current/javadoc/org/gradle/external/javadoc/StandardJavadocDocletOptions.html
tasks.withType(Javadoc) {
options.version = true
options.author = true
options.encoding = "UTF-8"
options.charSet = "UTF-8" //解决中文乱码
}


javadoc {
if (JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}

if (JavaVersion.current().isJava8Compatible()) {
tasks.withType(Javadoc) {
// disable the crazy super-strict doclint tool in Java 8
// noinspection SpellCheckingInspection
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}

// jreleaser配置文件:~/.jreleaser/config.toml
/*
JRELEASER_MAVENCENTRAL_USERNAME = "<your-publisher-portal-username>"
JRELEASER_MAVENCENTRAL_PASSWORD = "<your-publisher-portal-password>"
JRELEASER_NEXUS2_USERNAME = "<your-sonatype-account-username>"
JRELEASER_NEXUS2_PASSWORD = "<your-sonatype-account-password>"
JRELEASER_GPG_PASSPHRASE = "<your-pgp-passphrase>"
JRELEASER_GITHUB_TOKEN = "<your-github-token"
*/
jreleaser {
signing {
active = 'ALWAYS'
armored = true
mode = 'FILE'
publicKey = '/Users/hanqf/develop_soft/gpg_key/hanqf/public.pgp'
secretKey = '/Users/hanqf/develop_soft/gpg_key/hanqf/private.pgp'
}
deploy {
maven {

/* Portal Publisher API */
mavenCentral {
sonatype {
active = 'ALWAYS'
url = 'https://central.sonatype.com/api/v1/publisher'
stagingRepository('build/staging-deploy')
}
}

}
}
}


  • ~/.jreleaser/config.toml :配置相关认证信息

1
2
3
4
5
6
7
8
9
10
# https://central.sonatype.com 的认证信息
JRELEASER_MAVENCENTRAL_USERNAME = "<your-publisher-portal-username>"
JRELEASER_MAVENCENTRAL_PASSWORD = "<your-publisher-portal-password>"
# https://oss.sonatype.org 的认证信息,这里没有用到,可以不进行配置
JRELEASER_NEXUS2_USERNAME = "<your-sonatype-account-username>"
JRELEASER_NEXUS2_PASSWORD = "<your-sonatype-account-password>"
# 创建 pgp 密钥时的密码
JRELEASER_GPG_PASSPHRASE = "<your-pgp-passphrase>"
# github token
JRELEASER_GITHUB_TOKEN = "<your-github-token"
  • 执行命令

1
2
3
./gradlew jreleaserConfig # 验证配置文件是否正确
./gradlew clean publish # 发布到本地 build/staging-deploy
./gradlew jreleaserFullRelease # 发布到远程Maven中央仓库