一、预置环境
基于Centos7预置环境(均可以使用我的一键安装脚本)
- java 1.8
- maven 3.6.3(maven3.8以上不再支持http,弃用)
- gpg 2.0+(Centos7内置)
需要去System Dashboard - Sonatype JIRA创建一个Issue,使用的域名还要验证是否真实持有。
创建成功之后,如图所示,此时就可以进行jar包部署了。
二、发布
2.1 创建密钥
安装生成随机字节的工具,用于密钥生成
1 2
| yum -y install rng-tools rngd -r /dev/urandom
|
着手生成密钥
将密钥发布到密钥服务器
1
| gpg --keyserver keyserver.ubuntu.com --send-keys 你的密钥
|
去掉空格,建议直接浏览器console
“B515 0A97 DB5A C076 3E6A 0BC2 D241 590E E91B 2A73”.replaceAll(/\s+/g,””);
2.2 配置Maven
在maven的settings.xml里面添加
可以which mvn来获取到路径,进而找到/usr/local/maven/conf/settings.xml
1 2 3 4 5
| <server> <id>ossrh</id> <username>sonatype用户名</username> <password>sonatype密码</password> </server>
|
2.3 配置pom
以我自己的pom为例,主要是将官方提供的plugin都cv了过来。
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
| <?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>top.meethigher</groupId> <artifactId>object-converter</artifactId> <version>1.0</version> <name>object-converter</name> <description>object converter(deep copy) based on annovation</description> <url>https://github.com/meethigher/object-converter</url> <licenses> <license> <name>Apache License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments>A business-friendly OSS license</comments> </license> </licenses> <scm> <url>https://github.com/meethigher/object-converter</url> <connection>https://github.com/meethigher/object-converter.git</connection> </scm>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
<dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.2</version> <scope>test</scope> </dependency> </dependencies>
<developers> <developer> <name>meethigher</name> <id>meethigher</id> <email>meethigher@qq.com</email> <url>https://meethigher.top</url> <organizationUrl>https://github.com/meethigher</organizationUrl> <roles> <role>Developer</role> </roles> <timezone>+8</timezone> </developer> </developers> <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-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> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <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> <plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.7</version> <extensions>true</extensions> <configuration> <serverId>ossrh</serverId> <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin> </plugins> </build> </project>
|
2.4 部署
运行
1
| mvn clean deploy -DskipTests=true
|
使用上面这个命令的前提是,需要在maven的settings.xml里面,额外添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <settings> <profiles> <profile> <id>ossrh</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <gpg.executable>gpg2</gpg.executable> <gpg.passphrase>the_pass_phrase</gpg.passphrase> </properties> </profile> </profiles> </settings>
|
上面这种方式,太麻烦,我不推荐。我自己的做法是直接在centos下运行命令
1
| mvn clean deploy -DskipTests=true -Dgpg.passphrase=你生成密钥时输入的密码
|
运行结果
同步结果如下图。
三、致谢参考
gpg(GnuPG)生成密钥时卡住在We need to generate a lot of random bytes_liuxin5521的博客-CSDN博客
如何上传自己的jar包到maven中央仓库(2021最新版)_Java鱼仔的博客-CSDN博客_jar包上传到maven仓库
我把自己的java库发布到了maven中央仓库,从此可以像Jackson、Spring的jar一样使用它了_程序员欣宸的博客-CSDN博客
使用 PGP 签名 - 中央存储库文档
Apache Maven - 中央存储库文档