1.背景
本教程为2024年9月最新版
我有一个Java项目,想发布到Maven中央仓库,任何人都可以在pom文件中引用我的代码
- 引用格式如下(以rocketmq为例):
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>5.3.0</version>
</dependency>
2.注册Sonatype账号
Maven中央仓库由Sonatype维护,所以需要注册一个Sonatype账号
- 地址: Sonatype Maven中央仓库
- 点击【Sign in】来到登录页
- 如果你已经有账号了,填写用户名或邮箱、密码后点击Continue直接登陆即可
- 如果你没有账号可以选择注册,点击Sign up即可进入注册页面,具体操作略
- 不想注册也可通过第三方(Google或GitHub)账号登录,前提是你有这些账号
3.创建命名空间(Namespace)
成功登录Sonatype后,需要创建至少一个命名空间
这一步这是必须的,命名空间可作为日后上传项目的groupId
注意:上传项目的groupId必须是已创建并审核通过的命名空间,否则将无法上传
下面看看如何创建一个命名空间:
-
点击用户名,再点击 View Namespaces 查看已有命名空间列表
-
然后点击 Add Namespace添加一个命名空间
-
填写命名空间名称,并提交
命名空间名称是以“.”分隔的,这个名字不是随便取的,参考以下规则:
(1)如果你有一个GitHub账号,则你可以创建一个io.github.xxx的命名空间,
其中“xxx”是你GitHub的用户名,可以登录到GitHub后查看个人主页的地址
比如“https://github.com/abc”的用户名就是“abc”,可以添加的命名空间为“io.github.abc”
Tips:
如果你是通过GitHub登录Sonatype的,
则会自动通过你的GitHub用户名创建一个命名空间,无需手动创建~
支持的代码库如下:
代码库 | 命名空间示例 |
---|---|
GitHub | io.github.myusername |
GitLab | io.gitlab.myusername |
Gitee | io.gitee.myusername |
Bitbucket | io.bitbucket.myusername |
(2)如果你有一个自己的域名,则可以通过域名添加一个命名空间
比如你的域名是“www.abc.com”,则可以添加一个“com.abc”的命名空间
(3)其他添加方式请参考官方文档:Namespace创建官方文档
- 提交后会看到一个待验证的命名空间,下方展示了验证码
验证方法,根据命名空间类型的不同验证方式也不同
3.1 GitHub的命名空间验证
如果是GitHub的命名空间,验证方式就是登录对应用户名的GitHub账号
创建一个新的仓库,名称为提交命名空间时生成的验证码(y3shwin48v)
- 仓库创建完毕后,回到Sonatype的命名空间页面点击验证
- 这时命名空间的状态由Unverified变为了Verification Pending,表示正在验证中
- 等待一段时间后,状态变为Verified时则表示命名空间验证通过
命名空间验证通过后:
1.之前在GitHub创建的仓库可以删除
2.已验证通过(状态为Verified)的命名空间不可删除,也不可以修改
3.2 域名的命名空间验证
如果你是通过域名创建的命名空间,则需要到对应的域名注册服务商配置一个域名解析用于验证
假设你创建的命名空间为“com.abc”,则域名是www.abc.com
登录域名服务商,为abc.com这个域名添加一个域名解析, 这里以阿里云为例:
- 登录阿里云app
- 进入域名解析
记录类型选“TXT”,主机记录填写“@”,记录值填写提交命名空间时生成的验证码(y3shwin48v)
填写完毕后保存即可,后续和GitHub创建好仓库之后的操作一致
验证通过后可以删除这个域名解析
4.安装GPG
GPG(GnuPG)是一款加密软件,可以对文件和电子邮件进行签名和加密
安装GPG,可对我们上传的代码文件进行签名
- GPG Windows版下载地址
- 双击“gpg4win-4.3.1.exe”安装即可,具体过程省略
5.GPG生成密钥
- 安装完成后,cmd打开命令行
输入"gpg --version"验证是否安装成功,如下代表安装成功
C:\Users\xxx>gpg --version
gpg (GnuPG) 2.4.5
libgcrypt 1.10.3
Copyright (C) 2024 g10 Code GmbH
License GNU GPL-3.0-or-later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Home: C:\Users\xxx\AppData\Roaming\gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
- 生成key命令:gpg --gen-key
填写Real name(姓名)和Email Address(邮箱,最好用真实的,可用于密钥找回)
输入"O"回车,弹出弹窗,填写密码口令(自定义,后面会用到)两次,然后点击OK,密匙就创建好了
pub ed25519 2024-09-014 [SC] [expires: 2027-09-14]
1189GDSG89043SGDB8943ABCGDFGAADK78DHS
uid realName <email address>
sub cv25519 2024-09-14 [E] [expires: 2027-09-14]
此处生成的公钥为:1189GDSG89043SGDB8943ABCGDFGAADK78DHS
- 发布公钥,命令:gpg --keyserver keyserver.ubuntu.com --send-keys <公钥>
如果出现发送失败的情况,可以选择其他服务器多次重试,总会成功的
中央服务器支持的GPG密钥服务器有:
keyserver.ubuntu.com
keys.openpgp.org
pgp.mit.edu
- 验证公钥是否上传成功:gpg --keyserver keyserver.ubuntu.com --recv-keys <公钥>
- 查看已有的公钥列表:gpg --list-keys
6.本地项目配置
6.1 pom文件完整配置
假设我已有审核通过的命名空间:io.github.xxx,现上传的项目名称为:abc
注意:同一个项目的同一个版本只能在Maven中央仓库发布一次,要多次发布须升级版本号
<?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.xxx</groupId>
<artifactId>abc</artifactId>
<version>1.0.0</version>
<!-- 项目信息 -->
<name>abc</name>
<description>项目描述</description>
<url>https://github.com/xxx/abc</url>
<!-- 打包方式 -->
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- maven编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<!-- maven Source插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- maven Javadoc插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.10.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- gpg插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- 改成你自己的路径 -->
<executable>D:\ProgramFiles\GnuPG\bin\gpg.exe</executable>
</configuration>
<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>central</publishingServerId>
<!--<tokenAuth>true</tokenAuth>-->
<!-- 若要自动发布,添加以下配置 -->
<!--<autoPublish>true</autoPublish>-->
<!-- 一直等到发布完成 -->
<!--<waitUntil>published</waitUntil>-->
</configuration>
</plugin>
</plugins>
</build>
<!-- 版本控制 -->
<scm>
<url>https://github.com/xxx/abc/tree/master</url>
<connection>scm:git:https://github.com/xxx/abc.git</connection>
<developerConnection>scm:git:https://github.com/xxx/abc.git</developerConnection>
</scm>
<!-- 开发者信息 -->
<developers>
<developer>
<name>xxx</name>
<email>xxx@gmail.com</email>
</developer>
</developers>
<!-- 开源协议 -->
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<!-- 官方maven配置文档 -->
<!-- https://central.sonatype.org/publish/publish-portal-maven/ -->
</project>
- 其中需要修改的信息如下:
<groupId>io.github.xxx</groupId>
<artifactId>abc</artifactId>
<version>1.0.0</version>
<name>abc</name>
<description>项目描述</description>
<url>https://github.com/xxx/abc</url>
groupId:必须是已审核通过的命名空间,不能随意填写,否则上传失败
artifactId和name:都是项目名称,可自己取名
version:版本号,一个版本号只能在Maven中央仓库发布一次,版本号不能带“SNAPSHOT”,快照版不支持上传
description:项目描述,不要乱写,会展示到Maven中央仓库官网
url:项目地址,比如GitHub地址,没有的话就空着
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- 改成你自己的路径 -->
<executable>D:\ProgramFiles\GnuPG\bin\gpg.exe</executable>
</configuration>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
executable: gpg的执行路径,改成自己的实际安装路径
<scm>
<url>https://github.com/xxx/abc/tree/master</url>
<connection>scm:git:https://github.com/xxx/abc.git</connection>
<developerConnection>scm:git:https://github.com/xxx/abc.git</developerConnection>
</scm>
<developers>
<developer>
<name>xxx</name>
<email>xxx@gmail.com</email>
</developer>
</developers>
scm:版本控制https://后面的部分按自己项目的实际情况填写
developers:开发者信息按自己的实际情况填写(姓名和邮箱地址)
6.2 maven settings文件完整配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<servers>
<!-- maven中央仓库 -->
<server>
<id>central</id>
<!-- 用户名和密码来自sonatype生成的User Token -->
<username>xxx</username>
<password>xxxxxxxxxxxxxxxxxxxxx</password>
</server>
</servers>
<mirrors>
<!-- 阿里云镜像仓库 -->
<mirror>
<id>alimanven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>gpg</id>
<properties>
<!-- 本地gpg执行目录(注意和安装目录不是同一个,与安装目录最外层目录同级) -->
<gpg.executable>D:\ProgramFiles\GnuPG\bin</gpg.executable>
<!-- 创建密钥时的密码口令passphrase -->
<gpg.passphrase>xxxxxx</gpg.passphrase>
</properties>
</profile>
</profiles>
<activeProfiles>
<!-- 应用gpg profile -->
<activeProfile>gpg</activeProfile>
</activeProfiles>
</settings>
- 其中需要修改的信息如下:
<servers>
<server>
<id>central</id>
<username>xxx</username>
<password>xxxxxxxxxxxxxxxxxxxxx</password>
</server>
</servers>
username和password来自Sonatype生成的User Token
点击用户名,点击"View Account"
注意:这个弹窗一旦关闭后,就没法再次查看了,所以最好自己复制下来,保存在本地
<profile>
<id>gpg</id>
<properties>
<!-- 本地gpg执行目录(注意和安装目录不是同一个,与安装目录最外层目录同级) -->
<gpg.executable>D:\ProgramFiles\GnuPG\bin</gpg.executable>
<!-- 创建密钥时的密码口令passphrase -->
<gpg.passphrase>xxxxxx</gpg.passphrase>
</properties>
</profile>
gpg.executable: gpg执行目录,按实际情况修改
gpg.passphrase:这个口令就是之前用gpg生成key时输入的口令
7.上传项目到命名空间
- 对项目执行Maven命令: clean、deploy
可以用命令行也可以在IDEA中点击执行
BUILD SUCCESS 后则上传成功,不成功自行查看失败原因并处理
8.发布项目
上传成功后登录Sonatype查看
点击用户名,然后点击View Deployments
IDEA deploy成功后,此处的Deployments列表就能看到,状态为VALIDATED
不想发布了可以点击Drop直接删除
发布则点击Publish, 状态会变成PUBLISHING, 中间需要等待一个小时、几个小时,甚至一天,具体时间不定(发布中可以取消发布)
发布成功后,状态会变为PUBLISHED, 这时项目就已经发布到Maven中央仓库了
注意:已发布成功的项目不能删除,也不能再次发布了
9.Maven引用
发布成功后,全世界任何人都可以通过以下gav坐标引用你的项目了
<dependency>
<groupId>io.github.xxx</groupId>
<artifactId>abc</artifactId>
<version>1.0.0</version>
</dependency>
Tips:
https://search.maven.org在发布成功后就可以搜索到了
https://mvnrepository.com的同步比较慢,一般要3-5天才能搜索到,仅仅只是搜索不到而已,不影响使用