欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

从零开始上传开源项目到Maven中央仓库

发布时间:2024/3/26 81 豆豆
生活随笔 收集整理的这篇文章主要介绍了 从零开始上传开源项目到Maven中央仓库 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

目录

  • 介绍
  • 简单讲解一下上传到Maven中央仓库流程
  • Maven(sonatype)账号准备
    • 1.注册Sonatype JIRA账号
    • 2.创建工单
    • 3.验证GroupId
      • 基于域名验证
      • 基于开源仓库的验证
    • 4.流转工单状态
    • 5.工单处理完成
    • 6.登录nexus repository manager
    • 7.nexus repository manager平台生成User Token
  • Maven项目打包并上传流程
    • 打包简要配置说明
    • 1.工程pom.xml配置
      • pom项目基本信息
      • maven中snapshot与release版区别
      • 配置分发管理
      • pom打包插件配置
    • 2.Gpg 签名工具安装与使用
      • 下载
      • 生成秘钥
    • 3.配置~/.m2/setting.xml
    • 4.打包-部署
  • 最后流程
  • 坑总结
    • 中央仓库无法找到我的包
    • https://oss.sonatype.org与https://s01.oss.sonatype.org
    • 心得

介绍

第一次比较正式的开源的自己的项目,心里还是很激动的,于是迫不及待的想要上传到Maven中央仓库,方便使用者引用。本教程将会从零开始讲解,但是由于时效的变化,在每个过程还是需要读者本身自己思考如何处理遇到问题。

