欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 如何将自己的项目发布到Maven中央仓库

如何将自己的项目发布到Maven中央仓库

2025/9/17 10:17:55 来源:https://blog.csdn.net/xhom_w/article/details/142258441  浏览:    关键词:如何将自己的项目发布到Maven中央仓库
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账号

  • 地址: 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用户名创建一个命名空间,无需手动创建~
支持的代码库如下:

代码库命名空间示例
GitHubio.github.myusername
GitLabio.gitlab.myusername
Giteeio.gitee.myusername
Bitbucketio.bitbucket.myusername

2.如果你有一个自己的域名,则可以通过域名添加一个命名空间
比如你的域名是“www.abc.com”,则可以添加一个“com.abc”的命名空间

3.其他添加方式请参考官方文档:Namespace创建官网文档

  • 提交后会看到一个待验证的命名空间,下方展示了验证码
    在这里插入图片描述

验证方法,根据命名空间类型的不同验证方式也不同

3.1 Github的命名空间验证

如果是github的命名空间,验证方式就是登录对应用户名的GitHub账号,创建一个新的仓库
库的名称为提交命名空间时生成的验证码(y3shwin48v)

在这里插入图片描述
在这里插入图片描述

  • 仓库创建完毕后,回到Sonatype的命名空间页面点击验证
  • 在这里插入图片描述
    在这里插入图片描述
  • 这时命名空间的状态由“Unverified”变为了“Verification Pending”,表示正在验证中,等到状态变为“Verified”时则命名空间创建成功

Tips:1.之前在GitHub创建的仓库可以删除;2.已审核通过的命名空间不可删除(其他状态可删除),也不可以修改

3.2域名的命名空间验证

如果你是通过域名创建的命名空间,则需要到对应的域名注册服务商配置一个域名解析用于验证
假设你的域名是www.abc.com, 则你创建的命名空间为“com.abc”
登录域名服务商,为abc.com添加一个域名解析, 这里以阿里云为例:

  • 登录阿里云app
  • 进入域名解析
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

记录类型选“TXT”,主机记录选“@”,记录值填写提交命名空间时生成的验证码,填写完毕后保存即可
后续和GitHub创建好仓库之后的操作一致
验证通过后可以删除这个域名解析

4.安装gpg
  • GPG Windows版下载
    在这里插入图片描述
  • 双击“gpg4win-4.3.1.exe”安装即可,具体过程省略
5.gpg生成密匙
  • 安装完成后,cmd打开命令行

输入"gpg --version"验证是否安装成功,如下代表安装成功

C:\Users\LENOVO>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\LENOVO\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.1pom文件完整配置

假设我已有审核通过的命名空间: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>项目描述,不要乱写,会展示在Maven中央仓库官网</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/zbc/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/zbc/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.2maven 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生成的UserToken
点击用户名,点击"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查看

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

IDEA deploy成功后,此处的Deployments列表就能看到,状态为待发布VALIDATED
不想发布了可以点击Drop删除
发布则点击Publish, 状态会变成发布中PUBLISHING, 中间需要等待一个小时、几个小时,甚至一天,具体时间不定
发布成功后,状态会变为已发布PUBLISHED, 这时项目就已经发布到中央仓库了
注意:发布成功的项目不能删除,也不能再次发布了

9.maven引用

发布成功后,全世界任何人都可以通过以下坐标引用你的项目了

<dependency><groupId>io.github.xxx</groupId><artifactId>abc</artifactId><version>1.0.0</version>
</dependency>

Tips:
https://mvnrepository.com/的同步比较慢,一般要3-5天才能搜索到(仅仅只是搜索不到而已,不影响别人引入和使用)
https://search.maven.org/ 在发布成功后就可以搜索到了

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词