发布jar到maven中央仓库

最近写了一个开源组件,想发布到maven仓库直接使用。
在此记录一下每一步的操作过程:

1、在这个网站注册一个账户

sonatype
账户名和密码需要记一下,不能忘了

2、点击新建,创建一个新的问题,依次填入项目的信息

下面是填入信息的参考:

项目:Community Support - Open Source Project Repository Hosting (OSSRH)
问题类型:New Project
Group Id:com.github.liuchengts
Project URL:https://github.com/liuchengts/spring-boot-auth-starter
SCM url:https://github.com/liuchengts/spring-boot-auth-starter.git
完成之后的图片是这样的

现在这个状态已经是解决了,刚新建好是待解决的状态。
我开始的 Group Id是乱写的,所以收到了一封 Group Id 确认的邮件
审核员提供了3种方式让你确认:

  • a、是否是自己的网站,如果是你需要按审核要求验证服务器
  • b、公共的代码托管仓库建议的命名方式 ,比如我是托管在GitHub上,Group Id 就为com.github.liuchengts
  • c、托管到sonatype推荐的仓库中,这项基本忽略,人家服务器在国外

3、我选择第二种,托管到GitHub的方式

sonatype上修改了 Group Idcom.github.liuchengts,并且在底下评论(其实评论按钮叫 "备注" 在最底下)了一句
Group Id Has been identified as com.github.liuchengts

4、等一会收到审核员回复,需要在托管的GitHub上按要求创建一个公开的项目,下面是原文

