前言
本文记录在没有私有 maven 仓库的情况下,如何发布项目到公有仓库。以便在有网络的情况下,使用 maven 坐标获取。
正文
Sonatype 简介
Sonatype 使用 Nexus 为开源项目提供托管服务。你可以通过它发布快照 (snapshot) 或是稳定版 (release) 到 Maven 中央仓库。我们只要注册一个 Sonatype 的 JIRA 账号、创建一个 JIRA ticket,然后对 POM 文件稍作配置即可。
步骤
1. 注册账号
打开 这个网站 注册账号,需要注意的是密码必须超过 12 位,且包含至少一个大写字符,一个小写字符,一个特殊字符,以及不少于三种的不同字符(字符,数字,符号)。描述的有些拗口,简单说就是包含大写字母、小写字符、符号和数字,并且超过 12 位即可。所以密码类似于 Ff123456789/ 这样的。
2. 创建 issue
登录成功后,打开 这个链接
其中 Summary 可以填写项目名
Description 填写项目介绍
groupId 填写 io.github.prpc (改为你的)
Project URL 和 SCM url 可以填写 github 项目对应的访问地址。
如图示例:
点击创建后会发起一个 issue,页面会自动跳转至一个新页面,如图示:
可以看到这里的状态是OPEN,这个状态其实是需要等待审核的,期间可能会经过多次状态变更,直至状态变为RESOLVED。注意:这个审核时间不确定。
需要关注审核状态的变更,一般变更会在下面增加新的评论,如果需要我们做什么按照指示进行操作即可。
3. 安装加密软件
用于对需要上传的文件加密和签名,windows 环境下载地址 GPG,下载有些慢,如有需要可从第三方源进行下载。
安装完成后在命令行输入 gpg --gen-key 命令生成自己的 public key,除了姓名、邮箱、备注外其他都可以使用默认配置,最后需要填写一个 passphase(数字即可比如: 62107872006),它在后面 mvn release 签名时会用到。
最后控制台会显示如下画面:
当我们点击确认后,因为这个加密软件其实是有界面的。它会自动帮我们导入,接下来页面会变成这样:
我们需要在这行记录上点击右键,选择在服务器上发布,它可能会提示我们需要生成撤销证书文件,一般而言我们很难遇到需要撤销该证书的情况,所以选择忽略。
成功后出现如上画面。
4. 修改项目
修改 pom
<!-- Configuration for maven central repository -->
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>oss</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
<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>
<url>https://github.com/pleuvoir/plugins-support</url>
<connection>scm:git:https://github.com/pleuvoir/prpc.git</connection>
<developerConnection>scm:git:https://github.com/pleuvoir/prpc.git</developerConnection>
<tag>v${project.version}</tag>
</scm>
<!-- 修改这里 -->
<developers>
<developer>
<name>pleuvoir</name>
<email>pleuvior@foxmail.com</email>
<url>https://pleuvoir.github.io</url>
</developer>
</developers>
修改 maven setting.xml
<!-- Sonatype 账号 -->
<settings>
<servers>
<server>
<id>oss</id>
<username>sonatype帐号</username>
<password>sonatype密码</password>
</server>
</servers>
</settings>
<!-- 配置 gpg 的签名 -->
<settings>
<profiles>
<profile>
<id>oss</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg2</gpg.executable>
<gpg.passphrase>修改这里 the_pass_phrase,上一步有提到</gpg.passphrase>
</properties>
</profile>
</profiles>
</settings>
5. 发布
mvn clean deploy -DskipTests -P release
或者使用IDEA编辑器选择即可发布。
搜索项目
打开 这个网址,search io.github.prpc 即可找到上传的项目信息。
后语
本文记录了如何发布开源项目到Sonatype,希望对大家有所帮助。