侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

如何推送个人项目至Maven中央仓库

2023-12-17 星期日 / 0 评论 / 0 点赞 / 26 阅读 / 10976 字

1. 开篇前段时间使用Gitee仓库搭建了一个Maven私有仓库,将一些开源包放到上面去,感觉使用起来还是不太方便,最近就折腾将这些包提交到Maven的中央仓库中。项目第一次提交Maven还是挺麻烦的

1. 开篇

前段时间使用Gitee仓库搭建了一个Maven私有仓库,将一些开源包放到上面去,感觉使用起来还是不太方便,最近就折腾将这些包提交到Maven的中央仓库中。项目第一次提交Maven还是挺麻烦的,所以写个文章Mark一下。

2. 操作步骤

2.1. 提交申请

注册一个sonatype.org帐号,登陆并提交一个issue,没错,就是提交一个issue,具体可参考如下:

其中:

  • 问题类型:一定要选New Project
  • 概要:填项目名称;
  • 描述:可不填;
  • Group Id:填写你的项目ID,需注意的是这个Group Id对应的域名需要是你持有的,后续会验证这项;
  • Project URL:就是项目主页地址;
  • SCM url:就是Git仓库地址;
  • username:填写你在sonatype JIRA的用户名,也可以填写别人的,这个用户名用于推送项目,所以要注意。
  • Already Synced to Central:是指你是否准备好提交到中央仓库了,应该是给官方人员判断是否需要优先处理。

2.2. 配置项目

2.2.1. 配置settings.xml

在Maven的settings.xml文件中增加<server>配置,配置你sonatype的账号密码,参考如下:

<servers>        <server>            <id>sonatype-nexus-snapshots</id>            <username>demo</username>            <password>******</password>        </server>        <server>            <id>sonatype-nexus-staging</id>            <username>demo</username>            <password>******</password>        </server>            </servers>

2.2.2. 配置pom.xml

  • 增加licensescmdeveloper信息。
    <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>        </license>    </licenses>    <scm>        <tag>master</tag>        <url>[email protected]:centy/xxl-job-spring-boot-starter.git</url>        <connection>scm:git:[email protected]:centy/xxl-job-spring-boot-starter.git</connection>        <developerConnection>scm:git:[email protected]:centy/xxl-job-spring-boot-starter.git</developerConnection>    </scm>    <developers>        <developer>            <name>centychen</name>            <email>[email protected]</email>        </developer>    </developers>
  • 增加release的profile配置,注意:distributionManagement.snapshotRepositorydistributionManagement.repository的id需与settings.xml中对应的server记录ID一致;distributionManagement的url根据官方反馈的url修改。
     <profiles>        <profile>            <id>release</id>            <build>                <plugins>                    <!-- Source -->                    <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>                    <!-- Javadoc -->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-javadoc-plugin</artifactId>                        <version>3.1.0</version>                        <executions>                            <execution>                                <phase>package</phase>                                <goals>                                    <goal>jar</goal>                                </goals>                            </execution>                        </executions>                    </plugin>                    <!-- GPG -->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-gpg-plugin</artifactId>                        <version>1.5</version>                        <executions>                            <execution>                                <phase>verify</phase>                                <goals>                                    <goal>sign</goal>                                </goals>                            </execution>                        </executions>                    </plugin>                    <!--Compiler-->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-compiler-plugin</artifactId>                        <version>3.8.0</version>                        <configuration>                            <source>1.8</source>                            <target>1.8</target>                            <fork>true</fork>                            <verbose>true</verbose>                            <encoding>UTF-8</encoding>                            <showWarnings>false</showWarnings>                        </configuration>                    </plugin>                    <!--Release-->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-release-plugin</artifactId>                        <version>2.5.3</version>                    </plugin>                </plugins>            </build>            <distributionManagement>                <snapshotRepository>                    <id>sonatype-nexus-snapshots</id>                    <name>Sonatype Nexus Snapshots</name>                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>                </snapshotRepository>                <repository>                    <id>sonatype-nexus-staging</id>                    <name>Nexus Release Repository</name>                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>                </repository>            </distributionManagement>        </profile>    </profiles>

2.3. 安装及配置PGP

  • Mac安装PGP,我是通过brew安装的,使用命令brew install gpg执行安装;
  • 使用命令gpg --gen-key生成公私钥,按照提示信息一步步操作,需要记住加密使用的Passphrase,下面步骤需使用;
  • 上传公钥至公钥服务器,可通过gpg --list-keys查看公钥ID,通过一下命令上传:
gpg --send-keys [公钥ID] --keyserver hkp://keyserver.ubuntu.com:11371

2.4. 构建并部署

2.4.1. 构建SNAPSHOT版本

  • 版本号修改为***-SNAPSHOT格式,如1.0.0-SNAPSHOT
  • 执行以下命令开始构建:
mvn clean deploy -P release -Dmaven.test.skip=true 
  • Deploy的时候会弹出一个输入Passphrase的页面,输入刚才生成pgp公私钥使用的密码。

  • 构建完成后,在https://oss.sonatype.org/content/repositories/snapshots中应该可以找到刚刚提交的snapshot版本。

2.4.2. 构建RELEASE版本

  • 版本号修改为***-RELEASE或者无后缀格式,如1.0.0-RELEASE1.0.0
  • 构建并Deploy
mvn clean deploy -P release -Dmaven.test.skip=true
  • Deploy的时候会弹出一个输入Passphrase的页面,输入刚才生成pgp公私钥使用的密码。

  • 登陆https://oss.sonatype.org/,点击左侧Staging Repositories,输入你的group id查找,可看到deploy记录:

  • 选中Deploy记录点击CloseConfirm,刷新后会发现记录状态已经变成Closed

  • 再选中记录点击ReleaseConfirm完成发布,发布完成后需要等待中央仓库同步,我是等了1个多小时才能在中央仓库搜索出来。

3. 可能踩到的坑

3.1. gpg: signing failed: Inappropriate ioctl for device

使用mvn clean deploy命令构建时,可能会报gpg: signing failed: Inappropriate ioctl for device,是因为无法弹出Passphrase页面,需要在系统环境变量中增加export GPG_TTY=$(tty)

3.2. Access denied to staging repository

如果Deploy的时候报 Access denied to staging repository...等错误,恭喜你,你的帐号权限有问题,需要再提一个issue处理该问题,可参考我提的issue。

3.3. 在Staging Repositories中执行Close操作不成功,状态依然是Open。

应该是执行close操作的数据校验有问题,比如pom.xml信息缺失等,我第一次提交的时候就没有在pom.xml中配置project name,校验就没有通过。留意页面下方Activity中的错误信息即可。

广告 广告

评论区