Maven公共中央仓库发布自己的Jar包

Maven公共中央仓库发布自己的Jar包

流程概要

  1. 创建工单
  2. 配置环境和项目
  3. 发布jar包

一、创建工单

1. 注册账号

https://issues.sonatype.org/secure/Signup!default.jspa ,前往该地址注册一个 sonatype 账号
Maven公共中央仓库发布自己的Jar包_第1张图片

2. 创建工单

登录注册的账号,页面控制面板上方菜单,有一个新建按钮(由于语言设置的不同,你的界面可能是英文)。点击,创建一个工单,如下图:
Maven公共中央仓库发布自己的Jar包_第2张图片
如果你有自己的域名,可以写你自己域名。没有的话可以用github账号作为组织域名标识。假如你的github用户名为xxx, 项目名为simple,则可以这样填写:

项目:Community Support - Open Source Project Repository Hosting (OSSRH)
问题类型:New Project
Group Id:com.github.xxx
Project URL:https://github.com/xxx/simple
SCM url:https://github.com/xxx/simple.git

创建后进入自己的工单,如下所示:
Maven公共中央仓库发布自己的Jar包_第3张图片
我的工单已经被管理员审批通过,所以是已解决状态。没有审批过的是开放状态,需要等待管理员的审批,由于时区不同,东半球的白天,西半球却是黑夜,耐心等待即可。

二、配置环境和项目

1. 配置maven setting.xml

上传jar到公共的中央仓库,首先要在setting.xml加入一些配置,如下

  <servers>
    <server>
      <id>snapshotid>
      <username>sonatype用户名username>
      <password>sonatype密码password>
    server>
    <server>
      <id>releaseid>
      <username>sonatype用户名username>
      <password>sonatype密码password>
    server>
  servers>

2. 配置pom.xml

我把自己项目的pom.xml文件粘贴如下:


<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.0modelVersion>
    <groupId>com.github.rxyorgroupId>
    <artifactId>carp-parentartifactId>
    <version>1.0.0version>
    <packaging>pompackaging>
    <name>carpname>
    <description>carp is project which contains common utils ,redis delay job , distributed lock and
        so on!
    description>
    <url>https://github.com/rxyor/carpurl>
    
     <properties>
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        
        <maven-source-plugin.version>3.0.1maven-source-plugin.version>
        <maven-clean-plugin.version>3.0.0maven-clean-plugin.version>
        <maven-resources-plugin.version>3.0.2maven-resources-plugin.version>
        <maven-compiler-plugin.version>3.7.0maven-compiler-plugin.version>
        <maven-surefire-plugin.version>2.20.1maven-surefire-plugin.version>
        <maven-war-plugin.version>3.2.0maven-war-plugin.version>
        <maven-install-plugin.version>2.5.2maven-install-plugin.version>
        <maven-deploy-plugin.version>2.8.2maven-deploy-plugin.version>
        <maven-gpg-plugin.version>1.6maven-gpg-plugin.version>
    properties>

    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-source-pluginartifactId>
                <configuration>
                    <attach>trueattach>
                configuration>
                <executions>
                    <execution>
                        <phase>compilephase>
                        <goals>
                            <goal>jargoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-javadoc-pluginartifactId>
                <configuration>
                    <encoding>UTF-8encoding>
                    <charset>UTF-8charset>
                    <docencoding>UTF-8docencoding>
                configuration>
                <executions>
                    <execution>
                        <id>attach-javadocsid>
                        <phase>packagephase>
                        <goals>
                            <goal>jargoal>
                        goals>
                        <configuration>
                            <additionalJOption>-Xdoclint:noneadditionalJOption>
                        configuration>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
    build>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
            <distribution>repodistribution>
        license>
    licenses>
    <scm>
        <url>https://github.com/rxyor/carpurl>
        <connection>scm:git:https://github.com/rxyor/carp.gitconnection>
        <developerConnection>scm:git:https://github.com/rxyor/carpdeveloperConnection>
    scm>
    <developers>
        <developer>
            <name>rxyorname>
            <email>[email protected]email>
            <url>https://github.com/rxyorurl>
        developer>
    developers>
    <profiles>
        <profile>
            <id>releaseid>
            <activation>
                <jdk>[1.8,)jdk>
            activation>
            <properties>
                <additionalparam>-Xdoclint:noneadditionalparam>
            properties>
            <distributionManagement>
                <snapshotRepository>
                    
                    <id>snapshotid>
                    <url>https://oss.sonatype.org/content/repositories/snapshotsurl>
                snapshotRepository>
                <repository>
                    
                    <id>releaseid>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2url>
                repository>
            distributionManagement>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-source-pluginartifactId>
                        <executions>
                            <execution>
                                <id>releaseid>
                                <phase>packagephase>
                                <goals>
                                    <goal>jar-no-forkgoal>
                                goals>
                            execution>
                            <execution>
                                <id>snapshotid>
                                <phase>packagephase>
                                <goals>
                                    <goal>jar-no-forkgoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-gpg-pluginartifactId>
                        <version>${maven-gpg-plugin.version}version>
                        <executions>
                            <execution>
                                <id>releaseid>
                                <phase>verifyphase>
                                <goals>
                                    <goal>signgoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                    <plugin>
                        <groupId>org.sonatype.pluginsgroupId>
                        <artifactId>nexus-staging-maven-pluginartifactId>
                        <extensions>trueextensions>
                        <configuration>
                            <serverId>releaseserverId>
                            <nexusUrl>https://oss.sonatype.org/nexusUrl>
                            <autoReleaseAfterClose>trueautoReleaseAfterClose>
                        configuration>
                    plugin>
                plugins>
            build>
        profile>
    profiles>
