惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

浅枫沐雪

反思 - 浅枫沐雪 面向领域驱动设计 —— Domain Driver Design idea授权备份 - 浅枫沐雪 记知识库数据迁移 - 浅枫沐雪 四月是你的谎言 - 浅枫沐雪
微服务如何发布api包接口给第三方使用? - 浅枫沐雪
作者: qfmx · 2025-05-15 · via 浅枫沐雪

这篇文章距离最后更新已过395 天,如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!


title: 微服务如何发布api包接口给第三方使用?
date: 2024-11-27 21:00:00
permalink: /pages/2sd1c67/
sidebar: auto
categories:

  • 随笔

tags:

  • 工作

author:

name: 溪初

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.发布到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.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>Xichu2jun</id>
              <name>Xi2chujun</name>
              <email>1136@qq.com</email>
              <url>${project.author.github}</url>
              <organization>xf23g-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>
  • 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用户

创建密钥

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 <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 <xxxxxxxx@foxmail.com>
sub   rsa3072 2021-12-06 [E] [有效至:2023-12-06]
秘钥生成好后,需要你把公钥上传到公共服务器供sonatype验证。
  • 官网教程里有三个地址可以用:
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: 发送至公钥服务器失败:文件结尾
  • 配置setting.xml
    setting.xml为Maven的全局配置文件,一般路径为$MAVEN_HOME/conf/settings.xml ,找到Services节点进行如下配置

    <servers>
      <server>
          <id>ossrh</id>
          <username>你的Sonatype账号</username>
          <password>你的Sonatype密码</password>
      </server>
    </servers>
    

    找到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>
    

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>Xichuj1un</id>
              <name>Xichu1jun</name>
              <email>116@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>
  • 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文件
# 等待上传完成即可

3.参考链接

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自浅枫沐雪