如何提交项目到Maven中央仓库(图文详解)

sonatype申请账号

申请地址: https://issues.sonatype.org/secure/Signup!default.jspa

申请完账号之后,登录

点击上方create,在这里插入图片描述

创建item,Project选择Community Support - Open Source Project Repository Hosting (OSSRH)

如何提交项目到Maven中央仓库(图文详解)_第1张图片

Issue Type选择New Project

如何提交项目到Maven中央仓库(图文详解)_第2张图片

Summary自己填写

Group Id、Project URL、SCM url 按照下面填写

如何提交项目到Maven中央仓库(图文详解)_第3张图片

创建 Issue 后,等待审核即可。一般会在一个工作日内审核完成。当Issue的Status变为RESOLVED 或 FIXED 后,即可进行下一步操作。

GPG密钥安装

Windows 系统,可以下载 Gpg4win 软件来生成密钥对。下载地址:https://www.gpg4win.org/download.html

mac可以使用brew安装:

brew install gpg

gpg 常用命令

查看是否安装成功
gpg --version

生成密钥对
gpg --gen-key

查看公钥
gpg --list-keys

将公钥发布到PGP密钥服务器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 公钥ID

查询公钥是否发布成功
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys

创建秘钥并发布

  • 运行命令gpg --gen-key生成密钥对,按照提示输入真实姓名,邮箱等。然后生成密钥时候,会让你输入两次密码,这个密码要记住,后续会用到。
  • 运行命令gpg --list-keys ,查看本地密钥。
  • 运行命令gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 密钥ID,将发布密钥至密钥服务器

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>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.3.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.github.FeiChaoyugroupId>
    <artifactId>mybatis-layuiartifactId>
    <version>1.0.0version>
    <name>mybatis-layuiname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <mapper.starter.version>2.1.5mapper.starter.version>
        <pagehelper.starter.version>1.2.10pagehelper.starter.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.0version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.9version>
        dependency>
        
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>${mapper.starter.version}version>
        dependency>
        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>${pagehelper.starter.version}version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis.spring.bootgroupId>
                    <artifactId>mybatis-spring-boot-starterartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.3version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>8.0.15version>
                    dependency>
                    <dependency>
                        <groupId>tk.mybatisgroupId>
                        <artifactId>mapper-generatorartifactId>
                        <version>1.0.0version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

    

    <distributionManagement>
        <repository>
            <id>ossrhid>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/url>
        repository>
        <snapshotRepository>
            <id>ossrhid>
            <url>https://oss.sonatype.org/content/repositories/snapshotsurl>
        snapshotRepository>
    distributionManagement>


    <profiles>

        <profile>
            <id>releaseid>
            <build>
                <plugins>
                    
                    
                        
                        
                        
                        
                        
                            
                            
                            
                        
                    

                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-release-pluginartifactId>
                        <version>2.3.2version>
                        <configuration>
                            <autoVersionSubmodules>trueautoVersionSubmodules>
                            <useReleaseProfile>falseuseReleaseProfile>
                            <releaseProfiles>releasereleaseProfiles>
                            <goals>deploygoals>
                        configuration>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-compiler-pluginartifactId>
                        <version>3.0version>
                        <configuration>
                            <source>1.8source>
                            <target>1.8target>
                        configuration>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-gpg-pluginartifactId>
                        <version>1.5version>
                        <executions>
                            <execution>
                                <id>sign-artifactsid>
                                <phase>verifyphase>
                                <goals>
                                    <goal>signgoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-source-pluginartifactId>
                        <version>3.0.1version>
                        <executions>
                            <execution>
                                <id>attach-sourcesid>
                                <goals>
                                    <goal>jar-no-forkgoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-javadoc-pluginartifactId>
                        <version>3.0.0version>
                        <executions>
                            <execution>
                                <id>attach-javadocsid>
                                <phase>packagephase>
                                <goals>
                                    <goal>jargoal>
                                goals>
                                
                                <configuration>                
                                    <failOnError>falsefailOnError>
                                    <doclint>nonedoclint>
                                configuration>
                            execution>
                        executions>
                    plugin>


                plugins>
            build>
        profile>
    profiles>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
        license>
    licenses>

    <scm>
        <url>https://github.com/FeiChaoyu/mybatis-layuiurl>
        <connection>https://github.com/FeiChaoyu/mybatis-layui.gitconnection>
        <developerConnection>https://github.com/FeiChaoyudeveloperConnection>
    scm>

    <developers>
        <developer>
            <name>feichaoyuname>
            <email>[email protected]email>
            <url>https://github.com/FeiChaoyuurl>
        developer>
    developers>

project>

需要注意

你的全限定包名(保持和你之前sonatype上填写的Group Id一致)

如何提交项目到Maven中央仓库(图文详解)_第4张图片

ossrh这个id是需要和你的maven本地配置里的保持一致,下面会讲到。

还有就是Java doc的问题,请按照我上面的配置来,网上有用另一个方法的,但我试过了不行,这里我就不列举了。

本地maven settings.xml配置

在此之前需要先在本地装maven,然后到conf文件下找到settings.xml文件

在里面对应的地方加上

<server>
        <id>ossrhid>
        <username>你的sonatype账号username>
        <password>你的sonatype密码password>
server>

提交项目到OSS

打开IDEA的Maven插件,输入如下命令,用于修改版本号

mvn versions:set -DnewVersion=1.0.0

执行如下命令即可将依赖发布到中央仓库

mvn clean deploy -P release

当执行以上 Maven 命令时,会自动弹出一个对话框,需要输入上面提到的 Passphase,它就是刚才设置的 GPG 密钥库的密码。

等待一段时间后,就发布成功了。

如何提交项目到Maven中央仓库(图文详解)_第5张图片
发布完最好使用如下命令重置为SNAPSHOT版本

mvn versions:set -DnewVersion=1.0.1-SNAPHOST

在 OSS中发布

如果之前用了我上面给的pom.xml中配置的自动发布插件,那么就可以省略手动发布操作

<plugin>
    <groupId>org.sonatype.pluginsgroupId>
    <artifactId>nexus-staging-maven-pluginartifactId>
    <version>1.6.3version>
    <extensions>trueextensions>
    <configuration>
        <serverId>ossrhserverId>
        <nexusUrl>https://oss.sonatype.org/nexusUrl>
        <autoReleaseAfterClose>trueautoReleaseAfterClose>
    configuration>
plugin>

正常手动操作

进入https://oss.sonatype.org并登陆,会在左侧有个staging Repositories点击进入,在右侧面板找到你的构件,状态应该是open,你要将其置为closed,点击上方的close按钮即可。然后没问题后你需要点击release按钮发布你的构件。

在这里插入图片描述

Issue中通知工作人员已经成功发布

在这里插入图片描述

等待工作人员的回复。
之前由于发布时候用的SNAPSHOT版本,所以没有成功,工作人员回复我
在这里插入图片描述
之后如果成功了,就会出现
如何提交项目到Maven中央仓库(图文详解)_第6张图片
等10分钟就可以引入依赖了


    com.github.FeiChaoyu
    my-demo
    1.0.0

等2个小时就能在Maven仓库中搜到你的项目了
在这里插入图片描述

至此,整个提交流程结束!

你可能感兴趣的:(Maven)