Please create a public repo called [https://github.com/liuchengts/OSSRH-56092](https://github.com/liuchengts/OSSRH-56092) to verify github account ownership.

If you do not own this github account, please read:
[http://central.sonatype.org/pages/choosing-your-coordinates.html](http://central.sonatype.org/pages/choosing-your-coordinates.html)

其实让你创建的公开项目名字就是你提问题的编号,项目是空的就行,去GitHub上创建了规定名字的开源项目后,去提的问题底下添加一条评论

Added, please verify [https://github.com/liuchengts/OSSRH-56092]

5、等一会收到审核员回复

com.github.liuchengts has been prepared, now user(s) liuchengts can:

*   Deploy snapshot artifacts into repository [https://oss.sonatype.org/content/repositories/snapshots](https://oss.sonatype.org/content/repositories/snapshots)
*   Deploy release artifacts into the staging repository [https://oss.sonatype.org/service/local/staging/deploy/maven2](https://oss.sonatype.org/service/local/staging/deploy/maven2)
*   Release staged artifacts into repository 'Releases'

please comment on this ticket when you promoted your first release, thanks

告诉了我项目的pom.xml的地址,最后有一句,第一次发布后需要来评论一下,方便提醒管理员审核

=======================

按操作步骤,此处是技术环节,开始生成gpg签名、修改maven的settings.xml配置文件、修改项目pom.xml、发布jar到远程仓库

6、生成gpg签名(我是mac电脑,所以只记录mac下的)

a、打开终端输入

gpg --gen-key ,如果没有gpg执行 brew install gpg后再执行前面的操作

b、输入名称
c、输入密码
c、输入英文字母o
d、如果是中文设置这里会弹出一个乱码的窗口,不要管他,这是让你输入一个秘钥(需要特别记住的)
e、输入秘钥,按回车
f、再次输入秘钥,按回车
g、等待几分钟

***注意要是操作报错了请看:

*** aa、执行

vi ~/.gnupg/gpg-agent.conf,新增一行

allow-loopback-pinentry 
*** ab、执行

vi ~/.gnupg/gpg.conf,新增二行

use-agent  
pinentry-mode loopback 

然后重新操作生成签名的过程

h、最后查看公钥

gpg --list-keys

公钥

这个就是公钥 0D025408499DCAAE9C4BDE0D8EAC051D3949179B

j、将公钥上传到服务器(只执行第一个都可以的,也可以传多个,反正服务器会自己同步),我上传到了2个公钥服务器

gpg --keyserver hkp://keys.openpgp.org:11371 --send-keys 0D025408499DCAAE9C4BDE0D8EAC051D3949179B
k、验证上传的公钥
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 0D025408499DCAAE9C4BDE0D8EAC051D3949179B

gpg --keyserver hkp://keys.openpgp.org:11371 --recv-keys  0D025408499DCAAE9C4BDE0D8EAC051D3949179B

到这里签名的事情完成了

7、修改maven的settings.xml配置文件

a、在 <servers>节点下增加
<server>
      <id>snapshot</id>
      <username>最开始注册的sonatype的账户用户mame</username>
      <password>最开始注册的sonatype的账户密码</password>
    </server>
    <server>
      <id>release</id>
      <username>最开始注册的sonatype的账户用户mame</username>
      <password>最开始注册的sonatype的账户密码</password>
    </server>
b、在<profiles> 节点下增加
<profile>
      <id>release</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <!-- 此处根据GPG版本填写,<2.0的填写GPG,2.0以上的填写gpg2 -->
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>生成gpg签名输入了2次的密码</gpg.passphrase>
      </properties>
    </profile>

到这里 maven的settings.xml配置文件完成

8、修改项目pom.xml

a、一种简单的方式,不必配很多复杂的东西了
<parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>
b、下面是我的项目pom.xml完整配置,我更倾向这种做法
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.github.liuchengts</groupId>
    <artifactId>spring-boot-auth-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <name>spring-boot-auth-starter</name>
    <description>基于springboot的权限验证</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--plugin-->
        <maven-source-plugin.version>3.0.1</maven-source-plugin.version>
        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
        <maven-gpg-plugin.version>1.1</maven-gpg-plugin.version>
        <maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
        <nexus-staging-maven-plugin.version>1.6.8</nexus-staging-maven-plugin.version>
    </properties>
    <!-- 许可证 -->
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <!-- 问题管理 -->
    <issueManagement>
        <system>github</system>
        <url>https://github.com/liuchengts/spring-boot-auth-starter/issues</url>
    </issueManagement>
    <!-- SCM(Source Control Management)标签允许你配置你的代码库,供 Maven web 站点和其它插件使用。 -->
    <scm>
        <tag>master</tag>
        <url>git@github.com:liuchengts/spring-boot-auth-starter.git</url>
        <connection>scm:git:git@github.com:liuchengts/spring-boot-auth-starter.git</connection>
        <developerConnection>scm:git:git@github.com:liuchengts/spring-boot-auth-starter.git</developerConnection>
    </scm>
    <!-- 项目开发者列表 -->
    <developers>
        <developer>
            <name>liuchengts</name>
            <email>790809568@qq.com</email>
            <organization>liuchengts</organization>
            <roles>
                <role>Developer</role>
            </roles>
            <timezone>+8</timezone>
        </developer>
    </developers>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>
        <!-- commons工具包 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--生成源码-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>${maven-source-plugin.version}</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--java doc-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>${maven-javadoc-plugin.version}</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <charset>UTF-8</charset>
                    <docencoding>UTF-8</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <!--为了防止不规范的java doc注释导致打包失败-->
                        <configuration>
                            <additionalJOption>-Xdoclint:none</additionalJOption>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--编译java-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>release</id>
            <activation>
                <jdk>[1.8,)</jdk>
            </activation>
            <properties>
                <additionalparam>-Xdoclint:none</additionalparam>
            </properties>
            <!-- 项目分发信息,在执行 mvn deploy 后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件部署到远程仓库。-->
            <distributionManagement>
                <snapshotRepository>
                    <!-- 这个 id 与第一步 setting.xml 中设置的对应 -->
                    <id>snapshot</id>
                    <!-- 这里的 url 就是 Issue 中回复的 snapshots 的 repository 地址-->
                    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
                </snapshotRepository>
                <repository>
                    <!-- 这个 id 与第一步 setting.xml 中设置的对应 -->
                    <id>release</id>
                    <!-- 这里的 url 就是 Issue 中回复的 staging 的 repository 地址-->
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
                </repository>
            </distributionManagement>
            <build>
                <plugins>
                    <!--生成源码-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>${maven-source-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>release</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>snapshot</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--签名校验-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>${maven-gpg-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>release</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--在deploy成功后会自动帮你进行Staging Repositories Close操作-->
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>${nexus-staging-maven-plugin.version}</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>release</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

按情况修改pom.xml中的内容,其中<dependencies> </dependencies>节点请忽略

9、发布jar到远程仓库

前置条件检查:

a、确定本机maven可以在终端执行 mvn ,不能执行不要问我,自己百度。
b、确定maven的settings.xml配置文件已经修改成前面的要求
c、确定项目的pom.xml修改正常
d、确定终端当前路径在当前项目下

执行 mvn clean deploy -P release
或者 mvn clean deploy -P release -Darguments="gpg.passphrase=生成gpg签名输入了2次的密码"

注意:第一次会让你输入那个签名的密码的,还有偶尔会出现超时,重试几次就好

到这里发布jar已经完成,可以到https://oss.sonatype.org/#stagingRepositories查看,账户密码与登录sonatype的相同
看到自己的jar了之后继续下面的步骤

=======================

10、当第一次构建完成后

需要去sonatype,在自己的问题底下评论一句

Component has been successfully issued

过一会审核员会回复

Central sync is activated for com.github.liuchengts. After you successfully release, your component will be published to Central, typically within 10 minutes, though updates to search.maven.org can take up to two hours.

这时候等待差不多2小时就能在中央仓库搜到刚才发布的包了

至此发布jar到maven中央仓库全部完成,在我踩了无数的坑之后找到一篇很好的文章,非常感谢博主,特在此推荐https://blog.csdn.net/liuyanglglg/article/details/90713552

欢迎关注我的个人公众号
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容