发布jar包到maven中央仓库(2021最新)

配置环境

安装java环境

链接:https://pan.baidu.com/s/1o-wFA-m33JQs-sQJ-DgRaQ 
提取码:ux7j

下载到服务器之后解压到指定位置


$ mkdir /usr/java
$ tar xzf jdk-8u301-linux-x64.tar.gz  -C /usr/java
$ vim /etc/profile

写入下面的内容

export JAVA_HOME=/usr/java/jdk1.8.0_301
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

保存退出
执行source /etc/profile使配置文件生效。

验证是否安装成功

 $ java -version
   java version "1.8.0_301"
   Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
   Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)

看到这个就表示安装成功了。

安装maven

$ mkdir /usr/mvn
$ cd /usr/mvn
$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
$ tar -zxvf apache-maven-3.8.1-bin.tar.gz
$ vim /etc/profile

写入下面内容

export MAVEN_HOME=/usr/mvn/apache-maven-3.8.1

export PATH=$PATH:$MAVEN_HOME/bin

保存退出
执行source /etc/profile使配置文件生效。

验证是否安装成功

 $ mvn -version
 Maven home: /usr/mvn/apache-maven-3.8.1
 Java version: 1.8.0_301, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_301/jre
 Default locale: en_US, platform encoding: UTF-8
 OS name: "linux", version: "4.18.0-240.10.1.el8_3.x86_64", arch: "amd64", family: "unix"

其他工具

$ yum install git gpg -y

如果已经安装可以跳过这步

注册sonatype账号

点击 注册一个新的账户。

登录之后新建一个issues
在这里插入图片描述

按要求填写就可以了,主要说一个Group Id
如果你的代码时托管在Github上的,那么写Group Id的时候就不能写
com.github.xxx了,我使用io开头,详情在这里

创建完成之后会跳转到这个连接https://issues.sonatype.org/browse/OSSRH-xxxx,当看到这个连接之后,你要在你的Github上创建一个仓库,来证明你是这个Github的主人,仓库的名字就是连接里的OSSRH-xxxx

创建后的仓库连接为https://github.com/yourgithubname/OSSRH-xxxx,然后在帖子下面留言告诉管理员这个仓库你已经创建好了名字为OSSRH-xxxx的仓库,这样可以省去他让你证明你是这个账户的拥有者,提高效率。等到管理员回复之后就可以上传jar包了。

下面是管理员的回复,看到这个就表示完成了。

io.github.xxx has been prepared, now user(s) youname can:
Publish snapshot and release artifacts to s01.oss.sonatype.org
Have a look at this section of our official guide for deployment instructions:
https://central.sonatype.org/publish/publish-guide/#deployment

Depending on your build configuration, your first component(s) might be released automatically after a successful deployment.
If that happens, you will see a comment on this ticket confirming that your artifact has synced to Maven Central.
If you do not see this comment within an hour or two, you can follow the steps in this section of our guide:
https://central.sonatype.org/publish/release/

######

As part of our efforts to improve the security and quality posture of the open source supply chain,
we plan to enable additional scanning of dependencies for security alerts soon. Since you're already
hosting your source code in Github, you can get these insights today by enabling Sonatype Lift.
Sonatype Lift is free forever on public repositories! Lift tells you about open source vulnerabilities
during code review, and goes beyond open source to scan your code for both code quality and security issues,
providing feedback right in your pull requests.
More information can be found at https://links.sonatype.com/products/lift/github-integration

######

发布前的准备

修改pom文件


  <groupId>io.github.xxxx</groupId>
  <artifactId>xxxx</artifactId>
  <version>1.0.0</version>
  <name>xxx</name>
  <url>xxxx</url>
  <description>xxxxx</description>
  <distributionManagement>
      <snapshotRepository>
          <id>ossrh</id>
          <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
      </snapshotRepository>
      <repository>
          <id>ossrh</id>
          <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
      </repository>
  </distributionManagement>
  <build>
      <plugins>

          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
          <!-- Source -->
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-source-plugin</artifactId>
              <version>2.2.1</version>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>jar-no-fork</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
          <!-- Javadoc -->
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-javadoc-plugin</artifactId>
              <version>2.9.1</version>
              <configuration>
                  <additionalparam>-Xdoclint:none</additionalparam>
              </configuration>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>jar</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
          <!-- GPG mvn clean deploy -P release -Dgpg.passphrase=YourPassphase -->
          <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>

  <licenses>
      <license>
          <name>MIT License</name>
          <url>https://github.com/xxx/xxx/xxxxxx/master/LICENSE</url>
          <distribution>repo,manual</distribution>
      </license>
  </licenses>
  <developers>
      <developer>
          <name>xxx</name>
          <email>xxxx</email>
          <url>xxxx</url>
      </developer>
  </developers>
  <scm>
      <connection>scm:git:https://github.com/xxx/xxxx.git</connection>
      <developerConnection>scm:git:https://github.com/xxxx/xxxx.git</developerConnection>
      <url>https://github.com/xxxx/xxxxx</url>
      <tag>0.0.1</tag>
  </scm>

只需修改和自己相关的内容即可,文中用xxxx表示,其他的不需要修改

Group Id一定要和申请的保持一致

上传gpg key

$  gpg generate-key
# 按照提示输入用户名和邮箱,最后会输入一个密码,记住这个密码下面会用 

将公钥发送到PGP密钥服务器

$   gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 6107DF0A8EE6A62EABFDD12914F722543E7D2C1E

返回结果

gpg: 将密钥‘14F722543E7D2C1E’上传到 hkp://keyserver.ubuntu.com:11371

验证是否上传成功

$   gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 6107DF0A8EE6A62EABFDD12914F722543E7D2C1E

返回结果

gpg: 密钥 14F722543E7D2C1E:“houbb <XXX@XX.com>”未改变
gpg: 合计被处理的数量:1
gpg:           未改变:1

setting.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
  <servers>
    <server>
      <id>ossrh</id>
      <username>sonatype账号</username>
      <password>sonatype密码</password>
    </server>
  </servers>
  <profiles>
   <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>gpg公钥的密码</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>

注意判断mvn使用的setting.xml是哪里的,一般在mvn主目录下的conf文件夹和用户目录下的.m2文件夹

上传程序

在项目根目录下执行

$ mvn clean deploy

稍等片刻会出现一个窗口,输入gpg的密码

看到这个表示发布成功

看到这个表示已经上传成功了。

上传成功之后登录到https://s01.oss.sonatype.org/,账号就是一开始注册的sonatype账号。

登录之后会点击Staging Repositories会看到这个页面,选中记录点击close,成功之后点击Release

到这步就已经大功告成了,稍等片刻就可以在这里看到上传的包了

比如 Group Idio.github.xxx

artifactIdtool

version1.0.0,

查看地址就是https://repo.maven.apache.org/maven2/io/github/xxx/tool/1.0.0

如果上面这个连接可以查到,就已经可以在项目中使用了。同步到中央仓库的时间不太确定。

发布成功之后去帖子上回复一下,告诉管理员发布成功了。

在这里插入图片描述

遇到的问题

  • 执行mvn deploy的时候总是返回401 错误,找了各种办法都不能解决,重新注册了个账户就可以了,不知道是为什么。

  • repository地址已经改变了,现在网上大部分的文章都是旧的,新的是这样的

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

官方文档有最新的配置信息,遇到问题之后还是要先查官方文档

  • centos发布时报错
    Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign

    解决方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值