1.创建一个OSS账户
2.创建一个项目
在oss上创建一个项目,并且根据经办人的指示,完成后续操作。
groupId可以选择:io.github.username
、com.gitee.username
,如果你有自己的网站可以用自己的domain。
3.安装并配置GPG
3.1安装
3.2配置
1)生成公钥:gpg --gen-key
根据提示输入用OSS户名和邮箱,并且输入秘钥,后面部署的时候需要填写秘钥。
2)查看公钥:gpg --list-keys
3)发布公钥
a.保证 gps 签名正常,多发几个服务器
gpg --keyserver http://pool.sks-keyservers.net:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keyserver.ubuntu.com:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.gnupg.net:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.openpgp.org:11371 --send-keys C87BD3F27FDE39FE0C5C376E923
b.验证是否发送成功
gpg --keyserver http://pool.sks-keyservers.net:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keyserver.ubuntu.com:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.gnupg.net:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
gpg --keyserver http://keys.openpgp.org:11371 --recv-keys C87BD3F27FDE39FE0C5C376E923
c.注意:如果发布公钥不成功,可以打开Kleopatra
软件,右键菜单,点击在服务器上发布,然后再执行上面的操作;当然,以上所有操作可以在Kleopatra图形化界面中完成。
4.配置Maven的setting.xml
配置oss仓库的认证信息
<servers>
<server>
<id>ossrh</id>
<username>Z/9hZm1p</username>
<password>9TNAXM0Y8N0PwzQelVDaYIcZO9sDwRJqgYaBF25l74dO</password>
</server>
<server>
<id>oss</id>
<username>Z/9hZm1p</username>
<password>9TNAXM0Y8N0PwzQelVDaYIcZO9sDwRJqgYaBF25l74dO</password>
</server>
</servers>
5.配置项目的pom.xml
<?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>your groupId</groupId>
<artifactId>your artifactId</artifactId>
<version>1.0.0-SNAPSHOTS</version>
<description>项目描述</description>
<name>artifactId</name>
<url>https://gitee.com/groupId/artifactId</url>
<licenses>
<license>
<name>GNU General Public License</name>
<url>http://www.gnu.org/licenses/gpl-3.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<!--输入在sonatype创建的账户和联系邮箱 -->
<name>kancy</name>
<email>793272861@qq.com</email>
<organization>kancy</organization>
<organizationUrl>https://www.cnblogs.com/kancy</organizationUrl>
</developer>
</developers>
<scm>
<url>https://gitee.com/groupId/artifactId.git</url>
</scm>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<!-- 源码 -->
<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>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--部署-->
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG 打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!--将组件部署到OSSRH并将其发布到Central Repository-->
<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>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>ossrh</id><!--与settings.xml的server.id保持一致-->
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>ossrh</id><!--与settings.xml的server.id保持一致-->
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
6.发布jar包
执行命令:mvn clean deploy -DskipTests
登录 https://oss.sonatype.org 可以查看到自己发布的jar包了,此时可能在中央仓库还不能查找到,需要等待一段时间,进行同步。
以后的发布,在maven中直接发布即可,不需要再次创建OSS项目了。
7.参考
【1】上传自己的jar到maven中央仓库
【2】将jar包发布到Maven中央仓库
【3】如何将JAR包发布到Maven中央仓库