Maven 中央仓库提交Jar包全程指南
本文记录一下将jar上传到maven中央仓库的全过程,文中项目依托在github上,使用的是mac环境 (关于maven、jdk的环境配置不属于本文内容)html
<!-- more -->java
首先咱们须要申请一个帐号,地址为: https://issues.sonatype.org/secure/Signup!default.jspagit
请记住这个帐号的用户名 + 密码,在后续的maven的setting.xml
配置文件中须要用到github
帐号申请完毕以后,点击新建
按钮(若是是由于的话,就是create
),提交一个issuemacos
Community Support - Open Source Project Repository Hosting (OSSRH)
New Project
com.github
,后面跟着的是你的帐号名,好比个人帐号是liuyueyi
,因此个人groupId是 com.github.liuyueyi
,若是不知足这个规则将没法经过后续的审核Project URL
: 项目地址,填对应的github链接 https://github.com/liuyueyi/quick-chinese-transferSCM URL
: 和上面的基本一致,只是多了一个.git
基本上须要配置的东西以下图,最后点击新建便可apache
上面提交以后,等待审核便可ubuntu
在后续的上传jar包时,须要利用gpg进行签名,下面介绍一下mac的安装流程vim
推荐用法segmentfault
macos安装能够借助homebrew来实现安全
brew install gpg
备选方案
可是个人mac系统比较老,使用上面的方式安装失败,直接抛了异常,根据搜索结果来看,不升级系统貌似没有什么好的解决办法
下面是采用安装包的方式,原则上建议到官网去下载安装包,依然是由于版本问题,最新的我也安装不上,因此找了一个历史的下载网址,(不保证这个网站上的安装包的安全性。虽然我本身用的也是它)
若有须要,能够跳转: https://sourceforge.net/p/gpgosx/docu/Download/
我选择的是2.2.12
版本,安装完毕以后,能够查看一下里面的readme
文件,查看具体的安装路径
好比在个人电脑上安装路径为: /usr/local/gnupg-2.2/bin
,为了方便使用,能够设置一下环境
vim ~/.bash_profile # 添加新的path路径 PATH=$PATH:/usr/local/gnupg-2.2/bin source ~/.bash_profile
密钥生成及发布
安装完毕以后,设置咱们本身的密钥
# 生成密钥对 # 输入用户名 + 邮箱,请记住这个密码,后面上传jar包的时候会用到 gpg --gen-key
查看本地密钥
# 生成完毕以后,查看本地密钥 gpg --list-keys
上图中勾住的就是咱们的公钥id,接下来将公钥id上传到密钥服务器
## 上传公钥 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 公钥ID ## 查看公钥上传状况 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 公钥ID
接下来,咱们须要设置一下咱们的maven配置文件setting.xml
,将咱们的用户信息填写进去
vim ~/.m2/setting.xml
添加第一步中申请的帐号信息,(用户名+密码就是第一步申请的帐号密码)
# 添加帐号信息 <servers> <server> <id>ossrh</id> <username>user</username> <password>password</password> </server> </servers>
前面的步骤属于大的环境相关,接下来就须要在咱们的实际项目中,配置必要的信息了,这里以https://github.com/liuyueyi/quick-chinese-transfer的配置为实例进行说明
<?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>com.github.liuyueyi</groupId> <artifactId>quick-chinese-transfer</artifactId> <packaging>pom</packaging> <version>0.1</version> <modules> <module>transfer-core</module> </modules> <name>quick-chinese-transfer</name> <description> A Java library supporting conversion between Simplified-Chinese, Traditional-Chinese </description> <url>https://github.com/liuyueyi/quick-chinese-transfer</url> <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> <issueManagement> <system>github</system> <url>https://github.com/liuyueyi/quick-chinese-transfer/issues</url> </issueManagement> <scm> <connection>scm:git:https://github.com/liuyueyi/quick-chinese-transfer.git</connection> <developerConnection>scm:git:https://github.com/liuyueyi/quick-chinese-transfer.git</developerConnection> <url>https://github.com/liuyueyi/quick-chinese-transfer</url> </scm> <developers> <developer> <name>YiHui</name> <email>bangzewu@126.com</email> <url>http://blog.hhui.top</url> </developer> </developers> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <configuration> <mavenExecutorId>forked-path</mavenExecutorId> <useReleaseProfile>false</useReleaseProfile> <arguments>-Psonatype-oss-release</arguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.1.0</version> <inherited>true</inherited> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <excludeResources>true</excludeResources> <useDefaultExcludes>true</useDefaultExcludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>3.0.0</version> <inherited>true</inherited> <executions> <execution> <id>bundle-sources</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <maxmemory>1024</maxmemory> <encoding>UTF-8</encoding> <show>protected</show> <notree>true</notree> <!-- Avoid running into Java 8's very restrictive doclint issues --> <failOnError>false</failOnError> <doclint>none</doclint> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> <configuration> <formats> <format>html</format> <format>xml</format> </formats> <check/> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> </plugins> </build> <distributionManagement> <repository> <id>ossrh</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> <snapshotRepository> <id>ossrh</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement> <profiles> <profile> <id>release</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>sign-artifacts</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
上面是一个完整的配置信息,其中,很是核心的几个点
groupId
: 请注意与申请的保持一致plugins
: 咱们上传的jar包,须要包含doc和源码,因此maven-source-plugin
+ maven-javadoc-plugin
必不可少maven-gpg-plugin
: 签名的插件,必要在个人实际项目开发过程当中,这里遇到了一个问题,maven-gpg-plugin
下载不下来一直标红,若是遇到这种问题,能够定向下载
mvn dependency:get -DrepoUrl=http://repo.maven.apache.org/maven2/ -Dartifact=org.apache.maven.plugins:maven-gpg-plugin:1.6
除此以外,还能够经过idea设置 -> maven -> Repositories
更新依赖
上面这个配置完毕以后,就是打包上传,直接使用如下命令便可
mvn clean deploy -DskipTests=true -P release
这个命令执行过程当中,会弹出一个输入gpg密码的弹窗,输入咱们第二步中生成gpg密钥时,填写的密码便可
jar包上传完毕以后,就能够在https://oss.sonatype.org/看到了
注意
当咱们第一步提交的issues审核以后,会有一个邮件通知你,能够发布对应的jar包了,也能够在issues看到下面的回复,通常有下面两步
接下来登陆 https://oss.sonatype.org/#stagingRepositories 管理咱们上传的jar包
Staging Repositories
close点击完毕以后,若是一切正常,那么等待一段时间以后,就能够发现release按钮能够点击了,而后点击release发布便可
若是一切顺利,咱们会收到一个邮件,告诉咱们发布成功,准备同步jar包了
而后等十来分钟,就能够直接依赖导入jar包了
<dependency> <groupId>com.github.liuyueyi</groupId> <artifactId>quick-transfer-core</artifactId> <version>0.1</version> </dependency>
注意
关于上面这个发布,有可能没有那么顺利,好比我以前遇到了几个问题,点击选中包的Activites
能够查看失败的缘由
上面几个问题的缘由主要在于项目的pom配置有问题,致使上传的包没有签名,没有source
, java-doc
其次还遇到过一次说是gpg密钥没有找到的问题,这个有多是由于咱们上传的密钥尚未同步过去,有延迟,再试一次就能够了
虽然网上挺多这种教程,可是在实际的操做中,总会遇到一些别人没有遇到的问题,固然若是没有遇到问题,那固然是最幸运的事情了;本文主要是为了记录jar包上传的中央仓库的全过程,作一个概括小结,也方便后续的查阅,固然若是对其余的小伙伴能有所帮助也是不错的
在写本文的时候,已经能够在中央仓库搜索到上传的jar包了
参考文档
一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛
尽信书则不如,以上内容,纯属一家之言,因我的能力有限,不免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激