详解如何将JAR包发布到Maven中央仓库
作者:Fundebug 发布时间:2023-02-14 07:26:00
将jar包发布到Maven中央仓库(Maven Central Repository),这样所有的Java开发者都可以使用Maven直接导入依赖,例如fundebug-java:
<!-- 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.2
JDK:1.8.0_192
Maven:3.5.4
1. 注册JIRA账号
JIRA是一个项目管理服务,类似于国内的Teambition。Sonatype通过JIRA来管理OSSRH仓库。
注册地址:https://issues.sonatype.org/secure/Signup!default.jspa
需要填写Email, Full Name, Username以及password,其中Username与Password后面的步骤需要用到,请记下来。
2. 创建issue
通过在JIRA上创建issue来申请发布新的jar包,Sonatype的工作人员会进行审核,审核不算严格,一般按照要求填写不会有问题。
创建链接:https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134
创建issue的时候需要填写下面这些信息:
Summary
Description
Group Id
Project URL
SCM url
大家可以参考我申请发布fundebug-java与fundebug-spring时所填写的内容:OSSRH-45238
由于时差,前一天创建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/kiwenlau/.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的setting.xml
[setting.xml]()为Maven的全局配置文件,在MacBook上的位置为/usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml,我们需要将第1步配置的Username和Password添加到<servers></servers>
标签中,这样我们才能将jar包部署到Sonatype OSSRH仓库:
<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
配置时参考我的pom.xml,根据需要修改即可。
<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>com.fundebug</groupId>
<artifactId>fundebug-java-notifier</artifactId>
<version>0.2.0</version>
<packaging>pom</packaging>
<name>fundebug-java-notifier</name>
<url>https://github.com/Fundebug/fundebug-java-notifier</url>
<description>Capture Java and Spring exceptions automatically</description>
<licenses>
<license>
<name>Server Side Public License</name>
<url>https://www.mongodb.com/licensing/server-side-public-license</url>
<distribution>repo</distribution>
<comments>A not business-friendly OSS license</comments>
</license>
</licenses>
<scm>
<url>https://github.com/Fundebug/fundebug-java-notifier</url>
<connection>https://github.com/Fundebug/fundebug-java-notifier.git</connection>
</scm>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<developers>
<developer>
<name>kiwenlau</name>
<id>kiwenlau</id>
<email>kiwenlau@gmail.com</email>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<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>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
<modules>
<module>fundebug-java</module>
<module>fundebug-spring</module>
<module>examples/hello-world</module>
<module>examples/spring-rest-api</module>
</modules>
</project>
6. 发布jar包
执行mvn clean deploy
处理,即可将jar包发布到Sonatype OSSRH仓库。
mvn clean deploy -projects fundebug-java,fundebug-spring
我们的项目fundebug-java-notifier含有多个模块,仅需部署fundebug-java与fundebug-spring,因此使用-projects选项来指定。
第一次执行mvn clean deploy命令时,需要输入GPG密钥的密码。
mvn clean deploy命令执行成功的输出是这样的(部分日志):
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] fundebug-java 0.2.0 ................................ SUCCESS [ 22.183 s]
[INFO] fundebug-spring 0.2.0 .............................. SUCCESS [ 16.383 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.728 s
[INFO] Finished at: 2019-01-12T20:10:16+08:00
[INFO] ------------------------------------------------------------------------
7. close并release
mvn clean deploy命令执行成功,使用JIRA账号登陆:https://oss.sonatype.org/#stagingRepositories,就可以看到你所发布的jar包了:
选中对于的repository之后,点击箭头所指的close,close时会检查发布的构件是否符合要求。若符合要求,则close成功,成功之后点击箭头所指的release,即可正式将jar包发布到Sonatype OSSRH仓库。
release成功大概2个小时之后,该构件就会同步到Maven中央仓库:
参考
Guide to uploading artifacts to the Central Repository
OSSRH Guide
Maven入门教程
来源:https://segmentfault.com/a/1190000017877798
猜你喜欢
- idea2020.3.3配置Maven走过的那些坑idea2021最新激活码idea2020.3.3激活码有两种方法,小编在这推荐使用方法一
- 自定义注解实现接口幂等性方式近期需要对接口进行幂等性的改造,特此记录下。背景在微服务架构中,幂等是一致性方面的一个重要概念。一个幂等操作的特
- 本文实例为大家分享了C#实现快速排序的具体代码,供大家参考,具体内容如下代码:/// <summary> /// 排序 ///
- 项目概况:Spring Cloud搭的微服务,使用了eureka,FeignClient,现在遇到FeignClient调用接口时不支持上传
- 1. 什么是JvmJVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来
- 一、前言我们在日常开发中,避不开的就是参数校验,有人说前端不是会在表单中进行校验的吗?在后端中,我们可以直接不管前端怎么样判断过滤,我们后端
- 本文实例为大家分享了java实现微信红包的具体代码,供大家参考,具体内容如下要求基于BigDecimal类实现微信红包算法的功能,比如设置红
- 一、AXIS调用远程WebService,以国内手机号归属地查询为例 1、wsdl地址:http://ws.webxml.com.
- /// <summary> /// 读写INI文件的类。
- 一、实验题目二、分析实验要求为:实现一个界面,界面中包含一个文本显示区和两个按钮(存档和读档)读档按钮作用是打开文件并读取内容,将内容显示在
- SpringBoot接收文件和对象使用场景:某个接口,需要同时接收文件和实体,也就是参数一、这个时候,前端就不能json格式传送数据了,要用
- 目录基于Java的guava开源库工具类1、guava的maven配置引入 2、LoadingCache3、Multimap 和
- 引出泛型我们通过如下的示例,引出为什么泛型的概念。public class Test {public static void main(St
- 用java实现循环队列的方法:1、添加一个属性size用来记录眼下的元素个数。目的是当head=rear的时候。通过size=0还是size
- synchronized 和 Reentrantlock多线程编程中,当代码需要同步时我们会用到锁。Java为我们提供了内置锁(synchr
- AuthenticationProvider解析首先进入到AuthenticationProvider源码中可以看到它只是个简单的接口里面也
- JSR303简介JSR-303 是 JAVA EE 6 中的一项子规范,叫做 Bean Validation,官方参考实现是Hibernat
- Java注解是在JDK5时引入的新特性,鉴于目前大部分框架(如spring)都使用了注解简化代码并提高编码的效率,因此掌握并深入理解注解对于
- springboot远程debug调试1.首先去编辑器打开项目2.打开Edit Configurations 选择remote选项
- 1. 初始 Spring Boot1.1 什么是Spring BootSpring 的诞生是为了简化 Java 程序的开发的Spring B