JAVA自定义SDK发布到maven central
1. 如果没有账号,去 https://central.sonatype.com/ 注册账号,记录账号和密码
2. 登录之后,点击右上角自己的账号,点击View Namespaces
3. 如果Namespace 为空,则需要创建一个命名空间
4. 我的代码托管在github上,我的用户是qiaoyk666,所以我创建的命名空间名称为:io.github.qiaoyk666
5. 命名空间创建完成后,需要验证,点击Verify Namespace
6. 如上图所示,需要在我的github账号下,创建一个名称为 08bgcxofpi 的公开Repository进行验证
7. 验证通过后(github很快,一分钟内),Namespace状态由Unverified变为Verified
8. 创建push到maven central的账号和密码,这一步抛弃了原来固定的username和password,选择了一个随机的username和password,这个username和password用来push你的jar包到中央仓库里面去,所以一定要保存好,以后都不会显示了,只有在创建成功的时候才会显示一次,如果重新生成,则之前的作废,需要修改成新的。
8.1 点击右上角的view account,然后点击 Generate User Token
8.2 点击 Ok
需要把图中红框部分的<server>xxx</server> 脚本内容复制粘贴到本地的maven settings.xml,我的maven地址为 E:\Program Files\maven-3.9.9\apache-maven-3.9.9\conf\settings.xml。
在 settings.xml的 <servers></servers> 标签内,添加
<server>
<id>central</id>
<username>JkD7GMU5</username>
<password>QROLWjVUPYvlNjz0do16W6sEArUSEBe8+p9/KM4operE</password>
</server>
注意:这里我把${server} 替换成了central,需要注意,在自己项目的pom.xml中,publishingServerId也应该配成central,这两个地方需要保持一致
GPG准备
第一步,下载GPG
GPG 用于创建asc文件用于验证你的文件的正确性和安全性,我们直接去官网下载:
https://gnupg.org/download/index.html
第二步,生成秘钥
去图中的路径的bin路径下,进入cmd命令行
生成秘钥
输入gpg --gen-key
依次输入名称,邮箱地址,名称输入你命名空间的名称,这里要注意,名称是命名空间的名称,比如我的是 io.github.qiaoyk666
这里会弹出一个让你输入密钥并二次确认的窗口,记住你的密码,后面发布代码的时候会用。
发布秘钥
上面 4CB3D9314CD5F1277582A11F4ADBA3851D627E38 就是你的密钥id
这里发布到keyserver.ubuntu.com
服务器上,这样中央仓库也有你的密钥,所以它才能验证你的身份,执行如下命令:
gpg --keyserver keyserver.ubuntu.com --send-keys 4CB3D9314CD5F1277582A11F4ADBA3851D627E38
如果发布失败,可以用下面的其他两个地址:
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
验证秘钥
验证秘钥是否发布成功:
出现下面内容则说明发布成功
E:\dev\gpg2\GnuPG\bin>gpg --keyserver keyserver.ubuntu.com --recv-keys 4CB3D9314CD5F1277582A11F4ADBA3851D627E38
gpg: key 4ADBA3851D627E38: "wangfugui-ma <masiyi163163@163.com>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1
发布jar包
第一步,编辑pom文件
在你的项目pom文件中加入以下的内容:
<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.qiaoyk666</groupId>
<artifactId>license-sdk-java</artifactId>
<version>0.0.2</version>
<name>license-sdk-java</name>
<description>license-sdk-java</description>
<url>https://github.com/qiaoyk666/license-sdk-java</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>
<id>qiaoyk</id>
<name>xxx</name>
<email>xxx@qq.com</email>
<roles>
<role>Project Manager</role>
<role>Architect</role>
</roles>
</developer>
</developers>
<!-- 项目仓库信息 -->
<scm>
<!--远程仓库git地址-->
<connection>https://github.com/qiaoyk666/license-sdk-java.git</connection>
<!--github仓库地址-->
<url>https://github.com/qiaoyk666/license-sdk-java</url>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- javadoc插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<failOnError>false</failOnError>
</configuration>
<version>3.10.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<configuration>
<executable>E:\gpg4win4.4.0\GnuPG\bin\gpg.exe</executable>
<keyname>qiaoyk</keyname>
</configuration>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</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>
<!-- central发布插件 -->
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<!-- 这里的publishingServerId是在settings.xml中配置的server认证信息 -->
<publishingServerId>central</publishingServerId>
<tokenAuth>true</tokenAuth>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.55</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
</dependencies>
</project>
第二步,打包上传
mvn 先clean 后deploy
这个时候会让你输入你在开头设置的密码(也就是GPG准备的第二步,生成密钥时,设置的密码),输入成功开始打包上传。
打包成功,如图:
回到 https://central.sonatype.com/publishing/deployments,找到自己提交的部署,点击发布:
发布后,过一段时间(大概十几分钟,二十几分钟),就可以在maven central repository 搜索到自己发布的sdk了,如下:
点进去,即可看到如何引用:
这样在别的java maven项目里,就可以在pom.xml配置使用了
<dependency> <groupId>io.github.qiaoyk666</groupId> <artifactId>license-sdk-java</artifactId> <version>0.0.2</version> </dependency>
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步