写了多年的代码,总有一些自己的积累,或者自己写的好的工具,想分享出去,那么你就需要把你的jar包发布到maven中央仓库,如何发布到maven中央仓库,参照以下步骤!
步骤
- 工单管理
- 注册Sonatype的账户。地址:https://issues.sonatype.org
- 创建工单
- 项目类型:Community Support - Open Source Project Repository Hosting
- groupId:
(1) 比如你申请 com.tool 那么你得拥有 tool.com 这个域名的所有权,他需要域名解析配置一个TXT记录。
(2) 如果你没有域名,还有一个解决办法,groupid可以申请以你项目托管的仓库来命名,比如:com.github.{账号名来定义}
(3) 创建成功后,接下来等待后台管理员审核,一般一个工作日以内,和管理员互动沟通,直至管理员审核通过即可。
- 构建仓库
- 生成GPG秘钥对
- 如果你使用的是windows系统,建议直接用git客户端的 Git Bash(注意:如果你不想后续包各种gpg的错,一定要用 Git Bash)
- 执行:gpg --gen-key(注意:过程中需要填写名字、邮箱等,其他步骤可以使用默认值,注意其中有个 Passphase 的参数需要记住,这个相当于是是密钥的密码,下一步发布过程中进行签名操作的时候会用到,以及每次上传中央仓库会用到)
- 将公钥发布到 PGP 密钥服务器:gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公钥ID
- 查询公钥是否发布成功:gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 公钥ID
- 常用GPG命令:
- gpg --version 检查安装成功没
- gpg --gen-key 生成密钥对
- gpg --list-keys 查看公钥
- gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公钥ID 将公钥发布到 PGP 密钥服务器
- gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 公钥ID 查询公钥是否发布成功
- 配置项目工程pom.xml
<pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.1</version> <configuration> <mavenExecutorId>forked-path</mavenExecutorId> <useReleaseProfile>false</useReleaseProfile> <arguments>${arguments} -Psonatype-oss-release</arguments> </configuration> </plugin> </plugins> </pluginManagement>
<scm> <tag>master</tag> <url>https://e.coding.net/tool.git</url> <connection>scm:git:https://e.coding.net/tool.git</connection> <developerConnection>scm:git:https://e.coding.net/tool.git</developerConnection> </scm>
<repositories> <repository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <distributionManagement> <snapshotRepository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>${sonatypeOssDistMgmtSnapshotsUrl}</url> </snapshotRepository> <repository> <id>sonatype-nexus-staging</id> <name>Nexus Release Repository</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> <profiles> <profile> <id>sonatype-oss-release</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</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.7</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> <configuration> <additionalparam>-Xdoclint:none</additionalparam> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> </license> </licenses> <developers> <developer> <name>tool</name> <email>tool@qq.com</email> <organization>tool</organization> </developer> </developers>
<properties> <arguments /> <sonatypeOssDistMgmtSnapshotsUrl>https://oss.sonatype.org/content/repositories/snapshots/</sonatypeOssDistMgmtSnapshotsUrl> </properties>
- 配置Maven setting.xml
<servers> <server> <id>sonatype-nexus-snapshots</id> <username>Sonatype 账号</username> <password>Sonatype 密码</password> </server> <server> <id>sonatype-nexus-staging</id> <username>Sonatype 账号</username> <password>Sonatype 密码</password> </server> </servers>
- 上传到构建仓库
mvn clean deploy -P sonatype-oss-release -Darguments="gpg.passphrase=秘钥密码"
- 在构建库中发布 release
- 进入 https://oss.sonatype.org/,点击”Staging Repositories” -> 勾选你的构件并点击 close -> 点击 tab 栏的 release
- 等待审批通过后,一小段时间(约1-2个小时)后即可同步到maven的中央仓库
- 发布成功后,通知 Sonatype 的工作人员关闭 issue
- 仓库镜像