发布开源组件到Maven中央仓库

一、登录Sonatype

https://central.sonatype.com/Sonatype官方推出的开源组件检索与管理平台,依托 Sonatype 近 20 年运营 Maven Central(Java 生态核心的开源仓库)的经验打造,核心服务于软件开发者和企业的开源供应链管理,简单来说,这是一个能查找、核验开源组件,同时了解开源供应链现状的核心平台。

注意:这个最好用github账号登录,github账号会自动生成命名空间,后续的代码发布都得用这个命名空间

二、 GPG密钥工具准备

1、GPG密钥工具下载安装

打开 GunPG Download ,找到 「GnuPG binary releases」项,根据电脑的环境下载安装合适的GPG工具。下面以Windwos 10/11为例。

2、图形界面生成密钥

名称是Sonatype 命名空间名称,邮件填自己的就行,打上对勾,其他默认,然后点击确定

由于上面勾选了,所以这里的需要自己设置一个密码,输入两次;后面的maven的配置文件中需要用到,用于上传验证

设置密码后页面会出现一条记录,右键导出密钥

3、密钥上传到公钥服务器

keys.openpgp.org  这是一个公共密钥的服务

这个页面可以查询上传的密钥,根据生成的密钥 id 查询

点击此页面的 upload,可跳转密钥上传页面,将上面导出的公钥上传

4、maven setttings.xml 配置

按上图配置,签名密码就是上面生成密钥时配置的密码

