写了个Java SDK(ava Library),想要让更多的猿媛使用,如何发布到中央仓库(Maven Central)呢?
踩了挺多坑,简单记录下。
groupId
首先,你需要向官方申请唯一的 groupId
。如何申请呢?
打开链接,注册一个帐户并登录: https://issues.sonatype.org/secure/Signup!default.jspa
点击导航栏中的 Create
,
Community Support - Open Source Project Repository Hosting (OSSRH)
New Project
my code project
Group Id 可输入你心仪的(我这里输入的是 com.github.xu42
,如果你需要用自己的域名倒置 一般需要做一下TXT解析)
Project URL 输入你的github project地址
SCM url同理
Username 输入你的用户名
这是我之前的,可以参考下: https://issues.sonatype.org/browse/OSSRH-41340
然后等待官方人员审核,一般2小时,审核通过后,可进行下一步
pgp
安全需要,需要对发布的代码进行pgp加密,如果本机没有安装 pgp
,自行搜索安装一下。
gpg --gen-key
,根据提示一路走下去,输入的密码要记一下,后面会用到
怎么发布呢?
gpg --keyserver hkp://keyserver.ubuntu.com --send-keys XXXXXX gpg --keyserver hkp://pool.sks-keyservers.net --send-keys XXXXXX gpg --keyserver hkp://keys.openpgp.org --send-keys XXXXXX
这一步就完事了,继续后面的
settings.xml
为了避免有不一致的地方,建议直接copy下面,然后更改中文处
<settings>
<servers>
<server>
<id>ossrh</id>
<username>你的Jira-用户名</username>
<password>你的Jira-密码</password>
</server>
</servers>
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>你的gpg密码</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
pom.xml
同样的,为了避免不一致的地方,建议直接copy,更改项目信息为你的
<?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>com.github.xu42</groupId>
<artifactId>china-mobile-number</artifactId>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>China Mobile Check</description>
<url>https://github.com/xu42/china-mobile-number-java-sdk</url>
<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>
<developers>
<developer>
<name>Xu Yangjie</name>
<email>xu42.cn@gmail.com</email>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/xu42/china-mobile-number-java-sdk.git</connection>
<developerConnection>scm:git:ssh://github.com:xu42/china-mobile-number-java-sdk.git</developerConnection>
<url>https://github.com/xu42/china-mobile-number-java-sdk/tree/master</url>
</scm>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</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>
<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>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</project>
终于到了关键的一步了
在项目目录下执行 mvn clean deploy -e
, 加 -e
主要是为了方便查看失败的原因。
发布 release 版本: mvn nexus-staging:release
其实这一关键一步,很简单,但可能遇到的坑非常多,下面列出几个,对号入座。
打包阶段,可能会签名失败,报错 gpg: signing failed: Inappropriate ioctl for device
如何解决?控制台输入 export GPG_TTY=$(tty)
,然后重新打包
还是打包阶段,报错 No public key: Key with id:xxxxxx
如何解决?可能是中央仓库在获取公钥时超时了,所以上面发布公钥发布到了多个地方,也是为了避免这个问题,如果还是出现了,重试、重试、重试
如果出现 rule failue xxxx
之类的,可能是官方没有审核好呢,等等吧然后继续重试,记得发布后需要在原Jira上备注一下已经release 以便审核。
打包之后,可以在 https://oss.sonatype.org/#stagingRepositories
看到处于 staging
的包,可以查看具体的信息