将个人项目发布到mavan中央仓库


第一步,准备自己的git代码

比如在gitee或者github上的代码,我的是gitee码云上的,开源了一个处理业务日志采集的组件,支持注解方式,支持SpEL表达式,支持变量自定义。
话不多说,直接上我的项目地址:https://gitee.com/goudadong/logkit.git

第二步,注册sonatype账号

注册地址是:https://issues.sonatype.org/secure/Signup!default.jspa

填写注册信息,密码设置12位,非常复杂,恶心至极

创建之后登陆进去,新建一个issue

然后填写git的项目:

这是我最终填写后的结果:

填写完之后,后天会有人审核,由于本人没有域名,因此使用了他们指定的groupid:io.gitee.gouddong 作为项目的goupid,如果你有域名,那么就会做一个验证就可以了,

没有域名的情况下,他叫我在gitee上新建一个临时仓库,然后给他回复一下,已经创建好了,这个时候他就会进行审核,审核通过就会提示你可以将代码发布到 https://s01.oss.sonatype.org 上了。

第三步,配置maven

根据提示,访问https://central.sonatype.org/publish/publish-guide/#deployment 地址,
点击 Apache Maven

进去之后,往下翻,一直翻到:

打开maven setting文件,复制:


  
    
      ossrh
      用户名
      密码
    
  

然后在自己的项目的pom.xml中配置发布地址和maven构建插件


        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
                
                    org.apache.maven.plugins
                    maven-source-plugin
                    
                        
                            attach-sources
                            
                                jar-no-fork
                            
                        
                    
                
                
                    org.apache.maven.plugins
                    maven-deploy-plugin
                    ${maven.deploy.version}
                
            
        
        
            
            
                org.apache.maven.plugins
                maven-source-plugin
                2.2.1
                
                    
                        package
                        
                            jar-no-fork
                        
                    
                
            
            
            
                org.apache.maven.plugins
                maven-javadoc-plugin
                2.9.1
                
                    private
                    true
                    UTF-8
                    UTF-8
                    UTF-8
                    -Xdoclint:none
                    
                
                
                    
                        attach-javadocs
                        
                            jar
                        
                    
                
            
            
            
                org.apache.maven.plugins
                maven-gpg-plugin
                1.6
                
                    
                        verify
                        
                            sign
                        
                    
                
            
            
            
                org.sonatype.plugins
                nexus-staging-maven-plugin
                1.6.7
                true
                
                    ossrh
                    https://s01.oss.sonatype.org/
                    true
                
            
            
            
                org.codehaus.mojo
                flatten-maven-plugin
                ${maven.flatten.version}
                
                    true
                    resolveCiFriendliesOnly
                
                
                    
                        flatten
                        process-resources
                        
                            flatten
                        
                    
                    
                        flatten.clean
                        clean
                        
                            clean
                        
                    
                
            
        
    
    
        
            ossrh
            
                true
            
            
                gpg
                the_pass_phrase
            
        
    

并且在pom.xml中配置开发者、开源协议和项目的名称和描述以及url地址:

logkit
业务日志采集组件
https://gitee.com/goudadong/logkit

        
            The Apache Software License, Version 2.0
            http://www.apache.org/licenses/LICENSE-2.0.txt
        
    
    
        scm:git:https://gitee.com/goudadong/logkit.git
        scm:git:https://gitee.com/goudadong/logkit.git
        git:https://gitee.com/goudadong/logkit.git
    
    
        
            redwinter
            2360564660@qq.com
        
    

第四步 配置PGP的公钥信息

Windows下使用gpg4win来进行配置。下载地址 https://www.gpg4win.org/get-gpg4win.html
本人是windows系统,所以下载windows版本的进行安装,安装完之后gpg这个命令会自动生效,打开cmd输入:gpg --list-keys 查看自己的gpg公钥:

刚开始没有,就创建一个,使用命令: gpg --gen-key 进行创建,然后根据提示设置姓名,邮箱等信息,最后会生成Passphase的密钥,然后提示输入一个密码,输入你的密码之后公钥key就生成成功, 这个密码你需要记住,是一个8位的密码,在maven发布签名的时候会用到,然后你是用gpg --list-keys 就可以查看到刚刚生成的公钥key。
接下来使用 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <你的公钥key> 公钥信息发送到ubuntu.com服务器,后续推送maven仓库会做校验。

第五步 depoy项目

使用mvn clean deploy -P 这里的profile-id是你maven配置的prifile的id值,刚开始配置的是release ,那么就填写release就好了,执行之后maven会去编译最终上传到 https://s01.oss.sonatype.org/ 仓库。
**注意: 如果在deploy时提示code 401,那么就是你的用户名或者密码没设置正确,如果提示reasonphrase: forbidden 那么可能就是你的pom 文件中distributionManagement 设置有问题,因为 https://central.sonatype.org/publish/publish-maven/ 提示在2021年开始 https://oss.sonatype.org/ 这个地址已经不能使用了,我刚开始就是url地址写的是 https://oss.sonatype.org//content/repositories/snapshots 所以死活传不上去 **


  
    ossrh
    https://s01.oss.sonatype.org/content/repositories/snapshots
  
  
    ossrh
    https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/
  


        
            oss-releases
            https://s01.oss.sonatype.org/content/repositories/releases
            
                true
            
            
                false
            
        
        
            oss-snapshots
            https://s01.oss.sonatype.org/content/repositories/snapshots
            
                false
            
            
                true
            
        

第六步,查看是否发布成功

进入 https://s01.oss.sonatype.org/ 使用sonatype 的账号和密码登录,然后就可以搜索到你上传的项目了