本文转载自 发布Jar到Maven中央仓库--Maven版(最新方式)
Maven发布jar包到中央仓库的方式发生了变化,之前的教程(上传jar包到maven中央仓库)部分失效了,https://oss.sonatype.org已经不再支持新用户注册,新的注册地址为 https://central.sonatype.com 。如果按照之前的教程推送jar包,会得到一个403响应。下面说下新版发布方式。
摘要
新版 Maven 中央仓库发布流程主要改变在于通过 Central Portal 进行,不再使用传统的 Jira 注册方式。你需要注册 Sonatype 账号并创建或验证命名空间,生成发布 Token 替代旧的用户名和密码进行认证,然后通过配置 Maven 的 settings.xml
和 pom.xml
文件,使用 GPG 生成签名密钥,最后打包并上传到中央仓库。
新版本推送方式从注册到验证到发布,都在统一的网站 https://central.sonatype.com ,实际上比以前的发布方式要简单的多。
一、将项目推送到远程仓库,如 Github
或者Gitee
二、注册 Sonatype
账户
进入 https://central.sonatype.com 注册一个账号,邮箱要真实。
三、登录 Sonatype 创建Namespace
进入 Namespace 模块,然后点击 Add Namespace 按钮,Namespace 具有唯一性。发布前一定要创建Namespace
,实际上就是项目的Group Id
,因为要进行验证,所以这里不能随便填写,你可以配置为你拥有的域名,比如我的域名是hanqunfeng.com
,这里就填写com.hanqunfeng
。域名的验证方法是配置DNS TXT记录。
如果你没有域名,也可以使用Github的域名,比如我的Github用户名是hanqunfeng
,则这里可以配置为io.github.hanqunfeng
。Github的验证方式是根据给定个名称创建一个public repository。
四、发布
1.准备签名
方法一:可以使用工具创建密钥对
需要下载一个签名工具,我是mac电脑,下载的是https://gpgtools.org。
安装后点击新建,按照提示创建一个密钥对即可,注意高级选项里有个过期时间,默认是3年
。创建好后会主动提示你是否将公钥发布到key server
,点击Upload Public key
即可。也可以在创建后的证书列表页面邮件选择证书
–>Send Public Key To Key Server
。
导出证书时,勾选密码并设置密码就是私钥和公钥证书,不勾选密码就是公钥,看生成文件的名称就可以,公开就是公钥,私密就是私钥,格式都是asc
,其实就是字符串
,可以用记事本打开查看。
如果windows系统,可以下载https://www.gpg4win.org/ ,使用方式差不多,最后点击“将公钥上传的目录服务”。
公钥发布到key server
后要稍微等一会,大约10分钟
吧,因为key server
有多个,同步需要一些时间。
记住你创建密钥对时的密码,发布项目时要使用。
方法二:也可以使用命令行创建密钥对,版本[gpg (GnuPG/MacGPG2) 2.2.24]
# 创建密钥对,按提示输入用户名称和邮箱地址
gpg --generate-key
# 列出密钥,hanqunfeng就是创建密钥对是的用户名,此处也可以使用邮箱
# 结果中第二行一长串的后8位就是keyId,比如:30FF8D58,gradle构建时会用到
gpg --list-keys hanqunfeng
# 也可以直接通过id查询
gpg --list-keys 30FF8D58
# 上传公钥到server key,默认上传到hkps://keys.openpgp.org,但是提示上传失败
# 看到网上的示例可以通过--keyserver指定上传的服务器地址,但是我这个版本[gpg (GnuPG/MacGPG2) 2.2.24]没有这个参数
# 使用 https://gpgtools.org 上传公钥就会成功
gpg --send-keys 30FF8D58
# 查看指纹
gpg --fingerprint 30FF8D58
# 删除私钥,这里也可以使用用户名称或者邮箱,如果唯一的话
gpg --delete-secret-keys 30FF8D58
# 删除公钥
gpg --delete-keys 30FF8D58
2.settings.xml
配置 maven
的 settings.xml
文件,设置一个 server
,里面添加 User Token
。
登录https://central.sonatype.com后点击右上角的用户名称–> View Account --> Generate User Token
<settings>
[...]
<servers>
[...]
<server>
<id>maven-central</id>
<username>username</username>
<password>token</password>
</server>
</servers>
</settings>
3.pom.xml
重点是后面几个plugin
这里重点说一下central-publishing-maven-plugin
这个插件,该插件会将jar包推送到Maven Central
仓库,如果插件没有配置参数autoPublish
为true
,则但此时发布的jar会处于VALIDATED
状态,需要登录https://central.sonatype.com后切换到Deployment,找到我们刚刚上传的包名,然后点击右侧的Publish
按钮,如果一切顺利,大于10分钟后其状态变为PUBLISHED
,表示发布成功。如果发布失败,其状态会变更为FAILED
,可以在Component Summary
中查看失败原因,重新发布即可。
<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.hanqunfeng</groupId>
<artifactId>aws-s3-v2-tools</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>aws-s3-v2-tools</name>
<description>
AWS S3 Tools.
</description>
<url>https://blog.hanqunfeng.com</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>hanqf</id>
<name>hanqunfeng</name>
<email>qunfeng_han@126.com</email>
</developer>
</developers>
<scm>
<connection>scm:git:https://github.com/hanqunfeng/aws-s3-v2-tools.git
</connection>
<developerConnection>
scm:git:https://github.com:hanqunfeng/aws-s3-v2-tools.git
</developerConnection>
<url>https://github.com/hanqunfeng/aws-s3-v2-tools</url>
</scm>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.23.10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<!-- 为避免Potential Conflicts,你应该将commons-logging.jar从classpath中删除。你可以尝试从项目依赖中排除commons-logging,这样你的应用程序就会被强制使用Spring JCL而不是Commons Logging。 -->
<!-- 问题:Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3-transfer-manager</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-crt-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.32</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-source</id>
<phase>verify</phase>
<goals>
<!--生成源代码的jar -->
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadoc</id>
<phase>verify</phase>
<goals>
<!--生成javadoc的jar -->
<goal>jar</goal>
<!--生成javadoc的html -->
<goal>javadoc</goal>
</goals>
<configuration>
<!--不显示javadoc警告-->
<additionalOptions>-Xdoclint:none</additionalOptions>
<additionalJOption>-Xdoclint:none</additionalJOption>
</configuration>
</execution>
</executions>
</plugin>
<!-- gpg plugin,用于签名认证 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 发布到私服时需要注释掉下面两个插件 -->
<!--staging puglin,用于自动执行发布阶段(免手动)-->
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<!-- 这里的publishingServerId是在settings.xml中配置的server认证信息 -->
<publishingServerId>maven-central</publishingServerId>
<!-- 这里的autoPublish是自动发布,而不是手动发布 -->
<autoPublish>true</autoPublish>
<!-- 这里的waitUntil配置为published是等待发布完成,因为发布完成的时间比较长,所以可以不加这个参数 -->
<waitUntil>published</waitUntil>
<!-- 这里的deploymentName是发布到中央仓库的名称 -->
<deploymentName>${project.groupId}:${project.artifactId}:${project.version}</deploymentName>
</configuration>
</plugin>
<!-- release plugin,用于发布到release仓库部署插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
<distributionManagement>
<!-- <repository>-->
<!-- <id>maven-releases</id>-->
<!-- <name>Nexus Release Repository</name>-->
<!-- <url>http://nexus.cxzh.ltd:8081/repository/maven-releases/</url>-->
<!-- </repository>-->
<!-- <snapshotRepository>-->
<!-- <id>maven-snapshots</id>-->
<!-- <name>Nexus Snapshot Repository</name>-->
<!-- <url>http://nexus.cxzh.ltd:8081/repository/maven-snapshots/</url>-->
<!-- </snapshotRepository>-->
</distributionManagement>
</project>
4.执行命令
mvn clean package # 完成打包和测试
mvn clean verify # 完成源码打包和javadoc打包,同时完成签名
mvn clean deploy # 完成本地部署和maven中央仓库部署
执行过程中会提示你输入创建密钥对时的密码,如果不想人工参与,也可以使用如下方式(参考:http://maven.apache.org/plugins/maven-gpg-plugin/usage.html)
在执行命令时指定密码:
mvn clean deploy -Dgpg.passphrase=thephrase
setting.xml中创建一个server
<settings>
[...]
<servers>
[...]
<server>
<id>gpg.passphrase</id>
<passphrase>clear or encrypted text</passphrase>
</server>
</servers>
</settings>
5.更新部署
修改pom.xml
中的版本号,重新执行mvn clean deploy
即可。发布的jar包可以在https://central.sonatype.com中检索。
注意:本文归作者所有,未经作者允许,不得转载