这篇文章主要讲怎样将自己的jar包上传到maven 中央仓库,以便在项目中直接使用而不用构建自己的私服
例如:
<!-- https://mvnrepository.com/artifact/com.fundebug/fundebug-java -->
<dependency>
<groupId>com.fundebug</groupId>
<artifactId>fundebug-java</artifactId>
<version>0.2.0</version>
</dependency>
但是,Maven中央仓库并不支持直接发布jar包。我们需要将jar包发布到一些指定的第三方Maven仓库,然后该仓库再将jar包同步到Maven中央仓库。
其中,最"简单"的方式是通过Sonatype OSSRH仓库来发布jar包。接下来,我会介绍如何将jar包发布到Sonatype OSSRH。
本教程所使用的系统配置如下:
- OS: MacOS 10.14.1
- JDK:1.8.0_192
- Maven:3.5.4
下面开始配置
1、申请sonatype 账号
JIRA是一个项目管理服务,类似于国内的Teambition。Sonatype通过JIRA来管理OSSRH仓库。
注册地址:https://issues.sonatype.org/secure/Signup!default.jspa
2、创建issue
通过在JIRA上创建issue来申请发布新的jar包,Sonatype的工作人员会进行审核,审核不算严格,一般按照要求填写不会有问题。
创建链接:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
创建issue的时候需要填写下面这些信息:
- Summary
简单描述一下你的项目内容 - Description
详细描述你的项目功能 - Group Id
你需要发布的项目的group Id - Project URL
你的开源项目的 github 或者gitee 地址 - SCM url
你的开源项目的github 或者 gitee 仓库地址
参考地址 https://issues.sonatype.org/browse/OSSRH-53141
提交申请之后 由于时差可能需要半天到一天才会有回复回复内容如下:
这个时候你需要把你的域名添加一天TEXT 解析:
OSSRH-xxxxx 到 https://issues.sonatype.org/browse/OSSRH-xxxxxx
添加完成之后需要回复此issue 告诉对方已经添加完成
没有什么问题的话 对方会审核通过并告诉你可以上传jar包的仓库了 此时你需要在上传第一个包的时候回复此issue
此时issue的status变为RESOLVED,我们就可以进行下一步操作了。
3、装并配置GPG
发布到Maven仓库中的所有文件都要使用GPG签名,以保障完整性。因此,我们需要在本地安装并配置GPG。
安装GPG
MacBook安装GPG非常简单,下载并安装GPG Suite即可。
生成GPG密钥对
gpg --gen-key
生成密钥时将需要输入name、email以及password。password在之后的步骤需要用到,请记下来。
上传GPG公钥
将公钥上传到公共的密钥服务器,这样其他人才可以通过公钥来验证jar包的完整性。
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys CAB4165C69B699D989D2A62BD74A11D3F9F41243
其中CAB4165C69B699D989D2A62BD74A11D3F9F41243为密钥的ID,可以通过gpg --list-keys命令查看
gpg --list-keys
/Users/XXXXXX/.gnupg/pubring.kbx
----------------------------------
pub dsa2048 2010-08-19 [SC] [expires: 2020-06-15]
85E38F69046B44C1EC9FB07B76D78F0500D026C4
uid [ unknown] GPGTools Team <team@gpgtools.org>
sub elg2048 2010-08-19 [E] [expires: 2020-06-15]
sub rsa4096 2014-04-08 [S] [expires: 2024-01-02]
pub rsa2048 2019-01-03 [SC] [expires: 2021-01-02]
CAB4165C69B699D989D2A62BD74A11D3F9F41243
uid [ultimate] kiwenlau <kiwenlau@gmail.com>
sub rsa2048 2019-01-03 [E] [expires: 2021-01-02]
4、 配置maven settings.xml 此配置文件在电脑位置可能不同, 根据各自的设置找到位置修改
<servers>
<server>
<id>ossrh</id>
<username>Fundebug</username>
<password>passsword</password>
</server>
</servers>
5、配置pom.xml
pom.xml挺长的。根据Sonatype OSSRH的要求,以下信息都必须配置:
- Supply Javadoc and Sources
- Sign Files with GPG/PGP
- Sufficient Metadata
- Correct Coordinates
- Project Name, Description and URL
- License Information
- Developer Information
- SCM Information
我的配置文件:
<?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>cn.ipokerface</groupId>
<artifactId>mybatis</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>IPoker-MybatisOperator</module>
<module>IPoker-MybatisGenerator</module>
<module>IPoker-MybatisGeneratorMavenPlugin</module>
</modules>
<name>IPoker-Mybatis</name>
<description>Mybatis-Operator Parent Project.</description>
<!-->项目的协议<-->
<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>
<comments>A not business-friendly OSS license</comments>
</license>
</licenses>
<!-->开发者的信息<-->
<developers>
<developer>
<name>pokerface</name>
<id>pokerface</id>
<email>214888341@163.com</email>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<scm>
<url>https://gitee.com/PokerFace_Fo/IPoker-MybatisOperator</url>
<connection>https://gitee.com/PokerFace_Fo/IPoker-MybatisOperator.git</connection>
</scm>
<properties>
<project.main.verion>1.0.0-SNAPSHOT</project.main.verion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- JDK version -->
<java.version>1.8</java.version>
<!--mybatis-->
<mybatis-core.version>3.4.6</mybatis-core.version>
<!-- mybatis generator -->
<mybatis-generator-maven-plugin.version>1.3.7</mybatis-generator-maven-plugin.version>
<mybatis-generator-core.version>1.3.7</mybatis-generator-core.version>
<!-- maven -->
<apache.maven.artifact.version>2.2.1</apache.maven.artifact.version>
<apache.maven.core.version>3.3.9</apache.maven.core.version>
<apache.maven-plugin-api.version>3.5.4</apache.maven-plugin-api.version>
<apache.maven-plugin-api-annotations.version>3.5.2</apache.maven-plugin-api-annotations.version>
<apache.maven-plugin-source.version>3.0.1</apache.maven-plugin-source.version>
<skipTests>true</skipTests>
</properties>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<phase>package</phase>
<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>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<!-- 注意这两处的id必须与setting.xml中server配置的Id一致 -->
<id>ossrh-sonatype-repository</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>ossrh-sonatype-repository</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>
6、发布Jar包
执行mvn clean deploy处理,即可将jar包发布到Sonatype OSSRH仓库。
mvn clean deploy -projects IPoker-MybatisOperator,IPoker-MybatisGenerator,IPoker-MybatisGeneratorMavenPlugin
我的项目含有多个模块,因此使用-projects选项来指定发布modules。
第一次执行mvn clean deploy命令时,需要输入GPG密钥的密码。
mvn clean deploy命令执行成功的输出是这样的(部分日志):
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ mybatis-generator-maven-plugin ---
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.jar
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.jar (23 KB at 18.1 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.pom
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.pom (6 KB at 6.0 KB/sec)
Downloading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/maven-metadata.xml
Downloading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/maven-metadata.xml
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/maven-metadata.xml
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/maven-metadata.xml (350 B at 0.2 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/maven-metadata.xml
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/maven-metadata.xml (271 B at 0.3 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-sources.jar
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-sources.jar (13 KB at 10.2 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-javadoc.jar
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-javadoc.jar (54 KB at 35.4 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.jar.asc
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.jar.asc (833 B at 2.4 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.pom.asc
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0.pom.asc (833 B at 2.6 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-sources.jar.asc
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-sources.jar.asc (833 B at 2.3 KB/sec)
Uploading: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-javadoc.jar.asc
Uploaded: https://oss.sonatype.org/service/local/staging/deploy/maven2/cn/ipokerface/mybatis-generator-maven-plugin/1.0.0/mybatis-generator-maven-plugin-1.0.0-javadoc.jar.asc (833 B at 2.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] IPoker-MybatisOperator ............................. SUCCESS [ 25.716 s]
[INFO] IPoker-MybatisGenerator ............................ SUCCESS [ 13.398 s]
[INFO] IPoker-MyBatisGeneratorMavenPlugin ................. SUCCESS [ 18.559 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 57.788 s
[INFO] Finished at: 2019-11-17T18:58:36+08:00
[INFO] Final Memory: 32M/582M
[INFO] ------------------------------------------------------------------------
7、 确认发布 close release
mvn clean deploy命令执行成功,使用sonatype账号登陆:https://oss.sonatype.org/#stagingRepositories,就可以看到你所发布的jar包了:
选中对于的repository之后,点击箭头所指的close,close时会检查发布的构件是否符合要求。若符合要求,则close成功,成功之后点击箭头所指的release,即可正式将jar包发布到Sonatype OSSRH仓库。