记录一次发布jar包到maven中央仓库的经历
1.注册JIRA账号
注册地址:https://issues.sonatype.org/secure/Dashboard.jspa
注:这里密码要设置规则有点麻烦,看它的提示设置
2.创建 issue
注:这个里的groupId要和pom中的groupId一致(看别人写的,没证实过)
参考已经审核通过的
这些都是要填写的!Project URL 和SCM url 可以填写自己github项目的地址
创建好以后 sonatype的工作人员审核处理,速度还是很快的,一般一个工作日以内就有回复
比如:
让你验证下是否是你自己填写的,一般有几种方式,常用选择第一种
- Add a TXT record to your DNS referencing this JIRA ticket:
OSSRH-54313(Fastest)
自己添加好DNS后,再次点击回应按钮,一般还是一个工作日内会回复你:
这个就代表好了,可以继续操作了
3.配置maven Setting.xml文件
id要和pom中的id对应,配置过私服的应该都知道
用户名密码是注册jira的用户名密码
4.windows环境安装gpg4win
下载地址:https://www.gpg4win.org/download.html
安装成功后,进入cmd,输入命令:
gpg --gen-key
中间提示你要设置名字+邮箱,其他可以使用默认值,记住输入的passphrase,后面mvn deploy会用到
5.上传密钥
上传刚刚生成的秘钥
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 公钥ID
常用gpg命令如下:
查看是否安装成功
gpg --version
生成密钥对
gpg --gen-key
查看公钥
gpg --list-keys
将公钥发布到PGP密钥服务器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 公钥ID
查询公钥是否发布成功
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 公钥ID
下面是上传好的效果图
6.配置pom.xml(重点)
这里有好多坑,所以直接copy操作好的pom出来
<?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>ltd.vastchain</groupId>
<artifactId>vctc-java-sdk</artifactId>
<version>1.0.0</version>
<name>vctc-java-sdk</name>
<description>Official Java SDK for VastChain. https://www.vastchain.cn</description>
<url>https://github.com/vastchain/vctc-java-sdk</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<licenses>
<license>
<name>MIT License</name>
<url>https://github.com/everitoken/evt4j/blob/master/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>https://github.com/vastchain/vctc-java-sdk</url>
<connection>scm:git:git@github.com:vastchain/vctc-java-sdk</connection>
<developerConnection>scm:git:git@github.com:vastchain/vctc-java-sdk</developerConnection>
</scm>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</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>
</plugins>
</build>
<developers>
<developer>
<name>weihui</name>
<email>weihui@vastchain.ltd</email>
</developer>
</developers>
<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>
</project>
严格来说这些<licenses> <scm><>plugin都要配置,甚至name,description,url都要配置
如果不配置,deploy的时候会报错
之前遇到3类错误,各种标签missing,直接添加就好了
还有一种就是没有公钥,需要你重新上传一次公钥就好了,重新执行命令
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 公钥ID
就可以了
第三个错误是javadoc 找不到,所以我这边就配置了failOnError
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
7.maven deploy
和操作私服一样
build过程中会让你输入之前填写的passphrase
最后看到BUILD SUCCESS,就代表成功了,如果有错误,按照上面列的3种,解决掉再点一次deploy即可
8.发布到中央仓库
登陆https://oss.sonatype.org/#stagingRepositories
用户名密码和注册的jira账号一样
点击release就ok了
坐等2小时等它自己同步到maven中央仓库
jira那边也回复了
不过我这边是需要搜索下才显示
https://search.maven.org
如果直接搜索这个搜不到,需要搜索上面那个链接访问看看
注:如果没有通过配置自动发布插件nexus-staging-maven-plugin,进行mvn deploy,是看不到的,而且需要手动drop掉,比如: