wangjie_fourth

may the force be with you

0%

发布自己的Jar包到Maven中央仓库

出于以下目的,我开始尝试建立自己的jar包:

  • 为了以后解决常见问题,把对应问题的解决代码保存起来,后面可以快速完成
  • 针对网上一些问题,而我工作上没有用到的,可以自己先写一份代码保存起来,以后再遇到可以直接使用

接下来就是解决步骤:

  • 本地手动发布代码至maven中央仓库
  • 与GitHub仓库的事件绑定,自动发布

本地手动发布

1、创建账号
登陆网站,创建一个网站,推荐使用GitHub账号的方式创建。

2、创建Namespace
这个为了验证你有创建对应groupId的权限。这里推荐你创建一个组织,然后就可以申请名为io.github.<organization-name>Namespace

3、GPG
这个看文档似乎为了生成的jar包、源码等文件,生成一个签名,具体用途不清楚,我猜可能是为了校验吧。按照文档的步骤是:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装gpg
brew install gpg
# 验证是否安装完成
gpg --version
# 生成key信息,按照要求输入就行,记住输入的密码
gpg --gen-key
# 其中输出的结果有如下信息,要记住第二行的CA开头的字符串
# pub rsa3072 2021-06-23 [SC] [expires: 2023-06-23]
# CA925CD6C9E8D064FF05B4728190C4130ABA0F98
# uid Central Repo Test <central@example.com>
# sub rsa3072 2021-06-23 [E] [expires: 2023-06-23]
# 发布你的公钥
gpg --keyserver keyserver.ubuntu.com --send-keys CA925CD6C9E8D064FF05B4728190C4130ABA0F98

4、项目的pom文件修改
(1)项目的坐标要明确

1
2
3
4
<groupId>org.github.fourth</groupId>
<artifactId>iywe-java</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>

(2)提供一些基础信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<name>iywe-java</name>
<description>improve your work efficiency</description>
<url>https://github.com/iywes/iywe-java</url>

<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/iywes/iywe-java/issues</url>
</issueManagement>

<inceptionYear>2024</inceptionYear>

<licenses>
<license>
<name>MIT License</name>
<url>https://github.com/iywes/iywe-java/blob/main/LICENSE</url>
<distribution>repo</distribution>
</license>
</licenses>

<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>releases</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<scm>
<connection>scm:git:https://github.com/iywes/iywe-java.git</connection>
<developerConnection>scm:git:git@github.com:iywes/iywe-java.git</developerConnection>
<url>https://github.com/iywes/iywe-java</url>
</scm>

<developers>
<developer>
<id>wangjie-fourth</id>
<name>wang jie</name>
<email>wangjie_fourth@163.com</email>
<roles>
<role>owner</role>
<role>developer</role>
</roles>
</developer>
</developers>

(3)发布的插件信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<build>
<plugins>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<inherited>true</inherited>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeResources>true</excludeResources>
<useDefaultExcludes>true</useDefaultExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<inherited>true</inherited>
<executions>
<execution>
<id>bundle-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<maxmemory>1024</maxmemory>
<encoding>UTF-8</encoding>
<show>protected</show>
<notree>true</notree>
<!-- Avoid running into Java 8's very restrictive doclint issues -->
<failOnError>false</failOnError>
<doclint>none</doclint>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<passphraseServerId>ossez</passphraseServerId>
</configuration>
</plugin>
</plugins>
</build>

(4)修改mavensetting.xml文件

1

5、发布

1
mvn deploy

6、到官网确认发布
登陆官网,找到你刚才发布的信息,点击Publish就可以了。

GitHub仓库自动发布

上面这种方式可以在本地就可以发布了,但到GitHub仓库就有问题


参考资料: