浅枫沐雪 浅枫沐雪
首页

溪初

后端程序员一枚
首页
  • 微服务如何发布api包接口给第三方使用?

    • 0.前言
      • 1.开发环境
      • 2.相关账户
      • 3.发包规范
    • 1.发布到aliyun镜像仓库
      • 1.1.创建阿里云制品库
      • 1.2.发包配置
      • 1.3.配置maven镜像仓库地址
    • 2.发布到maven公共镜像仓库
      • 1.注册官网:https://central.sonatype.com/publishing
      • 2.创建一个命名空间,可以使用github,但是用自己的域名更方便,直接添加txt解析即可,顶级域名使用@,类型txt,值sonatype提供的,然后验证即可
      • 3.授权token用户
      • 4.配置gpg签名
      • 5.发布到Maven中央仓库
      • 6.手动发布
    • 3.参考链接
    溪初
    2024-11-27
    随笔
    目录

    微服务如何发布api包接口给第三方使用?

    # 微服务如何发布api包接口给第三方使用?

    # 0.前言

    # 1.开发环境

    • 操作系统:win10/11
    • jdk 1.8
    • maven 3.9.9
    • idea 2024.3.2
    • git客户端,git bash 需要使用

    # 2.相关账户

    • 阿里云账号:自己注册一下就好
    • maven中央仓库账号:自己用邮箱注册一下就好
    • github账号:可使用github的域名上传到maven中央仓库,有域名直接忽略
    • 域名:自己购买一个,不需要备案,只需要添加解析记录

    # 3.发包规范

    • 0.0.0-SNAPSHOT 以 SNAPSHOT 结尾是开发阶段包,都是 0.0.0-SNAPSHOT 命名。
    • release 包,只需要命名版本号即可。
    <!--快照包命名规范-->
    <dependency>
        <groupId>cn.bugstack</groupId>
        <artifactId>xfg-dev-tech-api</artifactId>
        <version>0.0.0-SNAPSHOT</version>
    </dependency>
    
    <!--release命名规范-->
    <dependency>
        <groupId>cn.bugstack</groupId>
        <artifactId>xfg-dev-tech-api</artifactId>
        <version>1.0</version>
    </dependency>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    # 1.发布到aliyun镜像仓库

    # 1.1.创建阿里云制品库

    • 地址:https://packages.aliyun.com/ (opens new window)- 填写个人信息申请即可。
    • 申请后,你会看到制品仓库配置,自己编辑名称与描述。包括生产库和非生产库,他们主要负责给你提供 release、snapshot 发包和拉取。
    • 创建2个制品库,命名参考xxx-release、xxx-snapshot,xxx为你的项目名称。分别代表发布版本和开发版本

    # 1.2.发包配置

    分别进入 xxx-release、xxx-snapshot,都可以获得一份 Maven settings.xml 配置文件,把两份文件夹下载后可以找到 release、snapshot 的差异,合并成一份文件。这样你就可以在本地发 release、snapshot 包了。

    • 如下是我的个人配置
    <?xml version="1.0" encoding="UTF-8"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
        
    	<localRepository>D:\DEVELOP\maven\aliyun-private-repo</localRepository>
    	
    	<mirrors>
            <mirror>
                <id>mirror</id>
                <mirrorOf>central,jcenter,!repo-aliyun</mirrorOf>
                <name>mirror</name>
                <url>https://maven.aliyun.com/nexus/content/groups/public</url>
            </mirror>
        </mirrors>
        <servers>
            <server>
                <id>repo-release</id>
                <username>5f1c62a0918d483c39b2320d58</username>
                <password>RansgR1hmS_12g</password>
            </server>
    		<server>
                <id>repo-snapshot</id>
                <username>5fc621a0918d483c39b2320d58</username>
                <password>Ransg1RhmS_2g</password>
            </server>
    		
            <!--gpg签名,这个username、password填注册用户名密码-->
    		<server>
    			<id>ossrh</id>
    			<username>118556@qq.com</username>
    			<password>Xfffic1edfsdhu1jun29.</password>
    		</server>
    		
            <!--maven中央仓库复制的授权用户,然后改一下id即可-->
    		<server>
    			<id>central-xcfffj</id>
    			<username>/ytfffffffFXv1j</username>
    			<password>aPeCKjfffffffffffTzZxiBibJmsyGcPu7bymMPqd1bef8bOYef3JtR</password>
    		</server>
    
    		
        </servers>
        <profiles>
            
            <!--直接复制这块,注意改下id,不重复就好-->
    		<profile>
                <id>rdc</id>
                <properties>
                    
                    <altReleaseDeploymentRepository>
                        repo-aliyun::default::https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-aliyun
                    </altReleaseDeploymentRepository>
                    
                    
                    <altSnapshotDeploymentRepository>
                        repo-aliyun::default::https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-aliyun
                    </altSnapshotDeploymentRepository>
                    
                </properties>
                <repositories>
                    <repository>
                        <id>central</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>false</enabled>
                        </snapshots>
                    </repository>
                    <repository>
                        <id>snapshots</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>false</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                    <repository>
                        <id>repo-aliyun</id>
                        <url>https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-aliyun</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                </repositories>
                <pluginRepositories>
                    <pluginRepository>
                        <id>central</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>false</enabled>
                        </snapshots>
                    </pluginRepository>
                    <pluginRepository>
                        <id>snapshots</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>false</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </pluginRepository>
                    <pluginRepository>
                        <id>repo-aliyun</id>
                        <url>https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-aliyun</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </pluginRepository>
                </pluginRepositories>
            </profile>
    
    
            <!--直接复制这块,注意改下id,不重复就好-->
            <profile>
                <id>rdc-pro</id>
                <properties>
                    
                    <altReleaseDeploymentRepository>
                        repo-axvdb::default::https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-axvdb
                    </altReleaseDeploymentRepository>
                    
                    
                    <altSnapshotDeploymentRepository>
                        repo-axvdb::default::https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-axvdb
                    </altSnapshotDeploymentRepository>
                    
                </properties>
                <repositories>
                    <repository>
                        <id>central</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>false</enabled>
                        </snapshots>
                    </repository>
                    <repository>
                        <id>snapshots</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>false</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                    <repository>
                        <id>repo-axvdb</id>
                        <url>https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-axvdb</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </repository>
                </repositories>
                <pluginRepositories>
                    <pluginRepository>
                        <id>central</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>false</enabled>
                        </snapshots>
                    </pluginRepository>
                    <pluginRepository>
                        <id>snapshots</id>
                        <url>https://maven.aliyun.com/nexus/content/groups/public</url>
                        <releases>
                            <enabled>false</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </pluginRepository>
                    <pluginRepository>
                        <id>repo-axvdb</id>
                        <url>https://packages.aliyun.com/5fc62a2be89148238ce829c4/maven/repo-axvdb</url>
                        <releases>
                            <enabled>true</enabled>
                        </releases>
                        <snapshots>
                            <enabled>true</enabled>
                        </snapshots>
                    </pluginRepository>
                </pluginRepositories>
            </profile>
    
    
            <!--gpg签名,直接复制这块,注意改下id,不重复就好-->
    		<profile>
    			  <id>ossrh</id>
    			  <properties>
                      <!--gpg.exe安装路径-->
    				  <gpg.executable>"D:\softs\GnuPG\bin\gpg.exe"</gpg.executable>
                      <!--自己创建证书时候的密码-->
    				  <gpg.passphrase>gmOEN8yU</gpg.passphrase>
    			  </properties>
    		</profile>
    	</profiles>
        <activeProfiles>
            <!--默认激活项目,这个决定发布到哪个仓库,从额外哪个仓库拉镜像(先从默认镜像找)-->
            <!--没关系,到了idea还能自己重新勾选-->
            <activeProfile>rdc</activeProfile>
        </activeProfiles>
    </settings>
    
    
    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227

    # 1.3.配置maven镜像仓库地址

    • 配置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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        
        <groupId>cn.allms.maven</groupId>
        <artifactId>dev-tech-api</artifactId>
        <version>2.0.3</version>
    
        <!--
        1. 阿里云发包,打开 parent 注释掉单独的 groupId、version
        2. maven 中心仓库发包,保留目前的结构
        3. 阿里云发包 不需要下面的 gpg 等配置操作,可以注释掉。
        -->
    
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <retrofit2.version>2.9.0</retrofit2.version>
            <slf4j.version>2.0.6</slf4j.version>
            <maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
            <maven-source-plugin.version>3.2.1</maven-source-plugin.version>
            <maven-gpg-plugin.version>3.2.7</maven-gpg-plugin.version>
            <maven-checksum-plugin.version>1.10</maven-checksum-plugin.version>
            <project.author.github>https://github.com/qfmx</project.author.github>
        </properties>
    
        <name>dev-tech-api</name>
        <description>maven 中心仓库发包</description>
        <url>${project.author.github}/xfg-dev-tech-micro-service-a</url>
    
        <licenses>
            <license>
                <name>Apache License</name>
                <url>https://opensource.org/license/apache-2-0/</url>
                <distribution>repo</distribution>
            </license>
        </licenses>
    
        <developers>
            <developer>
                <id>Xichujun</id>
                <name>Xichujun</name>
                <email>1125438556@qq.com</email>
                <url>${project.author.github}</url>
                <organization>xfg-dev-tech-micro-service-a</organization>
                <organizationUrl>${project.author.github}/xfg-dev-tech-micro-service-a</organizationUrl>
                <roles>
                    <role>architect</role>
                    <role>developer</role>
                </roles>
                <timezone>Asia/Shanghai</timezone>
            </developer>
        </developers>
    
        <scm>
            <connection>scm:git:${project.author.github}/xfg-dev-tech-micro-service-a.git</connection>
            <developerConnection>scm:git:${project.author.github}/xfg-dev-tech-micro-service-a.git</developerConnection>
            <tag>HEAD</tag>
            <url>${project.author.github}/xfg-dev-tech-micro-service-a</url>
        </scm>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>2.2.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>
                        <encoding>UTF-8</encoding>
                        <aggregate>true</aggregate>
                        <charset>UTF-8</charset>
                        <docencoding>UTF-8</docencoding>
                    </configuration>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <additionalparam>-Xdoclint:none</additionalparam>
                                <javadocExecutable>
                                    C:\\Program Files\\java\\jdk1.8.0_201\\bin\\javadoc
                                </javadocExecutable>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.3</version>
                    <configuration>
                        <autoVersionSubmodules>true</autoVersionSubmodules>
                        <useReleaseProfile>false</useReleaseProfile>
                        <releaseProfiles>release</releaseProfiles>
                        <goals>deploy</goals>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>net.nicoulaj.maven.plugins</groupId>
                    <artifactId>checksum-maven-plugin</artifactId>
                    <version>${maven-checksum-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>create-checksums</id>
                            <goals>
                                <goal>artifacts</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <!--发阿里云注释这个插件,发到maven仓库启用,勾选released配置-->
               <!-- <plugin>
                    <groupId>org.sonatype.central</groupId>
                    <artifactId>central-publishing-maven-plugin</artifactId>
                    <version>0.5.0</version>
                    <extensions>true</extensions>
                    <configuration>
                        <publishingServerId>central-xcj</publishingServerId>
                    </configuration>
                </plugin>-->
                <!--发阿里云注释这个插件,发到maven仓库启用。勾选gpg配置-->
                <!--<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>1.5</version>
                    <executions>
                        <execution>
                            <id>sign-artifacts</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>-->
            </plugins>
        </build>
    
        <profiles>
            <profile>
                <id>release</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-javadoc-plugin</artifactId>
                            <version>2.9.1</version>
                            <executions>
                                <execution>
                                    <id>attach-javadocs</id>
                                    <goals>
                                        <goal>jar</goal>
                                    </goals>
                                    <configuration>
                                        <additionalparam>-Xdoclint:none</additionalparam>
                                        <javadocExecutable>
                                            C:\\Program Files\\java\\jdk1.8.0_201\\bin\\javadoc
                                        </javadocExecutable>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    
    </project>
    
    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    • idea检查配置已经勾选了xxx-release或者xxx-snapshot,如果没有,手动勾选
    • idea点击maven -> setting -> build -> build tools -> maven -> user setting file -> 选择settings.xml
    • mvn clean,或者点击idea的clean,确保本地仓库没有缓存
    • mvn deploy,或者点击idea的deploy,发布到maven仓库
    • ok,可以去对应的制品仓库去看看了

    # 2.发布到maven公共镜像仓库

    # 1.注册官网:https://central.sonatype.com/publishing

    # 2.创建一个命名空间,可以使用github,但是用自己的域名更方便,直接添加txt解析即可,顶级域名使用@,类型txt,值sonatype提供的,然后验证即可

    # 3.授权token用户

    • 点击右上角头像,选择view account
    • 路径:https://central.sonatype.com/account
    • 点击Generate User Token即可
    • 然后复制到自己settings.xml中
     <!--maven中央仓库复制的授权用户,然后改一下id即可.放到servers标签中-->
    		<server>
    			<id>central-xcfffj</id>
    			<username>/ytfffffffFXv1j</username>
    			<password>aPeCKjfffffffffffTzZxiBibJmsyGcPu7bymMPqd1bef8bOYef3JtR</password>
    		</server>
    
    1
    2
    3
    4
    5
    6

    # 4.配置gpg签名

    • 下载gpg工具,https://www.gnupg.org/download/index.html
    • 安装Gpg4win:Full featured Windows version of GnuPG ,https://gpg4win.org/get-gpg4win.html

    此工具通过Windows窗口进行密钥的管理。

    • 安装download sig:Simple installer for GnuPG 1.4, https://www.gnupg.org/ftp/gcrypt/binary/gnupg-w32cli-1.4.23.exe

    此工具用于后续Maven发布使用

    创建密钥
    
    Gpg4win
    
    生成秘钥,按步步骤提示进行操作
    必须勾选密码
    image
    按提示输入密码
    image
    
    秘钥生成好后,右键选择密钥,需要你把公钥上传到公共服务器供sonatype验证。
    image
    
    命令行
    
    生成秘钥,按步骤提示进行操作
    
    gpg --generate-key
    
    # 生成结果
    gpg (GnuPG/MacGPG2) 2.2.32; Copyright (C) 2021 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.
    注意:使用 “gpg --full-generate-key” 以获得一个全功能的密钥生成对话框。
    GnuPG 需要构建用户标识以辨认您的密钥。
    真实姓名: xxxxxxx
    电子邮件地址: xxxxxxx@foxmail.com
    您选定了此用户标识:
    “xxxxxxxx &lt;xxxxx@foxmail.com>”
    更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)? u
    更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)? o
    我们需要生成大量的随机字节。在质数生成期间做些其他操作(敲打键盘
    、移动鼠标、读写硬盘之类的)将会是一个不错的主意;这会让随机数
    发生器有更好的机会获得足够的熵。
    我们需要生成大量的随机字节。在质数生成期间做些其他操作(敲打键盘
    、移动鼠标、读写硬盘之类的)将会是一个不错的主意;这会让随机数
    发生器有更好的机会获得足够的熵。
    gpg: 密钥 54EC3C8FA3A5B50F 被标记为绝对信任
    gpg: 目录‘/Users/xxxxxxxxx/.gnupg/openpgp-revocs.d’已创建
    gpg: 吊销证书已被存储为‘/Users/xxxxxxxxx/.gnupg/openpgp-revocs.d/xxxxxxxxxxxxxxxxxxxxxxxxxxxx.rev’
    公钥和私钥已经生成并被签名。
    pub   rsa3072 2021-12-06 [SC] [有效至:2023-12-06]
    8BDxxxxxxxxxxxxxxxxxxxxxxxxxxxxB50F
    uid                      xxxxxxxx &lt;xxxxxxxx@foxmail.com>
    sub   rsa3072 2021-12-06 [E] [有效至:2023-12-06]
    秘钥生成好后,需要你把公钥上传到公共服务器供sonatype验证。
    
    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
    • 官网教程里有三个地址可以用:

    keyserver.ubuntu.com keys.openpgp.org pgp.mit.edu

    ➜  ~ gpg --keyserver pgp.mit.edu:11371 --send-keys 8BD96B0EA18E5162B94EA7F754EC3C8FA3A5B50F
    gpg: 正在发送密钥 54EC3C8FA3A5B50F 到 pgp.mit.edu:11371
    gpg: 发送至公钥服务器失败:文件结尾
    gpg: 发送至公钥服务器失败:文件结尾
    ➜  ~ gpg --keyserver keyserver.ubuntu.com --send-keys 8BD96B0EA18E5162B94EA7F754EC3C8FA3A5B50F
    gpg: 正在发送密钥 54EC3C8FA3A5B50F 到 hkp://keyserver.ubuntu.com
    gpg: 发送至公钥服务器失败:Network is unreachable
    gpg: 发送至公钥服务器失败:Network is unreachable
    ➜  ~ gpg --keyserver pgp.mit.edu:11371 --send-keys 54EC3C8FA3A5B50F
    gpg: 正在发送密钥 54EC3C8FA3A5B50F 到 pgp.mit.edu:11371
    gpg: 发送至公钥服务器失败:文件结尾
    gpg: 发送至公钥服务器失败:文件结尾
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    • 配置setting.xml setting.xml为Maven的全局配置文件,一般路径为$MAVEN_HOME/conf/settings.xml ,找到Services节点进行如下配置
    <servers>
        <server>
            <id>ossrh</id>
            <username>你的Sonatype账号</username>
            <password>你的Sonatype密码</password>
        </server>
    </servers>
    
    
    1
    2
    3
    4
    5
    6
    7
    8

    找到profiles节点进行如下配置,gpg.executable节点建议配置第二个安装包安装的gpg路径,这一步一定要做不然容易发布的时候提示找不到gpg。

    <profiles>
          <profile>
              <id>ossrh</id>
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
              <properties>
                  <gpg.executable>"D:\Program Files (x86)\GnuPG\bin\gpg.exe"</gpg.executable>
                  <gpg.passphrase>你的密钥创建时候的密码</gpg.passphrase>
              </properties>
          </profile>
    </profiles>
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    # 5.发布到Maven中央仓库

    • 打开idea,配置勾选release、ossrh(gpg发布签名的)
    • 配置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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        
        <groupId>cn.allms.maven</groupId>
        <artifactId>dev-tech-api</artifactId>
        <version>2.0.3</version>
    
        <!--
        1. 阿里云发包,打开 parent 注释掉单独的 groupId、version
        2. maven 中心仓库发包,保留目前的结构
        3. 阿里云发包 不需要下面的 gpg 等配置操作,可以注释掉。
        -->
    
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <retrofit2.version>2.9.0</retrofit2.version>
            <slf4j.version>2.0.6</slf4j.version>
            <maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
            <maven-source-plugin.version>3.2.1</maven-source-plugin.version>
            <maven-gpg-plugin.version>3.2.7</maven-gpg-plugin.version>
            <maven-checksum-plugin.version>1.10</maven-checksum-plugin.version>
            <project.author.github>https://github.com/qfmx</project.author.github>
        </properties>
    
        <name>dev-tech-api</name>
        <description>maven 中心仓库发包</description>
        <url>${project.author.github}/xfg-dev-tech-micro-service-a</url>
    
        <licenses>
            <license>
                <name>Apache License</name>
                <url>https://opensource.org/license/apache-2-0/</url>
                <distribution>repo</distribution>
            </license>
        </licenses>
    
        <developers>
            <developer>
                <id>Xichujun</id>
                <name>Xichujun</name>
                <email>1125438556@qq.com</email>
                <url>${project.author.github}</url>
                <organization>xfg-dev-tech-micro-service-a</organization>
                <organizationUrl>${project.author.github}/xfg-dev-tech-micro-service-a</organizationUrl>
                <roles>
                    <role>architect</role>
                    <role>developer</role>
                </roles>
                <timezone>Asia/Shanghai</timezone>
            </developer>
        </developers>
    
        <scm>
            <connection>scm:git:${project.author.github}/xfg-dev-tech-micro-service-a.git</connection>
            <developerConnection>scm:git:${project.author.github}/xfg-dev-tech-micro-service-a.git</developerConnection>
            <tag>HEAD</tag>
            <url>${project.author.github}/xfg-dev-tech-micro-service-a</url>
        </scm>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>2.2.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>
                        <encoding>UTF-8</encoding>
                        <aggregate>true</aggregate>
                        <charset>UTF-8</charset>
                        <docencoding>UTF-8</docencoding>
                    </configuration>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <additionalparam>-Xdoclint:none</additionalparam>
                                <javadocExecutable>
                                    C:\\Program Files\\java\\jdk1.8.0_201\\bin\\javadoc
                                </javadocExecutable>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.3</version>
                    <configuration>
                        <autoVersionSubmodules>true</autoVersionSubmodules>
                        <useReleaseProfile>false</useReleaseProfile>
                        <releaseProfiles>release</releaseProfiles>
                        <goals>deploy</goals>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>net.nicoulaj.maven.plugins</groupId>
                    <artifactId>checksum-maven-plugin</artifactId>
                    <version>${maven-checksum-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>create-checksums</id>
                            <goals>
                                <goal>artifacts</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <!--发到maven仓库启用-->
                <!--发阿里云注释这个插件,发到maven仓库启用,勾选released配置-->
                <plugin>
                    <groupId>org.sonatype.central</groupId>
                    <artifactId>central-publishing-maven-plugin</artifactId>
                    <version>0.5.0</version>
                    <extensions>true</extensions>
                    <configuration>
                        <publishingServerId>central-xcj</publishingServerId>
                    </configuration>
                </plugin>
    
                <!--发到maven仓库启用-->
                <!--发阿里云注释这个插件,发到maven仓库启用。勾选gpg配置-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>1.5</version>
                    <executions>
                        <execution>
                            <id>sign-artifacts</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
        <profiles>
            <profile>
                <id>release</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-javadoc-plugin</artifactId>
                            <version>2.9.1</version>
                            <executions>
                                <execution>
                                    <id>attach-javadocs</id>
                                    <goals>
                                        <goal>jar</goal>
                                    </goals>
                                    <configuration>
                                        <additionalparam>-Xdoclint:none</additionalparam>
                                        <javadocExecutable>
                                            C:\\Program Files\\java\\jdk1.8.0_201\\bin\\javadoc
                                        </javadocExecutable>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    
    </project>
    
    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    • mvn clean,或者点击idea的clean,确保本地仓库没有缓存
    • mvn deploy,或者点击idea的deploy,发布到maven仓库
    • ok,可以去maven的制品仓库去看看了
    • 然后点击发布就ok了

    # 6.手动发布

    # 手动发布
    mvn clean
    mvn install
    # 找到taget目录下的文件
    cd target
    
    # 创建文件夹,命名规则 groupId/artifactId/version
    mkdir -p com/sonatype/central/example/example_java_project/0.1.0
    # 将文件拷贝到该目录下. 使用ctrl+c,然后切换到com/sonatype/central/example/example_java_project/0.1.0目录下,使用ctrl+v
    # 参考一个.jar相关有4个文件哦
    $ tree
    .
    `-- com
        `-- sonatype
            `-- central
                `-- example
                    `-- example_java_project
                        `-- 0.1.0
                            |-- example_java_project-0.1.0-javadoc.jar
                            |-- example_java_project-0.1.0-javadoc.jar.asc
                            |-- example_java_project-0.1.0-javadoc.jar.md5
                            |-- example_java_project-0.1.0-javadoc.jar.sha1
                            |-- example_java_project-0.1.0-sources.jar
                            |-- example_java_project-0.1.0-sources.jar.asc
                            |-- example_java_project-0.1.0-sources.jar.md5
                            |-- example_java_project-0.1.0-sources.jar.sha1
                            |-- example_java_project-0.1.0.jar
                            |-- example_java_project-0.1.0.jar.asc
                            |-- example_java_project-0.1.0.jar.md5
                            |-- example_java_project-0.1.0.jar.sha1
                            |-- example_java_project-0.1.0.pom
                            |-- example_java_project-0.1.0.pom.asc
                            |-- example_java_project-0.1.0.pom.md5
                            `-- example_java_project-0.1.0.pom.sha1
                            
    # 打包成zip包
    zip -r com.zip com/sonatype/central/example/example_java_project/0.1.0  
    
    # 上传到maven仓库
    name命名方式groupId:artifactId:version
    描述可以不写
    上传com.zip文件
    # 等待上传完成即可
    
    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

    # 3.参考链接

    • maven镜像仓库镜像上传必须文件规范 (opens new window)
    • Maven Central 微服务包发布管 (opens new window)
    • 手把手带你发布一个JAR包到Maven仓库 (opens new window)
    • 一篇搞定发布自己的类库到Maven中央仓库 (opens new window)
    #工作
    最后更新时间: 2024/12/11, 23:02:28
    最近更新
    01
    git本地空间损坏
    11-29
    02
    IDEA授权备份
    11-02
    03
    知识库数据迁移24-11-04
    11-01
    更多文章>
    Theme by Vdoing | Copyright © 2020-2025 溪初 | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式