记录一次提交maven项目到中央仓库

文章来源原创   作者:临窗旋墨   发布时间:2021-12-23   阅读:3334   标签:maven 分类:maven 专题:解决方案

记录一次提交maven项目到中央仓库

建议以官网说明为准,网上教程存在一定的时效性。

临窗旋墨 2021-12

我的pom文件

1 注册

地址:https://issues.sonatype.org/projects
过程略

2 新建 Issue

Issue Type 为New Project

过程略 按部就班填写即可

但是可能审核不通过,可能是group Id 填写问题,参见:Choosing your Coordinates - The Central Repository Documentation (sonatype.org)

可以使用托管平台的子域

ServiceExample groupIdLink to the hosting service Documentation
GitHubio.github.myusernamehttps://pages.github.com/
GitLabio.gitlab.myusernamehttps://about.gitlab.com/stages-devops-lifecycle/pages/
Giteeio.gitee.myusernamehttps://gitee.com/help/articles/4136

比如使用Gitee,的则把groupId设置为io.gitee.myusername, 然后新建一个public的仓库名字为jira号的(注意大小写保持一致),用以验证此仓库属于本人。

待jira变成RESOLVED/已解决

3 配置settings.xml文件

参考官网★:Apache Maven - The Central Repository Documentation (sonatype.org)

  1. <server>
  2. <id>sonatype-nexus-snapshots</id>
  3. <username>Sonatype账号</username>
  4. <password>Sonatype密码</password>
  5. </server>
  6. <server>
  7. <id>sonatype-nexus-staging</id>
  8. <username>Sonatype账号</username>
  9. <password>Sonatype密码</password>
  10. </server>

4 修改项目pom.xml文件

