美文网首页
发布jar到maven中央仓库

发布jar到maven中央仓库

作者: 安静的夜灬 | 来源:发表于2020-03-27 17:36 被阅读0次

    最近写了一个开源组件,想发布到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

    欢迎关注我的个人公众号

    相关文章

      网友评论

          本文标题:发布jar到maven中央仓库

          本文链接:https://www.haomeiwen.com/subject/kftruhtx.html