注册Sonatype的账户
在Sonatype上提交关于本项目的发布申请
点击页面上方 Create 按钮即可创建此项目的 Issue。
Project: Community Support - Open Source Project Repository Hosting (OSSRH)
Issue TypeRequired: New Project
Summary: java-toolkits (自己项目名称)
Group Id:com.github.songyz0310 (maven项目坐标 Group Id)
Project URL:https://github.com/songyz0310/java-toolkits (项目地址)
SCM url:https://github.com/songyz0310/java-toolkits.git (源代码地址)
配置Maven
<server>
<id>sonatype-nexus-snapshots</id>
<username>Sonatype账号</username>
<password>Sonatype密码</password>
</server>
<server>
<id>sonatype-nexus-staging</id>
<username>Sonatype账号</username>
<password>Sonatype密码</password>
</server>
配置项目 pom.xml 文件
增加开源许可协议,SCM信息,开发者信息等待根据自己信息填写即可
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<scm>
<connection>scm:git:https://xxxx/xxx.git</connection>
<developerConnection>scm:git:xxxx.git</developerConnection>
<url>git:https:/xxxxxx.git</url>
</scm>
<developers>
<developer>
<name>xxx</name>
<email>xxxx</email>
<organization>xxx</organization>
</developer>
</developers>
如果发布Release版本,需要添加Release的相关profile配置,distributionManagement节和maven-compiler-plugin节的配置信息根据自己的实际情况做修改。
<profile>
<id>release</id>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<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>
<show>private</show>
<nohelp>true</nohelp>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<additionalparam>-Xdoclint:none</additionalparam>
<!-- TODO 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!--Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<verbose>true</verbose>
<encoding>UTF-8</encoding>
<showWarnings>false</showWarnings>
</configuration>
</plugin>
<!--Release -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.1</version>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
配置PGP的公钥信息
由于我使用的是Windows,Mac和Linux用户请自行查找本步骤解决方案
Windows下使用gpg4win来进行配置。 下载地址 安装好之后,打开cmd控制台,使用 gpg --list-keys 命令来查询自己配置的key 如果提示gpg不是可执行程序或命令,请配置Path路径到 GnuPG 的bin目录
生成密钥的命令 gpg --gen-key,过程中需要使用到姓名、邮箱等信息,这里的配置最好和Sonatype注册信息、pom文件配置信息保持一致,以免不必要的麻烦。 生成的过程中,会有个弹框要求输入Passphase信息,这个是密钥的密码,同样需要记牢。发布签名的过程中会用得到。
使用 gpg --list-keys 命令查询配置好的公私钥信息,使用 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <公钥key> 将公钥信息发送到ubuntu.com服务器,后续推送maven仓库会做校验。
打包Deploy
使用 mvn clean deploy -P
命令,一键打包。 打包过程中会要求输入gpg密钥的**Passphase
**信息,输入即可。
【友情提示】如果打包过程中出现了401类的错误,可能是因为Maven的配置文件中Server节点配置的用户名和密码不正确,或者 Issue 还未审核通过。
发布Release
进入 https://oss.sonatype.org/#stagingRepositories
查看发布好的构件,点击左侧的Staging Repositories,可以使用Group Id或其他信息搜索自己的项目,
此时的构件状态为 Open,选中构件,并点击上方的 Close–>Confirm 在下边的Activity选项卡中查看状态。
当状态变成closed后,执行 Release–>Confirm 并在下边的Activity选项卡中查看状态。 成功后构件自动删除,一小段时间(约1-2个小时)后即可同步到maven的中央仓库。届时会有邮件通知。
*至此,发布到Maven中央仓库完成。
收尾工作
对自己提交的Issue增加Comment,留言致谢并表示发布已经完成,请工作人员关闭Issue。
有始有终吧。☺☺☺收工~~~