project>

3. pom.xml配置说明

上传到中央仓库并可以成功发布有以下前提条件:

  • 包含jar源码包
  • 包含java doc
  • 包含签名校验
  • 版本号包含snapshot的可能也无法发布成功
    等…

pom.xml配置的几个插件就是解决上述问题的,

  • maven-source-plugin用于生成源码包
  • maven-javadoc-plugin用于生成java doc,大部分代码的java doc注释都不规范,为了防止不规范的java doc注释导致打包失败,插件可以加入如下配置:
<configuration>
	<additionalJOption>-Xdoclint:noneadditionalJOption>
configuration>
  • maven-gpg-plugin用于添加签名

4. 安装并配置gpg

#安装gpg
brew install gpg
#生成key
gpg --gen-key
#输入对应信息
Real name: 
Email address: 

#查询生成的公钥的信息
gpg --list-keys

---------------------------------
pub   rsa2048 2019-05-30 [SC] [expires: 2021-05-29]
      B619ABB4B37B166061CD17352FCF6A922383XXXX
uid           [ultimate] rxyor <[email protected]>
sub   rsa2048 2019-05-30 [E] [expires: 2021-05-29]

#上传到密钥服务器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys B619ABB4B37B166061CD17352FCF6A922383XXXX
gpg: sending key 2FCF6A922383D13B to hkp://keyserver.ubuntu.com:11371

#查询密钥是否上传成功
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys B619ABB4B37B166061CD17352FCF6A922383XXXX
gpg: key 2FCF6A922383D13B: "rxyor " not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

三、发布jar包

1. deploy

工程根目录打开命令行工具,输入如下命令:

mvn clean deploy -P release -Darguments=gpg.passphrase="gpg密钥密码"

即使按照上述教程配置,你有可能遇到如下错误:

1) gpg签名错误

[INFO] --- maven-gpg-plugin:1.6:sign (release) @ carp-parent ---
gpg: signing failed: Inappropriate ioctl for device
gpg: signing failed: Inappropriate ioctl for device
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign (release) on project carp-parent: Exit code: 2 -> [Help 1]

如果发生错误,你的gpg可能需要额外的配置,以Mac为例,在gpg安装目录(~/.gnup)下建立两个配置文件:gpg.conf、gpg-agent.conf

# gpg.conf中加入
use-agent
pinentry-mode loopback
# gpg-agent.conf中加入
allow-loopback-pinentry

2) 400或401错误
请注意检查是否存在以下情况:

  • 发布到release时版本号不能包含snapshot
  • setting.xml 密码配置不对
  • setting.xml 中server id 与pom.xml配置不一致
  • maven环境变量配置setting.xml位置与自己配置的setting.xml不是同一个

解决上述错误后,再次执行上述命令,不出意外的话应该就可以deploy成功了。

2. Staging Repositories 关闭仓库操作

前往https://oss.sonatype.org/#stagingRepositories ,并使用sonatype账户登录, 按照下图所示关闭仓库
Maven公共中央仓库发布自己的Jar包_第4张图片
关闭仓库的时候需要检查是否出现错误,如果有错误需要按错误提示信息进行解决,如下图可以查看错误信息:
Maven公共中央仓库发布自己的Jar包_第5张图片
但实际上,由于pom.xml加入了插件:nexus-staging-maven-plugin,这插件在deploy成功后会自动帮你进行Staging Repositories Close操作。

3. 提醒管理员审批

关闭成功后,还需要在你的工单中提醒管理员你的组件发布成功,可以添加如下注释:

Component has been successfully issued

管理审批通过后,2个小时左右你就能在公共中央仓库搜索到发布的jar包了。

4. 中央仓库搜索发布的jar包

中央仓库搜索地址:https://search.maven.org/
Maven公共中央仓库发布自己的Jar包_第6张图片

你可能感兴趣的:(java)