中央仓库上传指南原创
# 简介
Maven 中央仓库是 Java 生态系统中最重要的依赖库之一,将你的项目发布到 Maven 中央仓库可以让全球的开发者更容易地使用你的库。本文将详细介绍如何将你的 Java 项目发布到 Maven 中央仓库的完整流程。
# 前置条件
- JDK 8 或更高版本
- Maven 3.0+
- GPG(用于签名)
- Sonatype OSSRH 账号
# 步骤概览
- 注册 Sonatype 账号
- 配置 GPG 签名
- 配置 Maven 的
settings.xml
- 配置项目的
pom.xml
- 发布到中央仓库
# 详细步骤
# 1. 注册 Sonatype 账号
从 2024 年 3 月 12 日起,所有注册都将通过 Sonatype 中央门户网站 (opens new window) 进行。
- 访问 Sonatype 中央门户网站 (opens new window)。
- 点击右上角的“注册”按钮。
- 填写用户名、密码、邮箱等信息。
- 完成注册并登录。
# 2. 配置 GPG 签名
为了保证上传到 Maven 中央仓库的文件的安全性和完整性,你需要使用 GPG 对文件进行签名。
生成 GPG 密钥:
gpg --gen-key
1在生成过程中,选择默认的“RSA and RSA”选项,并设置密钥大小(建议使用 4096 位)。然后输入你的姓名、邮箱等信息,并设置一个安全的密码。
查看 GPG 密钥:
gpg --list-secret-keys --keyid-format LONG
1记录下你的 GPG 密钥 ID。
# 3. 配置 Maven 的 settings.xml
配置 GPG 插件:在项目的
pom.xml
文件中,添加以下配置,以便 Maven 能够使用 GPG 对文件进行签名:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18配置 Maven 中央仓库的服务器信息:在你的用户目录下的
.m2/settings.xml
文件中,添加以下配置:<servers> <server> <id>central</id> <username>你的Sonatype用户名</username> <password>你的Sonatype密码</password> </server> </servers>
1
2
3
4
5
6
7
# 4. 配置项目的 pom.xml
确保 pom.xml
文件中包含以下信息:
项目基本信息:
groupId
、artifactId
、version
、name
、url
、description
。开源许可证:
<licenses> <license> <name>Apache License | Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments>A business-friendly OSS license</comments> </license> </licenses>
1
2
3
4
5
6
7
8开发者信息:
<developers> <developer> <name>你的名字</name> <email>你的邮箱</email> <url>你的个人主页或GitHub主页</url> </developer> </developers>
1
2
3
4
5
6
7SCM 信息:
<scm> <url>你的项目主页地址</url> <connection>你的项目Git地址</connection> </scm>
1
2
3
4完整pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.github.liyao52033</groupId> <artifactId>liyao5-spring-boot-starter-common</artifactId> <version>1.2.1</version> <name>common-spring-boot-starter</name> <description发布描述</description> <url>https://github.com/liyao52033/youlai-boot.git</url> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.7.18</spring-boot.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <developers> <developer> <name>你的名字</name> <email>你的邮箱</email> <url>你的个人主页或GitHub主页</url> </developer> </developers> <scm> <connection>你的项目Git地址</connection> <developerConnection>scm:git:ssh:// 你的项目Git地址(ssh地址) </developerConnection> <url>你的项目Git地址</url> </scm> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version> </dependency> // 其他项目依赖... </dependencies> <distributionManagement> <snapshotRepository> <id>ossrh</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>ossrh</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>3.2.7</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.sonatype.central</groupId> <artifactId>central-publishing-maven-plugin</artifactId> <version>0.5.0</version> <extensions>true</extensions> <configuration> <publishingServerId>发布id</publishingServerId> <checksums>required</checksums> <deploymentName>发布人</deploymentName> </configuration> </plugin> </plugins> </build> <licenses> <license> <name>MIT License</name> <url>http://www.opensource.org/licenses/mit-license.php</url> </license> </licenses> </project>
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
# 5. 发布到中央仓库
在项目的根目录下,运行以下命令:
mvn deploy --settings ~/.m2/settings.xml
1这个命令会清理项目,然后构建项目,并将生成的文件上传到 Maven 中央仓库。
如果上传成功,你可以在 Maven 中央仓库 (opens new window) 中搜索你的项目,查看是否已经成功上传。
最后,在 Sonatype 中央门户网站 (opens new window) 点击“Publish”正式发布。
# 常见问题
# GPG 密钥问题
- 问题:GPG 密钥生成失败。
解决方法:检查你的系统是否安装了 GPG 工具(也可以通过
git bash
执行生成命令)。如果没有安装,可以通过以下命令安装:- 在 Ubuntu 系统中:
sudo apt-get install gnupg
1 - 在 macOS 系统中:
brew install gpg
1
- 在 Ubuntu 系统中:
# Maven 配置问题
- 问题:Maven 无法找到
settings.xml
文件。- 解决方法:确保你的
settings.xml
文件位于你的用户目录下的.m2
文件夹中。如果文件不存在,你可以手动创建一个。
- 解决方法:确保你的
# 项目上传问题
- 问题:项目上传失败,提示缺少某些文件。
- 解决方法:确保你的项目满足 Maven 中央仓库的要求,例如必须有 Javadoc 文件、源码文件等。如果缺少,需要在
pom.xml
中添加相应的插件配置。
- 解决方法:确保你的项目满足 Maven 中央仓库的要求,例如必须有 Javadoc 文件、源码文件等。如果缺少,需要在
# 总结
将项目上传到 Maven 中央仓库是一个相对简单的过程,但需要一些准备工作和配置。通过本指南,你应该已经了解了如何创建 Sonatype 账号、生成 GPG 密钥、配置 Maven 以及提交项目申请和上传项目。希望你的项目能够成功上传到 Maven 中央仓库,为全球的开发者提供便利。
如果你在上传过程中遇到任何问题,可以参考 Sonatype 的官方文档或在相关社区寻求帮助。
- 01
- 模板生成工具 原创02-18
- 02
- RTC实时时钟 原创02-12
- 03
- keil调试 原创01-21
- 04
- GPIO概述与配置 原创01-20
- 05
- element-plus多文件手动上传 原创11-03