参考官网★:Apache Maven - The Central Repository Documentation (sonatype.org)

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>io.gitee.xuqiudong</groupId>
  6. <artifactId>parent</artifactId>
  7. <version>1.0.1</version>
  8. <packaging>pom</packaging>
  9. <!-- 下列三项是需要的 -->
  10. <name>springboot项目的父项目</name>
  11. <description>springboot项目的父项目</description>
  12. <url>https://gitee.com/xuqiudong/vic-environment</url>
  13. <!-- 开源许可证声明 有很多不同的开源许可证 Apache Version2.0 表示别人可以直接商用你的开源项目而无任何版权问题 -->
  14. <licenses>
  15. <license>
  16. <name>The Apache Software License, Version 2.0</name>
  17. <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
  18. </license>
  19. </licenses>
  20. <!--开源地址 -->
  21. <scm>
  22. <connection>scm:git:ssh://git@gitee.com:xuqiudong/vic-environment.git</connection>
  23. <developerConnection>scm:git:https://gitee.com/xuqiudong/vic-environment.git</developerConnection>
  24. <url>https://gitee.com/xuqiudong/vic-environment</url>
  25. <tag>HEAD</tag>
  26. </scm>
  27. <!-- Issue地址 -->
  28. <issueManagement>
  29. <system>git Issue</system>
  30. <url>https://gitee.com/xuqiudong/vic-environment/issues</url>
  31. </issueManagement>
  32. <!-- 开发者信息 -->
  33. <developers>
  34. <developer>
  35. <name>vic.xu</name>
  36. <email>xuduochoua@163.com</email>
  37. <roles>
  38. <role>Developer</role>
  39. </roles>
  40. <timezone>+8</timezone>
  41. </developer>
  42. </developers>
  43. <properties>
  44. <!-- gpg可执行命令路径 -->
  45. <!--
  46. <gpg.executable>D:\tools\dev\GnuPG\bin\gpg</gpg.executable>
  47. <gpg.passphrase>12345678</gpg.passphrase>
  48. -->
  49. <vic.boot.version>1.0.0</vic.boot.version>
  50. <vic.group>io.gitee.xuqiudong</vic.group>
  51. <java.version>1.8</java.version>
  52. <project.encoding>UTF-8</project.encoding>
  53. <spring.boot.version>2.1.7.RELEASE</spring.boot.version>
  54. <javax.servlet>4.0.0</javax.servlet>
  55. <shiro-spring.version>1.6.0</shiro-spring.version>
  56. <jwt.auth0.version>3.8.1</jwt.auth0.version>
  57. <mybatis.starter.version>2.0.1</mybatis.starter.version>
  58. <pagehelper.starter.version>1.2.10</pagehelper.starter.version>
  59. <odbc.version>21.1.0.0</odbc.version>
  60. <!-- 插件版本 -->
  61. <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
  62. <maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
  63. <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
  64. <maven-javadoc-plugin.version>2.9.1</maven-javadoc-plugin.version>
  65. <mybatis-generator-maven-plugin.version>1.3.2</mybatis-generator-maven-plugin.version>
  66. <!-- 此版本被spring-boot使用 -->
  67. <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
  68. <!-- 打包时把源码打到本地 -->
  69. <maven-source-plugin.version>3.2.1</maven-source-plugin.version>
  70. <!-- copy resources -->
  71. <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
  72. <!-- spring boot -->
  73. <spring-boot-maven-plugin.version>2.4.0</spring-boot-maven-plugin.version>
  74. <elasticsearch.version>7.7.1</elasticsearch.version>
  75. </properties>
  76. <!-- Manage dependent versions of all sub-projects -->
  77. <dependencyManagement>
  78. </dependencyManagement>
  79. <!-- 打包到中央仓库的地址 -->
  80. <distributionManagement>
  81. <snapshotRepository>
  82. <id>ossrh</id>
  83. <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
  84. </snapshotRepository>
  85. <repository>
  86. <id>ossrh</id>
  87. <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
  88. </repository>
  89. </distributionManagement>
  90. <build>
  91. <pluginManagement>
  92. <plugins>
  93. <plugin>
  94. <groupId>org.springframework.boot</groupId>
  95. <artifactId>spring-boot-maven-plugin</artifactId>
  96. <version>${spring-boot-maven-plugin.version}</version>
  97. <executions>
  98. <execution>
  99. <goals>
  100. <goal>repackage</goal>
  101. </goals>
  102. </execution>
  103. </executions>
  104. </plugin>
  105. <!-- 指定jdk和编码 -->
  106. <plugin>
  107. <groupId>org.apache.maven.plugins</groupId>
  108. <artifactId>maven-compiler-plugin</artifactId>
  109. <version>${maven-compiler-plugin.version}</version>
  110. <configuration>
  111. <source>${java.version}</source>
  112. <target>${java.version}</target>
  113. <encoding>${project.encoding}</encoding>
  114. </configuration>
  115. </plugin>
  116. <!-- 跳过测试 -->
  117. <plugin>
  118. <groupId>org.apache.maven.plugins</groupId>
  119. <artifactId>maven-surefire-plugin</artifactId>
  120. <version>${maven-surefire-plugin.version}</version>
  121. <configuration>
  122. <skipTests>true</skipTests>
  123. </configuration>
  124. </plugin>
  125. <!--打包时 把源码打包到本地 -->
  126. <plugin>
  127. <groupId>org.apache.maven.plugins</groupId>
  128. <artifactId>maven-source-plugin</artifactId>
  129. <version>${maven-source-plugin.version}</version>
  130. <executions>
  131. <execution>
  132. <id>attach-sources</id>
  133. <phase>verify</phase>
  134. <goals>
  135. <goal>jar-no-fork</goal>
  136. </goals>
  137. </execution>
  138. </executions>
  139. </plugin>
  140. <!-- copy resources -->
  141. <plugin>
  142. <groupId>org.apache.maven.plugins</groupId>
  143. <artifactId>maven-resources-plugin</artifactId>
  144. <version>${maven-resources-plugin.version}</version>
  145. </plugin>
  146. <!--打jar插件 -->
  147. <plugin>
  148. <groupId>org.apache.maven.plugins</groupId>
  149. <artifactId>maven-jar-plugin</artifactId>
  150. <version>${maven-jar-plugin.version}</version>
  151. </plugin>
  152. <plugin>
  153. <groupId>org.apache.maven.plugins</groupId>
  154. <artifactId>maven-javadoc-plugin</artifactId>
  155. <version>${maven-javadoc-plugin.version}</version>
  156. <inherited>true</inherited>
  157. <executions>
  158. <execution>
  159. <id>bundle-sources</id>
  160. <phase>package</phase>
  161. <goals>
  162. <goal>jar</goal>
  163. </goals>
  164. </execution>
  165. </executions>
  166. <configuration>
  167. <source>${java.version}</source>
  168. <target>${java.version}</target>
  169. <maxmemory>1024</maxmemory>
  170. <encoding>UTF-8</encoding>
  171. <show>protected</show>
  172. <notree>true</notree>
  173. <!-- Avoid running into Java 8's very restrictive doclint issues -->
  174. <failOnError>true</failOnError>
  175. <!-- 防止文档不规范报错 -->
  176. <additionalparam>-Xdoclint:none</additionalparam>
  177. </configuration>
  178. </plugin>
  179. </plugins>
  180. </pluginManagement>
  181. <!-- 在此府项目中引入一些常用插件 -->
  182. <plugins>
  183. <plugin>
  184. <groupId>org.apache.maven.plugins</groupId>
  185. <artifactId>maven-compiler-plugin</artifactId>
  186. </plugin>
  187. <plugin>
  188. <groupId>org.apache.maven.plugins</groupId>
  189. <artifactId>maven-source-plugin</artifactId>
  190. </plugin>
  191. <plugin>
  192. <groupId>org.apache.maven.plugins</groupId>
  193. <artifactId>maven-javadoc-plugin</artifactId>
  194. </plugin>
  195. <plugin>
  196. <groupId>org.apache.maven.plugins</groupId>
  197. <artifactId>maven-surefire-plugin</artifactId>
  198. </plugin>
  199. </plugins>
  200. </build>
  201. <profiles>
  202. <profile>
  203. <!-- 上传的时候使用的配置和插件 -->
  204. <id>release</id>
  205. <build>
  206. <plugins>
  207. <plugin>
  208. <groupId>org.sonatype.plugins</groupId>
  209. <artifactId>nexus-staging-maven-plugin</artifactId>
  210. <version>1.6.7</version>
  211. <extensions>true</extensions>
  212. <configuration>
  213. <!-- 这里的id必须要和全局配置中的server一致 -->
  214. <serverId>ossrh</serverId>
  215. <!-- 这个地址,一定要和issue的评论中给出的地址一致! -->
  216. <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
  217. <!-- 如果希望发布后自动执行close和release操作,此处可以调整为true -->
  218. <autoReleaseAfterClose>false</autoReleaseAfterClose>
  219. </configuration>
  220. </plugin>
  221. <plugin>
  222. <groupId>org.apache.maven.plugins</groupId>
  223. <artifactId>maven-release-plugin</artifactId>
  224. <version>${maven-release-plugin.version}</version>
  225. <configuration>
  226. <autoVersionSubmodules>true</autoVersionSubmodules>
  227. <useReleaseProfile>false</useReleaseProfile>
  228. <!-- <releaseProfiles>release</releaseProfiles> -->
  229. <goals>deploy nexus-staging:release</goals>
  230. <checkModificationExcludes>
  231. <checkModificationExclude>pom.xml</checkModificationExclude>
  232. <checkModificationExclude>**</checkModificationExclude>
  233. </checkModificationExcludes>
  234. </configuration>
  235. </plugin>
  236. <!-- 11. 生成签名,确定使用那个gpg秘钥 -->
  237. <plugin>
  238. <groupId>org.apache.maven.plugins</groupId>
  239. <artifactId>maven-gpg-plugin</artifactId>
  240. <version>1.5</version>
  241. <executions>
  242. <execution>
  243. <id>sign-artifacts</id>
  244. <phase>verify</phase>
  245. <goals>
  246. <goal>sign</goal>
  247. </goals>
  248. <configuration>
  249. <keyname>vic.xu</keyname>
  250. <passphraseServerId>vic.xu</passphraseServerId>
  251. </configuration>
  252. </execution>
  253. </executions>
  254. </plugin>
  255. </plugins>
  256. </build>
  257. </profile>
  258. </profiles>
  259. </project>

