使用Github发布项目包到Maven中央仓库简要版本

Maven中央仓库并不支持直接发布jar包,需要将jar包发布到一些指定的第三方Maven仓库,然后该仓库再将jar包同步到Maven中央仓库

注册申请域

https://issues.sonatype.org/secure/CreateIssue!default.jspa

即将过期

环境搭建

# 安装 Gpg4win - a secure solution for file and email encryption.
winget install --id=GnuPG.Gpg4win  -e
# 生成key
gpg --generate-key
# 推送到公钥服务器
gpg --keyserver keyserver.ubuntu.com --send-keys $KEY_ID
# 查看本地密钥列表
gpg --list-secret-keys
# 显示key 可复制到 Github Actions secrets and variables
gpg -a --export-secret-keys $KEY_ID

项目配置

pom.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.adbyte.parent</groupId>
<artifactId>commons-dependencies</artifactId>
<version>0.0.1</version>
<name>Commons Dependencies</name>
<description>通用组件父依赖</description>
<packaging>pom</packaging>

<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.2.2</spring-boot.version>
</properties>

<url>https://github.com/yaodwwy/commons</url>

<!-- 许可证信息,Apache 2.0的许可证 -->
<licenses>
<license>
<name>The Apache Software License, Version2.0</name>
<url>https://www.apache.org/licenses/</url>
<distribution>repo</distribution>
</license>
</licenses>

<!-- 开发人员信息 -->
<developers>
<developer>
<name>Adam</name>
<email>[email protected]</email>
</developer>
</developers>

<!-- 项目仓库信息 -->
<scm>
<connection>scm:git:https://github.com/yaodwwy/commons.git</connection>
<developerConnection>mailto:[email protected]</developerConnection>
<url>https://github.com/yaodwwy/commons.git</url>
<tag>v${project.version}</tag>
</scm>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<!-- 生成java source.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<!-- TODO 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>update-child-modules</id>
<phase>validate</phase>
<goals>
<goal>update-child-modules</goal>
</goals>
<configuration>
<allowSnapshots>true</allowSnapshots>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

Github配置

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

name: Maven Central Repo Deployment
on:
push:
branches:
- main
# 定义一个发行任务
jobs:
release:
# 运行处理流程的 OS 环境
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Install Java and Maven
uses: actions/setup-java@v1
with:
java-version: 17

- name: Release Maven package
uses: samuelmeuli/[email protected]
with:
server_id: 'ossrh' # 与 maven pom 文件对应
maven_args: '-DskipTests'
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} # GPG 密钥
gpg_passphrase: ${{ secrets.GPG_PASSPHRASE }} # 必须设置的 GPG 密码
nexus_username: ${{ secrets.NEXUS_USERNAME }} # sonatype 用户名(不是邮箱)
nexus_password: ${{ secrets.NEXUS_PASSWORD }} # sonatype 登录密码

参考:

samuelmeuli/action-maven-publish 可更新版本 v1.4.0

https://github.com/samuelmeuli/action-maven-publish