首页 > 应用开发 > Java > 一站式指南:将你的组件发布到 Maven 中央仓库
Maven 中央仓库由 Sonatype 维护,所有发布操作需通过其平台授权:




为防止组件被篡改,中央仓库要求所有发布的文件必须经过 GPG 签名:

gpg --gen-key,按提示填写真实姓名、邮箱(与 Sonatype 账号一致),设置密钥密码并牢记


gpg --keyserver h@k@p://keyserver.ubuntu.com:11371 --send-keys 你的密钥ID,让中央仓库能验证签名合法性 (删除命令中的@)gpg --keyserver keyserver.ubuntu.com --recv-keys xxxxxx;另外一种方式直接到
https://keyserver.ubuntu.com/秘钥平台查询:
出现以上内容表明秘钥发布成功。
打开 Maven 的 settings.xml 文件(通常在 conf 目录下),添加以下配置:
<!-- Sonatype访问权限配置 --> <servers> <server> <id>central</id> <username>Sonatype生成的Token用户名</username> <password>Sonatype生成的Token密码</password> </server> </servers> <!-- GPG签名配置 --> <profiles> <profile> <id>gpg</id> <properties> <gpg.executable>gpg</gpg.executable> <gpg.keyname>你的GPG绑定邮箱</gpg.keyname> <gpg.passphrase>你的GPG密钥密码</gpg.passphrase> <gpg.useagent>true</gpg.useagent> </properties> </profile> </profiles>
修改待发布项目的 pom.xml,补充必要信息(直接复制替换占位符即可):
<project> <!-- 核心信息:GroupID需与验证通过的Namespace一致 --> <groupId>io.github.你的用户名</groupId> <artifactId>组件名称</artifactId> <version>1.0.0.RELEASE</version> <!-- 必须是正式版本,禁止SNAPSHOT --> <url>你的代码仓库地址(如https://github.com/用户名/仓库名)</url> <!-- 许可证信息(推荐Apache 2.0) --> <licenses> <license> <name>The Apache License, Version 2.0</name> <url>https://www.apache.org/licenses/LICENSE-2.0</url> </license> </licenses> <!-- 开发者信息 --> <developers> <developer> <name>你的姓名</name> <email>你的邮箱</email> </developer> </developers> <!-- 代码仓库信息 --> <scm> <connection>scm:git:你的仓库克隆地址(如https://github.com/用户名/仓库名.git)</connection> <developerConnection>scm:git:你的仓库SSH地址(如git@github.com:用户名/仓库名.git)</developerConnection> <url>你的仓库网页地址</url> </scm> <!-- 必要插件配置 --> <build> <plugins> <!-- 源码打包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- Javadoc打包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <configuration> <charset>UTF-8</charset> <encoding>UTF-8</encoding> <docencoding>UTF-8</docencoding> <additionalparam>-Xdoclint:none</additionalparam> </configuration> <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.1.0</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.4.0</version> <extensions>true</extensions> <configuration> <publishingServerId>central</publishingServerId> <tokenAuth>true</tokenAuth> </configuration> </plugin> </plugins> </build> </project>
mvn clean deploy -Dmaven.test.skip=true,过程中会提示输入 GPG 密钥密码,输入后等待执行完成

发布成功后,可通过 Maven 中央仓库搜索页,输入 GroupID 或组件名称查询你的组件。其他开发者只需在 pom.xml 中添加以下依赖,即可直接使用:
<dependency> <groupId>io.github.你的用户名</groupId> <artifactId>组件名称</artifactId> <version>1.0.0.RELEASE</version> </dependency>
至此,你的组件就正式加入 Maven 中央仓库的生态了!从自己用的工具到全球开发者可复用的组件,只差这一套标准化的发布流程。如果遇到问题,可参考 Sonatype 官方文档或留言交流,祝你发布顺利 ~
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70049421/viewspace-3109057/ ,如需转载,请注明出处,否则将追究法律责任。