发布自己的jar包到maven的中央仓库,让别人也可以直接导入坐标使用

239 阅读4分钟

发布jar包到maven中央仓库

准备账号

创建账号

central.sonatype.com/

右上角的“登录”链接,支持通过 Google 或 GitHub 进行社交登录。也可以选择注册自己的用户名和密码。我这里选择github进行登录。

image.png

image.png

登录完之后是这样的:

image.png

新建命名空间

如果你使用 GitHub 登录的,该步骤可跳过。

image.png

image.png

image.png 如果你的代码存在 GitHub 上,你就需要把命名空间设置为io.github.myusername。这里的myusername是你的github地址,例如我的的是https://github.com/gwyg所以我的命名空间是:io.github.gwyg

其他的仓库可以按照下面的要求来创建: GitHub io.github.myusername GitLab io.gitlab.myusername Gitee io.gitee.myusername Bitbucket io.bitbucket.myusername

验证命名空间

创建之后需要验证命名空间来依次证明这个命名空间是你独有的,需要在对应的地址创建一个开源的仓库,名称就是你点击verify namespace的名称,例如下面这样:

我这里是创建了gitee仓库的一个命名空间

image.png

image.png

创建push的账号和密码

这一步抛弃了原来固定的username和password,选择了一个随机的username和password,这个username和password用来push你的jar包到中央仓库里面去,所以一定要保存好,以后都不会显示了,只有在创建成功的时候才会显示一次。

image.png

image.png

image.png

image.png 把他粘贴到maven的setting.xml 文件里面

这个${server}可以写成你自定义的id,待会会用到,记住。

<server>
        <id>${server}</id>
        <username>w2JL882q</username>
        <password>fwCP62XwRq3KiKFIHttKQK2RMRuDgxsoWenUBADyC5PP</password>
</server>

image.png

GPG准备

下载GPG

GPG 用于创建asc文件用于验证你的文件的正确性和安全性,我们直接去官网下载:

gnupg.org/download/in…

最新版本好像要收费,低一点的版本就不需要了,安装就是傻瓜式安装,最好自己指定目录,方便后面的操作

image.png

安装过程比较简单,这里就不单独截图来教学了,安装的时候可以把这些开发工具放在同一个地方,方便管理。

生成秘钥

image.png

安装好之后我有两个目录,进入 GnuPG\bin 这个目录下的cmd

输入gpg --gen-key回车

依次输入名称,邮箱地址,名称输入你命名空间的名称(我这里使用的是github上的那个)

然后在弹出的窗口设置一个密码,用来保护密钥对。

image.png

发布秘钥

上面 067FBBE0CB587465FD81AF9C580E420B49E48996 就是密钥id

image.png

gpg --keyserver keyserver.ubuntu.com --send-keys 067FBBE0CB587465FD81AF9C580E420B49E48996

这里发布到keyserver.ubuntu.com服务器上,这样中央仓库也有你的密钥,所以它才能验证你的身份

As SKS Keyserver Network is being deprecated we recommend the use an specific GPG keyserver. Current GPG Keyservers supported by Central Servers are: keyserver.ubuntu.com keys.openpgp.org pgp.mit.edu

如果发布失败开源用上面的其他两个地址

验证秘钥

验证秘钥是否发布成功:

gpg --keyserver keyserver.ubuntu.com --recv-keys 067FBBE0CB587465FD81AF9C580E420B49E48996

出现下面内容则说明发布成功

image.png

发布jar包

编辑pom文件

dependencies里面的内容是根据你的项目里面的实际情况写,其他的都都安装实际情况写。

<?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.gwyg</groupId>
    <artifactId>hello-maven</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>hello-maven</name>
    <description>hello-maven</description>
    <url>https://github.com/Gwyg/hello-maven</url>

    <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>gwyg</name>
            <email>1773511271@qq.com</email>
            <!-- organization及相关信息,可选 -->
<!--            <organization>组织名称</organization>-->
<!--            <organizationUrl>组织官网</organizationUrl>-->
            <url>https://github.com/Gwyg/hello-maven</url>
            <timezone>+8</timezone>
        </developer>
    </developers>


  <!--  <scm>
        <connection>https://github.com/Gwyg/hello-maven.git</connection>
        <developerConnection>scm:git:ssh://git@github.com:Gwyg/hello-maven.git</developerConnection>
        <url>https://github.com/Gwyg/hello-maven</url>
    </scm>-->
    <scm>
        <!-- 项目URL -->
        <url>https://github.com/Gwyg/hello-maven</url>
        <!-- 项目URL.git -->
        <connection>scm:git:https://github.com/Gwyg/hello-maven.git</connection>
        <!-- 项目URL.git -->
        <developerConnection>scm:git:https://github.com/Gwyg/hello-maven.git</developerConnection>
    </scm>



    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 服务id 也就是setting.xml中的servers.server.id -->
        <serverId>gwyg</serverId>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <build>
        <plugins>
            <!-- 编译插件,设置源码以及编译的jdk版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <!-- 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>2.9.1</version>
                <configuration>
                    <additionalparam>-Xdoclint:none</additionalparam>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Javadoc -->
            <!-- Gpg Signature -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 新方式的配置,将组件部署到OSSRH并将其发布到Central Repository-->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <!-- 服务id 也就是setting.xml中的servers.server.id -->
                    <publishingServerId>gwyg</publishingServerId>
                    <tokenAuth>true</tokenAuth>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <snapshotRepository>
            <id>${serverId}</id>
            <url>https://central.sonatype.com/</url>
        </snapshotRepository>
    </distributionManagement>

</project>

打包上传

mvn 先clean 后deploy

不要有中文路径

这个时候会让你输入你在开头设置的密码,输入成功开始打包上传。

image.png

打包完成

image.png

发布jar包

打包成功之后Deployments模块里面也有对应的步骤,刚刚我们在idea控制台看到的id是:1ac77d8b-5abe-4458-ba2a-9d7484601370也就对应这个页面里面的Deployment ID

这个时候我们点击Publish意思就是将我们的jar包发布了

发布之后需要等待通过。

image.png

image.png

几分钟之后就能通过

搜索我们的jar包

搜索到就是成功了

image.png

测试jar包

在idea中就可以导入我们自己发布的jar包并使用了

<dependency>
    <groupId>io.github.gwyg</groupId>
    <artifactId>hello-maven</artifactId>
    <version>0.0.2</version>
</dependency>

导入我们上传的依赖

image.png

成功运行,到此就是完整的上传流程了

image.png