macos上deploy一个jar包到maven中央仓库

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

前言

一、创建sonatype账号

二、安装gpg环境

三、配置maven的settings.xml

四、配置pom.xml

五:deploy到中央仓库

六、在sonatype进行release

总结


前言

发布jar到maven中央仓库需要五个步骤,其中第二步gpg安装和使用在不同的操作系统上存在差异性,这里着重讲解的是macos上gpg的使用方法。


提示:以下是本篇文章正文内容,下面案例可供参考

一、创建sonatype账号

地址:Loading...
注意要记住账号密码,会用于我们后续的发布及相关操作

创建完账号后,请提交一个issues
选择:Community Support - Open Source Project Repository Hosting (OSSRH)

groupId:填写您的groupId
projectUrl :可以填写源代码所在的地址,或者官网首页,比如:https://gitee.com/wang-laoshi/shield.git

SCM url: 填写源代码的git地址,比如:https://gitee.com/wang-laoshi/shield

兜兜飞节

接下来按照提示验证域名(前提你需要一个域名并且能设置dns解析)
 

二、安装gpg环境

macos下安装gpg环境会遇到很多问题

默认命令: brew install gpg

但不太可能会一次成功,可能会遇到以下错误 gnupg: no bottle available!

解决方案: 将清华的软件源配置为brew的软件源

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc

然后运行

gpg --generate-key

依次输入用户名,邮箱,然后按o确认
再输入两次密码,记住这个密码,然后会生成

记住pub第二行部分的公钥字符串,复制一下

注意这串很长的字符串,就是你的publickey,用于上传秘钥到opengpg
然后上传秘钥

gpg --keyserver http://keys.openpgp.org:11371 --send-keys 99EF4C04F5A776CC9E很长的公钥

至此,gpg部分结束

三、配置maven的settings.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 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <localRepository>/Users/wanglaoshi/.m2/repository</localRepository>
  <proxies></proxies>

  <servers>
    <server>
      <id>sonatype-releases</id>
      <username>你的sonatype用户名</username>
      <password>你的sonatype密码</password>
    </server>
    <server>
      <id>sonatype-snapshots</id>
      <username>你的sonatype用户名</username>
      <password>你的sonatype密码</password>
    </server>
  </servers>

  <mirrors>
    <mirror>
      <id>mirror</id>
      <mirrorOf>central,jcenter,!rdc-releases,!rdc-snapshots</mirrorOf>
      <name>mirror</name>
      <url>https://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
  
  <profiles>
    <profile>
      <id>sonatype</id>
      <properties>
        <altReleaseDeploymentRepository>
          sonatype-releases::default::https://oss.sonatype.org/service/local/staging/deploy/maven2/
        </altReleaseDeploymentRepository>
        <altSnapshotDeploymentRepository>
          sonatype-snapshots::default::https://oss.sonatype.org/content/repositories/snapshots
        </altSnapshotDeploymentRepository>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>你输入的gpg的密码</gpg.passphrase>
      </properties>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>sonatype</activeProfile>
  </activeProfiles>
   

</settings>

四、配置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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.7.6</version>
        <relativePath/>
    </parent>

    <groupId>net.quanter</groupId>
    <artifactId>shield-parent</artifactId>
    <name>shield-parent</name>
    <packaging>pom</packaging>
    <version>1.4.0</version>
    <url>https://gitee.com/wang-laoshi/shield</url>
    <description>您的描述</description>

    <licenses>
        <license>
            <name>GNU Lesser General Public License Version 3</name>
            <url>http://www.gnu.org/licenses/lgpl.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <scm>
        <tag>master</tag>
        <url>https://gitee.com/wang-laoshi/shield</url>
        <connection>scm:git:https://gitee.com/wang-laoshi/shield</connection>
        <developerConnection>scm:git:https://gitee.com/wang-laoshi/shield</developerConnection>
    </scm>

    <organization>
        <name>宽特</name>
        <url>https://www.quanter.net</url>
    </organization>

    <developers>
        <developer>
            <name>王老实</name>
            <email>custer7572@163.com</email>
            <organization>宽特</organization>
        </developer>
    </developers>

    <properties>
        <java.version>11</java.version>
        <resource.delimiter>@</resource.delimiter>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <shield.version>1.4.0-SNAPSHOT</shield.version>
    </properties>

    <profiles>
        <profile>
            <id>sonatype</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>3.0.1</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.12</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>${releases.id}</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <charset>${project.build.sourceEncoding}</charset>
                    <docencoding>${project.build.sourceEncoding}</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <additionalJOption>-Xdoclint:none</additionalJOption>
                        </configuration>
                    </execution>
                    <execution>
                        <id>aggregate</id>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                        <phase>prepare-package</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

五:deploy到中央仓库

命令:
 

mvn clean install org.apache.maven.plugins:maven-deploy-plugin:2.8:deploy -DskipTests

如果出现错误
gpg: 签名时失败: Inappropriate ioctl for device,是因为没有让终端具备输入框的能力
则执行:
 

export GPG_TTY=$(tty)

该处使用的url网络请求的数据。

六、在sonatype进行release

登录https://oss.sonatype.org/
选择Staging Repositories,找到刚刚deploy上来的jar,选择close

如果正常情况下,不会出现警告标志,则说明成功了,如果gpg那一步失败了,则会出现无签名的警告
最后选择release

 

至此大功告成


总结

最麻烦的可能是第一步和第二步,后面基本上只要网络通畅,按照步骤操作基本上没啥大问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值