如何将自己的项目发布到Maven中央仓库

发布时间 2023-05-26 20:12:49作者: strongmore

注册Sonatype的账户

注册地址,Sonatype通过JIRA来管理OSSRH仓库。JIRA是一个项目管理服务,类似于国内的Teambition。
密码校验比较严格,最少12位,包含小写字母,大写字母,数字,还必须包含特殊字符如&, %。

项目的发布申请

创建一个issue,类型为New Project

创建成功之后就等待官方审核。

审核过程

主要就是通过issue的评论来继续进行的,主要是验证groupId对应的域名是否是我们拥有的。我们使用第二种方式,使用github的域名,对应的groupId为io.github.strongmore168,strongmore168为我的github账号,在github账号下创建一个空项目,名称为OSSRH-89128,就是issue的ID。然后在评论区回复一下。官方回复还是很快的。

安装并配置GPG

发布到Maven仓库中的所有文件都要使用GPG签名,以保障完整性。因此,我们需要在本地安装并配置GPG。
下载地址

命令提示符输入gpg --version,有输出表示安装成功。

生成密钥对

一个私钥,一个公钥,使用私钥对文件进行签名,将公钥分发到公钥服务器供其他用户下载,其他用户就可以使用公钥对签名进行验证。

gpg --gen-key

过程中需要输入用户名,邮箱和密码。密码后续要用。

将公钥信息上传到公共的公钥服务器

gpg --list-keys #查询本地公钥
gpg --list-secret-keys #查询本地私钥
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 773A9FB255ACEF00A42B8899BBAC244C65EC33AF #上传到服务器,773A9FB255ACEF00A42B8899BBAC244C65EC33AF为上面查询到的

配置Maven的setting.xml

setting.xml为Maven的全局配置文件,路径为C:\Users\xxx\.m2,需要注册Sonatype的账户时配置的Username和Password添加到servers标签中,这样才能将jar包部署到Sonatype OSSRH仓库:

<server>
  <id>sonatype-nexus-snapshots</id>
  <username>Sonatype账号</username>
  <password>Sonatype密码</password>
</server>

如果密码包含特殊字符,要转义,如&->&amp;

配置项目的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 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>io.github.strongmore168</groupId>
    <artifactId>mylombok</artifactId>
    <version>1.0-RELEASE</version>

    <name>Project MyLombok</name>
    <url>https://github.com/strongmore168/mylombok</url>
    <description>the simple lombok</description>

    <licenses>
        <license>
            <name>BSD 3-Clause</name>
            <url>https://spdx.org/licenses/BSD-3-Clause.html</url>
        </license>
    </licenses>
    <scm>
        <connection>https://github.com/strongmore168/mylombok.git</connection>
        <url>https://github.com/strongmore168/mylombok</url>
    </scm>
    <developers>
        <developer>
            <name>strongmore</name>
            <email>strongmore4@gmail.com</email>
            <roles>
                <role>Developer</role>
            </roles>
            <timezone>+8</timezone>
        </developer>
    </developers>

    <dependencies>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.8.0</version>
            <scope>system</scope>
            <systemPath>C:\\Users\\xxx\\Downloads\\tools-1.8.0.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>META-INF/**/*</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>process-META</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources/</directory>
                                    <includes>
                                        <include>**/*</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- Source -->
                    <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>
                    <!-- Javadoc -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>3.1.0</version>
                        <configuration>
                            <show>private</show>
                            <nohelp>true</nohelp>
                            <charset>UTF-8</charset>
                            <encoding>UTF-8</encoding>
                            <docencoding>UTF-8</docencoding>
                            <additionalparam>-Xdoclint:none</additionalparam>
                            <javadocExecutable>C:/D-myfiles/java/jdk/java-se-8u42-ri/bin/javadoc.exe</javadocExecutable>
                            <!-- TODO 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
                        </configuration>
                        <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.6</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--Release -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>2.5.1</version>
                    </plugin>
                </plugins>
            </build>

            <distributionManagement>
                <snapshotRepository>
                    <id>sonatype-nexus-snapshots</id>
                    <name>Sonatype Nexus Snapshots</name>
                    <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>sonatype-nexus-snapshots</id>
                    <name>Nexus Release Repository</name>
                    <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles>
</project>

其中snapshotRepository便是在setting.xml中定义的server的id。远程仓库地址https://s01.oss.sonatype.org来自于issue中官方回复的评论。

发布jar包

mvn clean deploy -P release

执行上述命令,打包并上传,release便是上面配置的profile元素的id。在执行的过程中需要输入上面设置的key的密码。

查看发布jar包

查看地址

可以看到close失败的原因。注意,这个网站很卡,不要使用VPN,使用了更卡。尝试了很多次终于close成功了。close成功之后再点击release发布。发布成功之后,邮件中(注册sonatype时填写的)会收到issue变化的信息,提示同步已经被激活,正常10分钟就可以更新同步。

image

实践过程中发现十分钟之内已经成功同步到https://repo1.maven.org/的中央仓库当中。

image

此时在项目中就可以引入此依赖了,至此项目发布maven中央仓库成功。

<dependency>
  <groupId>io.github.strongmore168</groupId>
  <artifactId>mylombok</artifactId>
  <version>1.0-RELEASE</version>
</dependency>

deploy过程中遇到的问题

An API incompatibility was encountered while executing org.apache.maven.plugins:maven-javadoc-plugin:2.9.1:jar: java.lang.ExceptionInInitializerError: null

maven-javadoc-plugin版本太低,改成3.1.0版本就好了

参考

如何发布自己的项目到Maven中央仓库?
GPG(GnuPG)的安装和使用