系列文章目录



前言

在软件开发的世界里,分享和协作是推动技术进步的重要力量。当你开发了一个优秀的SpringBoot插件,并希望将其分享给全世界的开发者使用时,将插件发布到Maven中央仓库(Central Repository)成为了一个理想的选择。本文将以发布一个SpringBoot插件到Maven中央仓库为例,详细介绍发布流程,并对比2024年之前和之后的发布区别。以macbook为例,介绍如何发布jar到中央仓库。

一、大概步骤

  • 注册Sonatype账号:访问Sonatype官网,注册一个账号。Sonatype是Maven中央仓库的运营者,你需要通过他们的平台来进行发布。你可以参考网上的文章来注册,因为博主的账号是在2024年前创建,目前只能通过旧的方式来发布,不过也是大同小异,原理差不多。

  • 签名插件:为了确保插件的安全性,你需要使用GPG密钥对插件进行签名。如果你还没有GPG密钥,请按照相关指南创建一个。以macbook为例,先下载安装GPG suite

  • 配置maven:按照规范配置settings.xml、pom.xml文件。

  • 发布到中央仓库

二、完整教程

1、https://central.sonatype.com/注册账号,网上教程很多,这里就不举例了,而且2024后就不需要通过jira的方式来提交,还是挺方便的。

参考文章:《2024年发布jar到国外maven中央仓库最新教程》

2、账号注册完毕,并且已经验证命名空间后,然后生成token,这个token很重要,简称密码A,后面要配置到maven的setting.xml文件。
在这里插入图片描述
由于我的是旧账号注册的,所以在https://s01.oss.sonatype.org/#welcome 生成了token。
在这里插入图片描述
将会得到下面这里一串东西。

username: o1VWi9O1
token: s4xxxxxxxxxxxxxxxTPH

用于下面setttings.xml文件
<server>
  <id>${server}</id>
  <username>o1VWi9O1</username>
  <password>s4xxxxxxxxxxxxTPH</password>
</server>

3、如果你的电脑是macbook的话,可以下载安装GPG suite,傻瓜式安装即可。windows的可以下载安装Gpg4win
在这里插入图片描述

4、配置秘钥,注意,姓名就是maven项目的groupId,也就是在官网验证的命名空间。
这里填写的密码要牢记,简称密码B,上传公钥和配置setting.xml的时候会用到。
在这里插入图片描述

5、上传公钥。
在这里插入图片描述
5、配置maven的setting.xml文件,主要增加server标签,用到了上面的密码A,其中ossrh是我们自定义的名字。

    <servers>
        <server>
            <id>ossrh</id>
            <username>o1VWi9O1</username>
            <password>s4FxxxxxxxxxxTPH</password>
        </server>
    </servers>

6、配置maven的setting.xml文件,修改profile标签,这里用到上面的密码B。其中id=ossrh要和上面server定义的保持一致。

        <profile>
            <id>ossrh</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <gpg.executable>gpg</gpg.executable>
                <gpg.passphrase>密码B</gpg.passphrase>
            </properties>
        </profile>

7、修改pom.xml文件,可以直接参考我的,只改动工程名字、描述、properties等不同的部分即可,因为大部分标签都是必须的,而且抽取了变量出来让你改了。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>io.github.vipjoey</groupId>
    <artifactId>multi-kafka-starter</artifactId>
    <version>2.2</version>
    <packaging>jar</packaging>


    <name>MultiKafkaStarter</name>
    <description>SpringBoot integrates multiple Kafka data sources with zero coding, supports any
        Kafka cluster, and has been encapsulated into a small module that integrates all Kafka
        configurations, allowing attention to return to the business itself.
    </description>


    <properties>
        <java.version>1.8</java.version>
        <projectUrl>https://github.com/VIPJoey/MultiKafkaStarter.git</projectUrl>
        <serverId>ossrh</serverId>
    </properties>

    <developers>
        <developer>
            <name>VIPJoey</name>
            <email>mmcorz@gmail.com</email>
            <url>${projectUrl}</url>
        </developer>
    </developers>

    <!--以下部分内容不需要修改,直接复制咱贴即可-->
    <url>${projectUrl}</url>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo,manual</distribution>
        </license>
    </licenses>
    <scm>
        <!-- 采用projectUrl变量代替这个值,方便给重复利用这个配置,也就是上面的标签替换一下值就行 -->
        <connection>${projectUrl}</connection>
        <developerConnection>${projectUrl}</developerConnection>
        <url>${projectUrl}</url>
    </scm>
    <distributionManagement>
        <snapshotRepository>
            <!--这个id和settings.xml中servers.server.id要相同,因为上传jar需要登录才有权限-->
            <id>${serverId}</id>
            <name>OSS Snapshots Repository</name>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
        </snapshotRepository>
        <repository>
            <!--这个id和settings.xml中servers.server.id要相同,因为上传jar需要登录才有权限-->
            <id>${serverId}</id>
            <name>OSS Staging Repository</name>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>


        <!-- kafka -->
        <!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.18.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>3.18.0</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <finalName>multi-kafka-starter</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>META-INF/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!-- 编译插件,设置源码以及编译的jdk版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <!--打包源码的插件-->
            <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>
            <!-- Javadoc 文档生成插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <!-- 忽略生成文档中的错误 -->
                    <additionalparam>-Xdoclint:none</additionalparam>
                    <aggregate>true</aggregate>
                    <charset>UTF-8</charset><!-- utf-8读取文件 -->
                    <encoding>UTF-8</encoding><!-- utf-8进行编码代码 -->
                    <docencoding>UTF-8</docencoding><!-- utf-8进行编码文档 -->
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </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>${serverId}</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

8、直接用idea上的发布功能即可。
在这里插入图片描述

三、参考文章

加我加群一起交流学习!更多干货下载、项目源码和大厂内推等着你

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