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先备份私钥,然后另一台电脑再导入即可使用。

参考

相关推荐
QC班长3 小时前
Maven公司私库配置踩坑点
java·服务器·maven·intellij-idea
SiYuanFeng6 小时前
新手学Git:如何把本地 Git 项目上传到 GitHub
git·github
桌面运维家11 小时前
中小学IDV云桌面vDisk挂载部署方案
github
MXN_小南学前端13 小时前
Vue3 + Spring Boot 工单系统实战:用户反馈和客服处理的完整闭环(提供gitHub仓库地址)
前端·javascript·spring boot·后端·开源·github
lentoo-13 小时前
GitHub 暂停了 Copilot 付费注册
github·copilot
一颗青果14 小时前
Cookie 与 Session 超详细讲解
服务器·前端·github
skywalk816315 小时前
为aicomm项目添加CI/CD 配置 让github帮我们自动测试
ci/cd·github
Yunzenn15 小时前
零基础复现Claude Code(五):终端篇——赋予执行命令的超能力
面试·github
Yunzenn15 小时前
零基础复现Claude Code(四):双手篇——赋予读写文件的能力
开源·github
CoderJia程序员甲16 小时前
GitHub 热榜项目 - 日榜(2026-04-23)
人工智能·ai·大模型·github·ai教程