记录一次提交开源JAR包到中央仓库的过程

原创
2019/05/03 18:19
阅读数 160

编写开源工程

我这次要开源的mybatis.batch插件,用于简化批量提交。 代码地址:https://github.com/liuaixi200/mybatis-batch-parent.git

申请提交到中央仓库的用户账号

申请地址:https://issues.sonatype.org
申请完后,记住用户名和密码,该用户名与密码可以用来登陆:oss.sonatype.org。

创建issure

注意两点: 项目选 : Community Support - Open Source Project Repository Hosting (OSSRH) 类型选: New Project

如果有自己的域名:groupId可以用自己的域名 如果没有自己的域名: groupId只能用com.github.xxx 或者io.github.xx

配置RSA密钥

下载gpg,下载地址:https://www.gnupg.org/download/ 安装完后,通常会用到以下命令

查看版本:  gpg --version  

生成密钥:

gpg --full-gen-key
gpg (GnuPG) 2.2.15; Copyright (C) 2019 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.

Real name: 用户名
Email address: 开发者邮箱
Comment: 111
You selected this USER-ID:
    "liuax (111) <liuax00@gmail.com>"

查看生成的密钥

D:\Program Files (x86)\GnuPG\bin>gpg --list-key
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
C:/Users/liuax01/AppData/Roaming/gnupg/pubring.kbx
--------------------------------------------------
pub   rsa2048 2019-05-03 [SC]
      XXXXXXXXXXXXXXX99AF5C965487C95B2525ED50
uid           [ultimate] liuax (111) <liuax00@gmail.com>
sub   rsa2048 2019-05-03 [E]

将key上传到服务器,最好以下3个都执行一遍,否则上传release包时,会报找不到pub key

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys XXXXXXXXXXXXXXX99AF5C965487C95B2525ED50
gpg --keyserver hkp://keys.gnupg.net:11371 --send-keys XXXXXXXXXXXXXXX99AF5C965487C95B2525ED50
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys XXXXXXXXXXXXXXX99AF5C965487C95B2525ED50

检测是否上传成功

gpg --keyserver hkp://keys.gnupg.net:11371 --recv-keys XXXXXXXXXXXXXXX99AF5C965487C95B2525ED50

配置pom.xml

添加name,description,url,licenses,developers,scm

 <name>mybatis.batch</name>
    <description>批量提交插件</description>
    <url>https://github.com/liuaixi200/mybatis-batch-parent</url>

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>liuax</name>
            <email>liuax00@gmail.com</email>
            <organization>liuaixi200</organization>
            <organizationUrl>https://github.com/liuaixi200</organizationUrl>
        </developer>
    </developers>

    <scm>
        <url>https://github.com/liuaixi200/mybatis-batch-parent.git</url>
    </scm>

添加 distributionManagement

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

添加profiles,以下插件缺一不可

<profiles>
        <profile>
            <id>release</id>
            <properties>
                <gpg.executable>gpg</gpg.executable>
                <gpg.passphrase>12345678</gpg.passphrase>
                <gpg.executable>D:\Program Files (x86)\GnuPG\bin\gpg.exe</gpg.executable>
                <gpg.homedir>C:/Users/liuax01/AppData/Roaming/gnupg</gpg.homedir>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-deploy-plugin</artifactId>
                        <version>2.8.2</version>
                        <executions>
                            <execution>
                                <id>default-deploy</id>
                                <phase>deploy</phase>
                                <goals>
                                    <goal>deploy</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.7</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>oss_sonatype</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <configuration>
                            <gpgArguments>
                                <gpgArgument>-Dgpg.passphrase=${gpg.passphrase}</gpgArgument>
                            </gpgArguments>
                        </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>3.0.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                        <configuration>
                            <aggregate>true</aggregate>
                            <charset>UTF-8</charset>
                            <encoding>UTF-8</encoding>
                            <docencoding>UTF-8</docencoding>
                            <additionalparam>-Xdoclint:none</additionalparam>
                        </configuration>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

setting中添加server

 <server>
      <id>oss_sonatype</id>
      <username>liuax</username>
      <password>wb!SSaM!gC3XXXX</password>
    </server>

需要上传时执行以下命令

mvn deploy -release

登陆oss.sonatype.org查看结果

回复issue 如果是第一次上传

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部