(1)安装gpg并生成gpg秘钥。
笔者使用的是苹果笔记本电脑,故选择最简单的brew工具进行安装,其他操作系统可以用其他方法自行安装。
brew install -v gpg
生成GPG秘钥:
gpg --gen-key
按提示数据用户名,电子邮件地址等,最重要的是秘钥的密码,这个密码必须牢记。
最后,你必须公布你的公钥。
# 查看KEYID
gpg --list-keys
# 公布
gpg --keyserver hkp://keyserver.ubuntu.com --send-keys <公钥特征的最后16位>
(2) 注册Sonatype账户
登录成功以后,创建Issue。
- 项目下拉框选择 "Community Support - Open Source Project Repository Hosting (OSSRH)"
- 类型下拉框选择 "New Project"
- groupId 自行填写好 笔者的groupdId是 "com.github.yingzhuo"
- Project URL 自行填写好,这里笔者填写的是github上的项目地址
- SCM url 也填写好
- username项填写自己的Sonatype账号就行了。
点击新建之后就可以等待了,Sonatype的管理员必须要人工审核,笔者进行的异常顺利,大约10分钟后注册的 "groupId"就被管理员通过了。
(3) 开发开源项目
敲代码的过程当然略去不说,关键是maven的配置项。
3.1 ~/.m2/settings.xml配置Sonatype账号和密码
<servers>
<server>
<id>oss-release</id>
<username>[用户名]</username>
<password>[密码]</password>
</server>
<server>
<id>oss-snapshots</id>
<username>[用户名]</username>
<password>[密码]</password>
</server>
</servers>
3.2 配置要发布的项目
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<tag>master</tag>
<url>git@github.com:yingzhuo/spring-security-patch.git</url>
<connection>scm:git:git@github.com:yingzhuo/spring-security-patch.git</connection>
<developerConnection>scm:git:git@github.com:yingzhuo/spring-security-patch.git</developerConnection>
</scm>
<developers>
<developer>
<name>yingzhuo</name>
<email>yingzhor@gmail.com</email>
</developer>
</developers>
<distributionManagement>
<snapshotRepository>
<id>oss-snapshot</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss-release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<profiles>
<profile>
<id>sonar</id>
<activation>
<jdk>[1.8,)</jdk>
</activation>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<!-- GPG -->
<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>
</profile>
</profiles>
3.3 部署项目
mvn clean deploy -P sonar -Darguments="gpg.passphrase=gpg密钥密码"
3.4 验证与发布
登录到oss.sonatype.org/,找到"Staging Repositories"选择你部署的Repo, 勾选上以后点击"Close",等正常关闭以后再点击"Release"。如果成功的话,你的开源软件就发布到中央仓库了。 大概过几个小时就可以使用了。
3.5 通知Sonatype管理员关闭issue
找到在Sonatype上发布的issue,给出一个comment。用英语,说话稍微客气点。
我说的是:
My Open-Source project released! Please approval, thank you!
管理员审批通过以后会回复你,我进行得也很顺利,下楼吃个饭,审批就通过了。对方回复道:
Central sync is activated for com.github.yingzhuo. After you successfully release, your component will be published to Central, typically within 10 minutes, though updates to search.maven.org can take up to two hours. We are making improvements to our onboarding process for OSSRH, and we'd appreciate your feedback! Apply to participate here for a free Amazon gift card: https://www.surveymonkey.com/r/NS3VPGF
大概过1到2天,发布的开源软件就可以在mvnrepository.com搜索到了。