github项目的jar包上传到maven中央仓库

目录

前言

  • 前面写过一篇把Jar包上传到GitHub仓库,有个弊端就是必须先声明repository地址才能把jar包拉下来,这次直接把jar包上传到中央仓库,就没这个烦恼了,直接声明pom坐标即可。

准备

中央仓库

  • 打开中央仓库 https://central.sonatype.com/ 注册帐号,建议直接使用GitHub登陆,Namespace会自动验证好。

  • pom.xml的groupId就写这个已验证的Namespace

  • 创建token

  • 配置到maven settings.xml文件中

xml 复制代码
 ...省略...
  <servers>
    <server>
      <id>central</id>
      <username>生成的Token用户名</username>
      <password>生成的Token密码</password>
    </server>
    ..省略...
  </servers>
 ...省略... 

生成 GPG 密钥

上传到中央仓库的二进制文件都必须经过签名,以证明它们没有被篡改

shell 复制代码
gpg --full-generate-key

密钥类型: RSA and RSA

密钥长度: 4096

其余按自己情况填写

  • 上传公钥
shell 复制代码
#先查看 ID
gpg --list-keys --keyid-format LONG
  • 上传到公钥服务器( Key ID是 rsa4096/ 后面16个字符)
shell 复制代码
gpg --keyserver keyserver.ubuntu.com --send-keys 你的KeyID

项目配置

xml 复制代码
  <groupId>io.github.1030907690</groupId>
    <artifactId>flink-sql-connector-elasticsearch8</artifactId>
    <version>${revision}</version>
    <name>${project.artifactId}</name>
    <url>https://github.com/1030907690/flink-sql-connector-elasticsearch8</url>
    <description>flink-sql-connector-elasticsearch8</description>


    <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>Zhou Zhongqing</name>
            <email>g1030907690@gmail.com</email>
            <organization>Zhou Zhongqing</organization>
            <organizationUrl>https://github.com/1030907690</organizationUrl>
        </developer>
    </developers>

   <scm>
        <connection>scm:git:git://github.com/1030907690/flink-sql-connector-elasticsearch8.git</connection>
        <developerConnection>scm:git:ssh://github.com:1030907690/flink-sql-connector-elasticsearch8.git</developerConnection>
        <url>https://github.com/1030907690/flink-sql-connector-elasticsearch8/tree/main</url>
    </scm>

    <properties>
        ...省略...
        <java.version>17</java.version>
        <revision>1.0.3</revision>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
<dependencies>
...省略依赖信息...
</dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>org.apache.flink:*</exclude>
                                    <exclude>org.slf4j:*</exclude>
                                    <exclude>log4j:*</exclude>
                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--     生成源javadocs文件       -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <!--     生成源文件       -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.3.0</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>3.6.3</version>
                <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>3.1.0</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--    Maven插件发布    -->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>central</publishingServerId>
                    <tokenAuth>true</tokenAuth>
                </configuration>
            </plugin>
            <!-- 替换变量 ${revision} -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>resolveCiFriendliesOnly</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>

        </plugins>
    </build>
  • 注意:每个类要有注释,否则执行deploy时会报错:

上传jar包

  • 我将版本号改为1.0.4
  • 点击deploy
  • 中途会弹窗输入密钥密码。
  • 上传成功后在中央仓库后台就能看到如下信息。
  • 点击Publish按钮后,稍侯再搜索便能查到此版本的pom坐标了。

其他

  • 如果你有两台电脑,都想使用同一个密钥,可以使用Kleopatra先备份私钥,然后另一台电脑再导入即可使用。

参考

相关推荐
My的梦想已实现1 小时前
关于JAVA Springboot集成支付后打包JAR之后报安全错误的处理
java·spring boot·jar
CoovallyAIHub2 小时前
VisionClaw:智能眼镜 + Gemini + Agent,看一眼就能帮你搜、帮你发、帮你做
算法·架构·github
CoovallyAIHub3 小时前
低空安全刚需!西工大UAV-DETR反无人机小目标检测,参数减少40%,mAP50:95提升6.6个百分点
算法·架构·github
橙露3 小时前
Shell 脚本实战:自动化备份、监控、告警脚本模板
运维·自动化·github
FollowHeart4 小时前
自建私有日记本:MyDiary —— 属于你的 NAS 极简写作空间
vue.js·github
阿里嘎多学长4 小时前
2026-03-31 GitHub 热点项目精选
开发语言·程序员·github·代码托管
逛逛GitHub5 小时前
把 Claude Code 变成你的桌面宠物,这个开源项目好有创意啊。
github
TlYf NTLE5 小时前
Spring Boot spring-boot-maven-plugin 参数配置详解
spring boot·后端·maven
Goodwin5 小时前
TypeScript 成 AI 应用层标配?GitHub Trending 告诉你2026前端往哪走
前端·人工智能·github
七夜zippoe6 小时前
OpenClaw 多渠道统一管理:构建全平台智能消息中枢
开发语言·microsoft·github·多渠道·openclaw