简单讲解一下上传到Maven中央仓库流程

  • 首先我们需要创建一个Sonatype JIRA的账号,这个平台可以理解为一套工单跟踪系统,它和Maven仓库可以说没有关系,也可以有关系。
  • 创建好上述账号后,我们需要创建一个工单,此工单的作用便是新建一个开源的Maven工程
  • 工单创建好后后台管理员会审核你提交的信息,在审核以及各种准备都完成后,你便可以登录给你分配的nexus repository manager平台管理你发布的maven归档。这里需要注意,这并不是中央仓库,这是给你分配的一个maven私服之一,当然最终会同步到中央仓库。
  • 配置我们的开源项目并部署到分配的maven私服。
  • 在私服访问归档是实时的,但是同步到中央仓库是需要时间的。
  • 好了,大体流程就这样,下面我们就一步步来探索吧。
  • Maven(sonatype)账号准备

    1.注册Sonatype JIRA账号

    https://issues.sonatype.org/secure/Signup!default.jspa这个连接便是注册地址,下方是图片

    按照提示注册即可,没有什么需要说的,唯一需要说的就是账号密码记录清楚,毕竟这个不常用。注册完成后就登录

    2.创建工单

    点击下方的Create按钮,界面风格可能在以后有变动,但是大体意思很明确,那就是找到创建工单的地方

    Proejct选择(Community Support - Open Source Project Repository Hosting (OSSRH)),这里面选项很多,我们选的这个选项意思就是(社区支持的开源项目托管库OSSRH-我们就可以理解为这就是Maven私服仓库)

    根据下方的表格提示填入内容,这里说明一下

    • Summary就是你的项目名
    • Description选填
    • GroupId(这个是最重要的,后台管理员审核的也是这个,往下看会专门介绍GroupId如何写,当然这个在创建成功后也可以改)
    • Project URL 你的项目官网或者git地址都可以的
    • SCM url 你的开源仓库地址,比如github,gitee等等
    • username 你的牛逼名称

    3.验证GroupId

    由于我都已经把所有流程创建好了,所以我只能根据工单的变化日志来写教程了。最开始我的GroupId是org.pettyfox.timeline

    上图大概意思是说你是否拥有pettyfox.org,本来想去买个.org域名的但是遗憾的发现,目前国内买不了,因此我便更改GroupId为com.pettyfox.timeline,在之前我是拥有pettyfox.com域名的,

    基于域名验证

    Do you own the domain pettyfox.org? If so, please verify ownership via one of the following methods:Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-71087 (Fastest) https://central.sonatype.org/faq/how-to-set-txt-record/ Setup a redirect to your https://github.com/mathcoder23 page (if it does not already exist)

    大体意思就是在域名的根解析中加一个TXT类型的解析,并且内容是OSSRH-71087这里你的根据你自己的OSSRH-xxx来更改,这个域名操作应该很简单吧,我就不贴步骤了。这样做的目的很简单,就验证你是否是这个域名的拥有者。

    关于域名与GroupId的关系:如果我们拥有域名 比如opensource.com,那么GroupId必须是com.opensource开头,后面怎么写不重要.

    基于开源仓库的验证

    有时候我们没有域名,并且也不想去买,那么我们可以通过开源仓库的平台来验证,比如GitHub、GitLab、Gitee、Bitbucket、SourceForge
    下面是官方的引导教程

    If you do not own this domain, please read: https://central.sonatype.org/publish/requirements/coordinates/You may also choose a Group ID that reflects your project hosting, in this case, something like io.github.mathcoder23 Edit your Group Id and create public repo called https://github.com/mathcoder23/OSSRH-71087 https://central.sonatype.org/publish/requirements/coordinates/#supported-code-hosting-services-for-personal-groupidcom.github Group IDs are invalid now but io.github may be used instead. Please read the following for more info: https://central.sonatype.org/changelog/#2021-04-01-comgithub-is-not-supported-anymore-as-a-valid-coordinateIf you decide to change your Group ID, please edit this ticket and update the Group ID field.

    首先你可以进入 https://central.sonatype.org/publish/requirements/coordinates/查看详细的步骤。
    下面是GroupId与项目名称的对应关系,安装对应的教程来操作即可

    最关键的一步应该就是创建一个公开仓库名为OSSRH-xxx或者在github的pages网页中创建OSSRH-xxx

    GroupId的验证基本上就是这样,其中最关键的还是根据管理员推送的GroupId验证指导来,仔细阅读他说的内容即可,这才是最好的方案,因为这些流程随时可能会更新、废弃等等。

    4.流转工单状态

    这一步的作用就是告诉管理员我们的准备做好了,你帮我处理一下。
    我这里已经是RESOLVED了,你们的应该是OPEN,我忘记在哪里点了,反正在红框中找到某个按钮将状态改为:WAITING FOR RESPOSE即等待响应

    变更OPEN为等待响应状态

    5.工单处理完成

    如果一切顺利,大约几分钟就会有结果,结果如下,最终的工单状态会变成RESOLVED

    第一行的变更组名不过在意,下方的消息就是告诉我已经验证我是com.pettyfox的拥有者,
    告诉我的Maven私服管理地址是https://s01.oss.sonatype.org这里需要注意的是你的地址到底是哪个需要根据评论的内容来,因为在以前是oss.sonatype.org,我的内容如下

    com.pettyfox.timeline has been prepared, now user(s) pettyfox23 can: Publish snapshot and release artifacts to s01.oss.sonatype.org Have a look at this section of our official guide for deployment instructions: https://central.sonatype.org/publish/publish-guide/#deploymentPlease comment on this ticket when you've released your first component(s), so we can activate the sync to Maven Central. Depending on your build configuration, this might happen automatically. If not, you can follow the steps in this section of our guide: https://central.sonatype.org/publish/release/######As part of our efforts to improve the security and quality posture of the open source supply chain, we plan to enable additional scanning of dependencies for security alerts soon. Since you're already hosting your source code in Github, you can get insights into code and dependency quality today by enabling the free Muse code quality scan. This will provide feedback right in your pull requests. More information can be found at https://www.sonatype.com/product/musedev-maven######

    上面的文字就是告诉我们Maven仓库地址,以及快照地址、正式版地址
    这里要注意一个坑,Please comment on this ticket when you've released your first component(s), so we can activate the sync to Maven Central.这里说了一句你必须评论一次(最下方Comment发一条内容),才能激活组件同步到中央仓库

    6.登录nexus repository manager

    由上述官方回复的内容可知,我们的nexus repository manager地址是https://s01.oss.sonatype.org,并且账号密码与Sonatype JIRA的账号密码相同
    登录后页面如下

    为了后续方便maven打包上传验证,我们申请基于Token的方式验证

    7.nexus repository manager平台生成User Token

    此步骤可以在后面打包上传时来完成,可以跳过
    在后台右上角点击用户名后,点击Profile

    选择下方下拉框,选中User Token

    按照提示创建一个User Token即可,并且保存

    现在关于账号的准备我们已经全部完成

    Maven项目打包并上传流程

    这里说明一下,其实官方引导的文档已经说的很清楚了下面我贴一下地址,可以参考

    • https://central.sonatype.org/publish/publish-maven/

    打包简要配置说明

  • 在Maven中完善项目基本介绍
  • 配置开发者信息
  • 配置scm开源代码信息
  • 配置分发仓库地址
  • 配置java源码生成插件
  • 配置java-doc生成插件
  • 配置gpg jar包签名插件(作用是保证jar包是你编译的,并且不会被别人篡改)
  • 配置ossrh登录账号
  • 1.工程pom.xml配置

    一下的pom是指工程下的pom.xml

    pom项目基本信息

    <name>timeline-mq</name> <description>A message queue for timeline. core is redis stream</description> <url>https://github.com/mathcoder23/timeline-mq</url> <licenses><license><name>The Apache Software License, Version 2.0</name><url>http://www.apache.org/licenses/LICENSE-2.0.txt</url></license> </licenses> <developers><developer><name>Petty Fox</name><email>mathcoder23@126.com</email><url>https://github.com/mathcoder23</url></developer> </developers><scm><url>https://github.com/mathcoder23/timeline-mq</url><connection>https://github.com/mathcoder23/timeline-mq.git</connection><developerConnection>https://github.com/mathcoder23/timeline-mq</developerConnection></scm>

    这一步配置按照理解填写即可

    maven中snapshot与release版区别

    在maven版本管理中,将jar包划分为两个重要的版本一个是快照版、一个是正式版,快照版意味着可能频繁的更新、变动,因此maven同步时每次都要去拉

    配置分发管理

    <distributionManagement><snapshotRepository><id>ossrh</id><url>https://s01.oss.sonatype.org/content/repositories/snapshots</url></snapshotRepository><repository><id>ossrh</id><url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url></repository></distributionManagement>

    这里需要注意一下,你的私服地址与我的是否一样很重要,你需要去看工单中官方给你回复的指导文档中地址是多少才是多少

    pom打包插件配置

    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>2.2.1</version><executions><execution><id>attach-sources</id><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><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions><configuration><tags><tag><name>date</name></tag></tags></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-gpg-plugin</artifactId><version>1.5</version><executions><execution><id>sign-artifacts</id><phase>verify</phase><goals><goal>sign</goal></goals></execution></executions></plugin><plugin><groupId>org.sonatype.plugins</groupId><artifactId>nexus-staging-maven-plugin</artifactId><version>1.6.7</version><extensions>true</extensions><configuration><serverId>ossrh</serverId><nexusUrl>https://s01.oss.sonatype.org/</nexusUrl><autoReleaseAfterClose>false</autoReleaseAfterClose></configuration></plugin></plugins></build>

    建议上述的pom代码不要不思考的复制粘贴,一定要想它的作用是什么,跟你的环境是否一致

    2.Gpg 签名工具安装与使用

    下载

    https://www.gnupg.org/download/安装提示下载即可,然后自行安装。安装完成后重启命令行,应该便会有gpg命令

    生成秘钥

    指定命令 gpg --gen-key,按照提示填入相应信息

    maven-gpg-plugin插件应该会读取Gpg的密钥信息

    3.配置~/.m2/setting.xml

    下面配置用于在打包后上传的认证
    这里的账号密码可以是上述教程生成的User Token也可以是登录密码,
    需要注意server下的id必须与pom.xml中定义的一致

    <servers><server><id>ossrh</id><username>XRLLzbbOaaN</username><password>xxx</password></server> </servers>

    4.打包-部署

    mvn clean package
    执行此命令后,就会开始打包,在过程中会提示输入Gpg的密钥密码。

    最后流程

    完成打包命令后,我们登录https://s01.oss.sonatype.org/

    一般我们发布的包,在Staging Repositories,需要经历Close检测,然后在Release,但是有些会自动完成的。假如你发现Staging Repositories有你提交的包,那么你需要Close一下,然后Release,否则你直接在此仓库中搜索就可以搜索到的。
    如下图

    上述完成后我们搜索组件


    至此,我们便成功了

    坑总结

    中央仓库无法找到我的包

    看下方红框的内容,意思是你需要评论一下这个工单,才会激活将私仓同步到中央仓库的行为。


    最后我们点击左下角的Comment评论过后,高兴的收到成功的回复了。大概意思就是说,已经给我开启了同步任务,在https://repo1.maven.org/maven2/可以拉到组件的时间通常在半个小时,在https://search.maven.org可搜索到通常在4个小时。

    https://oss.sonatype.org与https://s01.oss.sonatype.org

    在以前的一下教程中,是使用oss这个二级域名,但是2021年二月开始,使用s01这个三级域名
    从这个三级域名来看,后面不保证不会出现s02、s03这样的地址所以自己看自己对应的文档才是最重要的

    OSSRH Usage Notes⚓︎ Accessing Repositories⚓︎ The following repositories allow you to access your components in OSSRH directly. Users will simply get your components directly via the Central Repository, but for committers and other collaborators on your project this list might be useful.Repository URL for snapshot deployment and download access https://s01.oss.sonatype.org/content/repositories/snapshots/Repository URL for release deployment, no download access!https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ Note: As of February 2021, all new projects began being provisioned on https://s01.oss.sonatype.org/. If your project is not provisioned on https://s01.oss.sonatype.org/, you will want to use the legacy host https://oss.sonatype.org/.

    心得

    本教程只能给你一些在流程、注意事项的引导,由于文档的变化与发展,任何时候,我们应该将官方文档作为首要参考,其他的博客内容包含本博客应该是次要参考。sonatype回复的指导流程已经非常详细了。认真理解表达的内容也是避坑的途径。

    总结

    以上是生活随笔为你收集整理的从零开始上传开源项目到Maven中央仓库的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。