5 配置pgp的公私钥信息

下载地址 :https://www.gpg4win.org/get-gpg4win.html

  1. 生成秘钥: 手动新建即可,

    使用 gpg --list-keys 命令查询配置好的公私钥信息

  2. 使用 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <公钥key> 将公钥信息发送到ubuntu.com服务器,后续推送maven仓库会做校验。

    以下几个地址都可以试试

    gpg —keyserver hkp://keyserver.ubuntu.com:11371 —send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver keyserver.ubuntu.com —send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver keys.openpgp.org —send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver pgp.mit.edu —send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

  3. 查看公钥是否发送成功:

    以下几个地址都可以试试

    gpg —keyserver hkp://pool.sks-keyservers.net —recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver hkp://keyserver.ubuntu.com:80 —recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver keyserver.ubuntu.com —recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver keys.openpgp.org —recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

    gpg —keyserver pgp.mit.edu —recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

>
>

  1. gpg --keyserver hkp://pool.sks-keyservers.net:11371 --send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9
  2. gpg --keyserver hkp://pool.sks-keyservers.net:11371 --recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9
  3. gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9
  4. gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9
  5. gpg --keyserver hkp://keys.gnupg.net:11371 --send-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9
  6. gpg --keyserver hkp://keys.gnupg.net:11371 --recv-keys 2CEFDDED844F56AADB1B3B57222970C5E4026FA9

6 打包到中央仓库

mvn clean deploy -P release -X 使用releaseproifile 打包

我的pom文件

登陆到https://s01.oss.sonatype.org/ (jira里提供的仓库地址,网上的部分教程和它不一致) 可查询到上传的包

如果 <autoReleaseAfterClose>false</autoReleaseAfterClose> 则可以去手动发布一下,

不然就不需要。

注意看如下日志: 当上面的参数为true时:

[DEBUG] Repository was removed; stop watching it
[DEBUG] iogiteexuqiudong-1035 REMOVED

仓库的工作台是是看不到的,但是可以在如下地址看到( 30分钟后)

https://repo1.maven.org/maven2/{groupId},比如我的:

https://repo1.maven.org/maven2/io/gitee/xuqiudong/

总结:遇到的坑

1 groupId: 需要证明是domain的拥有者,所有后面改成了gitee的子域io.gitee.xuqiudong, 然后在gitee上建立一个仓库,仓库名为jira号,注意大小写, 我复制jira号到gitee新建的时候,就自动变成了小写,结果无法通过。

2 . 一般的问题, 在deploy的时候加上-X 参数,如果出现错误,按部就班解决就可以了。比如javadocjava-source没有生成。还有就是可能公钥没有上传成功。


参考:

Apache Maven - The Central Repository Documentation (sonatype.org)

(记录一次将本地的Maven项目上传到中央仓库的步骤

我把自己的java库发布到了maven中央仓库,从此可以像Jackson、Spring的jar一样使用它了_程序员欣宸的博客-CSDN博客_java库发布


发表评论

目录