目前网上关于上传maven中央仓库的文章,不是发布时间太久,要不就是复制粘贴其他文章。没办法解决自己的问题,特写下这篇文章希望可以帮助到你。
提要说明:
系统:Mac 12.3.1
gpg:1.8.10
配置时间:2023年
主要流程分三步:
- 在sonatype申请账号,然后创建issue;
- 安装gpg秘钥管理工具,生成秘钥并上传到服务器;
- 配置pom.xml和setting.xml,然后上传中央仓库;
1.申请sontype账号
sonatype是一个通过Nexus对开源项目提供托管服务的网站
需要科学上网,没有账号就点击sign up 注册
注意:
项目选择 Community Support - Open Source Project Repository Hosting (OSSRH)
问题类型选择 New Project
GitHub做groupId,需要用io.github做前缀,不然不过审
Gitee做groupId,使用com.gitee
创建完等待审核。
2.安装gpg
Windows下载地址:点击跳转
Mac下载地址:点击跳转
安装上面gpg图形管理软件,创建、上传秘钥
或者使用命令安装:
Ubuntu可以使用如下命令进行安装:
sudo apt-get install gnupg
mac可以使用brew安装:
brew install gpg
gpg常用命令
gpg --version
检查安装成功没gpg --gen-key
生成密钥对gpg --list-keys
查看公钥gpg --keyserver hkp://pgp.mit.edu --send-keys 公钥ID
将公钥发布到 PGP 密钥服务器gpg --keyserver hkp://pgp.mit.edu --recv-keys 公钥ID
查询公钥是否发布成功
通过下面的命令生成密钥对,过程中需要输入一个密码,这个密码要记住
gpg --gen-key
通过下面的命令查看密钥,并将公钥上传到远程服务器。
gpg --list-key
gpg --keyserver hkp://pgp.mit.edu --send-keys 公钥ID
3.配置pom.xml和maven
3.1 pom配置
pom.xml 中必须包括:name、description、url、licenses、developers、scm 等基本信息
<scm>
<url>https://github.com/XimuTech/spore-spring-boot-starter</url>
<connection>https://github.com/XimuTech/spore-spring-boot-starter.git</connection>
</scm>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/XimuTech/spore-spring-boot-starter/issues</url>
</issueManagement>
<developers>
<developer>
<name>ximu</name>
<email>ryximu@qq.com</email>
</developer>
</developers>
<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>
<distributionManagement>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<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>
3.2 maven的setting.xml配置
需要添加sonatype的身份认证,就是你一开始申请账号时候的用户名和密码。id要和pom.xml中distributionManagement 中的snapshotRepository和repository保持一致。
<servers>
<server>
<id>ossrh</id>
<username>your username</username>
<password>your password</password>
</server>
</servers>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.keyname>your gpg username</gpg.keyname>
<gpg.passphrase>your gpg password</gpg.passphrase>
</properties>
</profile>
</profiles>
3.3 发布项目
运行下面命令进行发布
mvn clean install deploy -P release -Dgpg.passphrase=生成秘钥时候你的密码
执行成功后登录s01.oss.sonatype.org网站,输入你的账号密码,点击左侧的Staging Repositories ->在搜索栏输入你的 groupId
可能遇到的问题
1. 401, ReasonPhrase: Unauthorized. -> [Help 1]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] webporter-parent ................................... SUCCESS [ 11.690 s]
[INFO] webporter-core ..................................... SUCCESS [ 5.208 s]
[INFO] webporter-data-elasticsearch ....................... SUCCESS [ 2.769 s]
[INFO] webporter-collector-zhihu .......................... FAILURE [ 7.889 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28.069 s
[INFO] Finished at: 2017-05-11T18:11:44+08:00
[INFO] Final Memory: 45M/723M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy (injected-nexus-deploy) on project webporter-collector-zhihu: Failed to deploy artifacts: Could not transfer artifact com.github.brianway:webporter-data-elasticsearch:jar:javadoc:1.0-20170511.101142-1 from/to sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots/): Failed to transfer file: https://oss.sonatype.org/content/repositories/snapshots/com/github/brianway/webporter-data-elasticsearch/1.0-SNAPSHOT/webporter-data-elasticsearch-1.0-20170511.101142-1-javadoc.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :webporter-collector-zhihu
这个一般是秘钥不对导致的,处理办法是重新生成秘钥和上传验证。再试一次应该可以解决。
评论区