将自己的项目作为jar包发布到maven中央仓库

2023-07-12,,

maven版本是3.5.0,jdk是1.8(注意,不是说项目是1.8就行,必须是环境变量里的也是,不能超过1.8,否则一大堆问题,执行mvn前用javac -version看下版本)

一:先在sonatype那里创建一个open source的issue后被审核通过,这个步骤网上的教程基本上一样可以百度;

二:安装gpg,并且先生成相关的key,然后上传到服务器;可看:https://blog.csdn.net/qq_38225558/article/details/94381467(注意,用powershell不要用cmd)

注意,这里上传时用的是key,而非用public key的id(用更短的那个 gpg key: xxxx)

三:配置setting.xml,主要是增加一个

<servers>
<!-- 上传jar包到maven中央仓库配置start -->
<server>
<id>ossrh</id>
<username>Sonatype账号</username>
<password>Sonatype密码</password>
</server>
<!-- 上传jar包到maven中央仓库配置end -->
</servers>

四:配置要提交到sonatype仓库的项目的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>
<groupId>com.gitee.silentdoer</groupId>
<artifactId>CommentManager</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>${project.groupId}:${project.artifactId}</name>
<description>The test uploads the jar to the maven central repository</description>
<url>https://gitee.com/silentdoer/CommentManager</url> <parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent> <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> <scm>
<connection>scm:git:git://gitee.com/silentdoer/CommentManager.git</connection>
<developerConnection>scm:git:git@gitee.com:silentdoer/CommentManager.git</developerConnection>
<url>https://gitee.com/silentdoer/CommentManager</url>
</scm> <developers>
<developer>
<name>silentdoer</name>
<email>1010993610@qq.com</email>
<url>https://gitee.com/silentdoer/CommentManager</url>
</developer>
</developers> <properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<!--<gpg.keyname>53FB91B916248F4D</gpg.keyname>
<gpg.passphrase>secret</gpg.passphrase>-->
<!--<maven.deploy.skip>true</maven.deploy.skip>-->
</properties> <!-- 【注】snapshotRepository 与 repository 中的 id 一定要与 setting.xml 中 server 的 id 保持一致! -->
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement> <profiles> <profile>
<id>ossrh</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</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>3.1.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles> </project>

五:在项目目录里执行(注意用powershell执行,之前用git的terminal执行死活成功不了)mvn clean deploy -P ossrh -Darguments="gpg.passphrase=password";(这里可能会提示要输入密码,后面的-Darguments=...可以不要,我这边貌似没什么用,还是要每次都输入密码)

注意上面的ossrh是profile的id;(经过测试其实用mvn clean deploy -P ossrh即可)

六:同步到Maven的中央仓库:

到:https://oss.sonatype.org/#stagingRepositories;【或者直接https://oss.sonatype.org,然后点击Staging Repositories】找到Staging Reopsitories,然后刷新后拉到最下面,最后一个就是自己提交的【也可以搜索】(注意每次提交新版本version要改下)

勾选它,点击左上角的close按钮。当项目状态变为closed之后,点击release按钮,输入必要的description信息后,就发布成功了。大约几小时后你就可以在中央仓库上搜索到你的项目了【比如:https://repo.maven.apache.org/maven2/】!(据说最长需要2小时)(也可以用Drop将上传的项目删除然后重新上传)

注意,这里close是可能失败的,系统会检测项目的合法性(主要是看有没有doc,soure,和asc这些文件);

还有就是可以不执行close-release操作,添加一个插件即可:

<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

自己的项目作为jar包发布到maven中央仓库的相关教程结束。

《将自己的项目作为jar包发布到maven中央仓库.doc》

下载本文的Word格式文档,以方便收藏与打印。