三、项目pom.xml配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 项目基本信息:中央仓库发布必填 -->
    <groupId>io.github.wangchen23</groupId> <!-- 组ID,通常为反向域名+用户名/项目名 -->
    <artifactId>mapperx</artifactId> <!-- 项目ID,唯一标识 -->
    <version>${revision}</version> <!-- 版本号,通过revision属性统一管理 -->
    <packaging>pom</packaging> <!-- 打包类型为pom,作为父模块 -->
    <name>MapperX</name> <!-- 项目名称 -->
    <description>A MyBatis extension for dynamic mapping and auto-fill features</description> <!-- 项目描述 -->
    <url>https://github.com/wangchen23/mapperx</url> <!-- 项目主页地址 -->

    <!-- 许可证信息:中央仓库发布必填,声明开源协议 -->
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name> <!-- 协议名称 -->
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url> <!-- 协议文本地址 -->
            <distribution>repo</distribution> <!-- 分发方式:随仓库发布 -->
        </license>
    </licenses>

    <!-- 开发人员信息:中央仓库发布必填,声明项目开发者 -->
    <developers>
        <developer>
            <id>wangchen23</id> <!-- 开发者ID(通常为GitHub用户名) -->
            <name>wangchen23</name> <!-- 开发者名称 -->
            <email>1374103097@qq.com</email> <!-- 开发者邮箱 -->
            <organizationUrl>https://github.com/wangchen23</organizationUrl> <!-- 开发者组织/个人主页 -->
        </developer>
    </developers>

    <!-- 源码管理信息:中央仓库发布必填,声明代码仓库地址 -->
    <scm>
        <connection>scm:git:https://github.com/wangchen23/mapperx.git</connection> <!-- 只读仓库地址 -->
        <developerConnection>scm:git:https://github.com/wangchen23/mapperx.git</developerConnection> <!-- 可写仓库地址 -->
        <url>https://github.com/wangchen23/mapperx</url> <!-- 仓库网页地址 -->
        <tag>v${project.version}</tag> <!-- 版本标签,与项目版本一致 -->
    </scm>

    <!-- 子模块声明:当前父模块包含的子项目 -->
    <modules>
        <module>mapperx-core</module>
        <module>mapperx-spring-boot-starter</module>
    </modules>

    <!-- 全局属性:统一管理版本号、编码等配置 -->
    <properties>
        <revision>1.1.0</revision> <!-- 项目统一版本号 -->
        <java.version>1.8</java.version> <!-- JDK版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 源码编码 -->
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 报告输出编码 -->
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version> <!-- Spring Boot版本 -->
        <mybatis.version>3.5.6</mybatis.version> <!-- MyBatis版本 -->
        <mybatis-spring-boot.version>2.1.4</mybatis-spring-boot.version> <!-- MyBatis Spring Boot Starter版本 -->
    </properties>

    <!-- 依赖管理:统一管理子模块的依赖版本,避免版本冲突 -->
    <dependencyManagement>
        <dependencies>
            <!-- 导入Spring Boot依赖管理 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- 声明MyBatis版本 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- 声明MyBatis Spring Boot Starter版本 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot.version}</version>
            </dependency>
            <!-- 声明自身核心模块版本 -->
            <dependency>
                <groupId>io.github.wangchen23</groupId>
                <artifactId>mapperx-core</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!-- Flatten 插件:规范化POM文件,移除中央仓库不允许的私有配置(如父POM引用),满足发布规范 -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <updatePomFile>true</updatePomFile> <!-- 更新原始POM文件为扁平化版本 -->
                    <flattenMode>oss</flattenMode> <!-- 扁平化模式:适配开源软件发布 -->
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase> <!-- 绑定到资源处理阶段执行 -->
                        <goals>
                            <goal>flatten</goal> <!-- 执行扁平化目标 -->
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase> <!-- 绑定到清理阶段执行 -->
                        <goals>
                            <goal>clean</goal> <!-- 清理扁平化生成的文件 -->
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Central Publishing Plugin:Sonatype官方发布插件,替代传统deploy,适配新版中央仓库发布流程 -->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.9.0</version>
                <extensions>true</extensions> <!-- 启用扩展模式,参与Maven生命周期 -->
                <configuration>
                    <!-- 对应Maven settings.xml中配置的server id(需提前配置中央仓库认证信息) -->
                    <publishingServerId>central</publishingServerId>
                    <autoPublish>true</autoPublish> <!-- 发布后自动完成流程,无需手动确认 -->
                </configuration>
            </plugin>

            <!-- 禁用传统deploy插件:避免与central-publishing-maven-plugin冲突 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip> <!-- 跳过传统deploy执行 -->
                </configuration>
            </plugin>
        </plugins>

        <pluginManagement>
            <!-- 插件管理:统一管理子模块继承的插件版本和配置,仅声明不执行 -->
            <plugins>
                <!-- 源码包生成插件:必须配置,用于生成源码 jar 包并附加到构建产物中,满足中央仓库发布要求 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.4.0</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <!-- 绑定到构建生命周期,生成源码 jar 包(no-fork 模式避免创建新进程,提升构建效率) -->
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- Javadoc 生成插件:必须配置,用于生成 API 文档 jar 包,满足中央仓库发布的文档要求 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>3.12.0</version>
                    <configuration>
                        <!-- 统一编码格式,避免中文乱码 -->
                        <encoding>UTF-8</encoding>
                        <docencoding>UTF-8</docencoding>
                        <charset>UTF-8</charset>
                        <!-- 忽略 Javadoc 生成时的错误(如注释格式问题),避免构建中断 -->
                        <failOnError>false</failOnError>
                    </configuration>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <!-- 绑定到构建生命周期,生成 Javadoc jar 包并附加到构建产物 -->
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- GPG 签名插件:必须配置,用于对所有发布产物(jar、pom、源码包、Javadoc 包)进行 GPG 签名,满足中央仓库的安全验证要求 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>3.2.8</version>
                    <executions>
                        <execution>
                            <id>sign-artifacts</id>
                            <!-- 绑定到 verify 阶段执行签名,确保产物验证通过后再签名 -->
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- 编译插件:统一配置项目编译规则,确保子模块编译环境一致 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.13.0</version>
                    <configuration>
                        <!-- 指定编译的 JDK 源码版本和目标版本(与项目指定的 Java 1.8 保持一致) -->
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <!-- 编译时使用 UTF-8 编码,避免中文注释/字符编译乱码 -->
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

若有子pom需注意:name、description和三个插件也是必要的

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>io.github.wangchen23</groupId>
        <artifactId>mapperx</artifactId>
        <version>${revision}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>mapperx-core</artifactId>
    <packaging>jar</packaging>

    <name>MapperX Core</name>
    <description>Core module of MapperX, providing dynamic mapping and auto-fill features</description>

    <dependencies>
        <!-- MyBatis核心依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

四、上传部署

看到这个就说明已经成功上传至maven中央仓库了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值