将个人项目发布到mavan中央仓库

第一步,准备自己的git代码

比如在gitee或者github上的代码,我的是gitee码云上的,开源了一个处理业务日志采集的组件,支持注解方式,支持SpEL表达式,支持变量自定义。
话不多说,直接上我的项目地址:https://gitee.com/goudadong/logkit.git

第二步,注册sonatype账号

注册地址是:https://issues.sonatype.org/secure/Signup!default.jspa

填写注册信息,密码设置12位,非常复杂,恶心至极
image

创建之后登陆进去,新建一个issue
image

然后填写git的项目:
image

这是我最终填写后的结果:
image

填写完之后,后天会有人审核,由于本人没有域名,因此使用了他们指定的groupid:io.gitee.gouddong 作为项目的goupid,如果你有域名,那么就会做一个验证就可以了,

image

没有域名的情况下,他叫我在gitee上新建一个临时仓库,然后给他回复一下,已经创建好了,这个时候他就会进行审核,审核通过就会提示你可以将代码发布到 https://s01.oss.sonatype.org 上了。

第三步,配置maven

根据提示,访问https://central.sonatype.org/publish/publish-guide/#deployment 地址,
点击 Apache Maven
image

进去之后,往下翻,一直翻到:
image
打开maven setting文件,复制:

<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>用户名</username>
      <password>密码</password>
    </server>
  </servers>
</settings>

然后在自己的项目的pom.xml中配置发布地址和maven构建插件

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <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-deploy-plugin</artifactId>
                    <version>${maven.deploy.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <!-- Source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>package</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>2.9.1</version>
                <configuration>
                    <show>private</show>
                    <nohelp>true</nohelp>
                    <charset>UTF-8</charset>
                    <encoding>UTF-8</encoding>
                    <docencoding>UTF-8</docencoding>
                    <additionalparam>-Xdoclint:none</additionalparam>
                    <!-- 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <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>
            <!--sonatype-->
            <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>
            <!--flatten-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>${maven.flatten.version}</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>ossrh</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <gpg.executable>gpg</gpg.executable>
                <gpg.passphrase>the_pass_phrase</gpg.passphrase>
            </properties>
        </profile>
    </profiles>

并且在pom.xml中配置开发者、开源协议和项目的名称和描述以及url地址:

<name>logkit</name>
<description>业务日志采集组件</description>
<url>https://gitee.com/goudadong/logkit</url>
<licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <scm>
        <connection>scm:git:https://gitee.com/goudadong/logkit.git</connection>
        <developerConnection>scm:git:https://gitee.com/goudadong/logkit.git</developerConnection>
        <url>git:https://gitee.com/goudadong/logkit.git</url>
    </scm>
    <developers>
        <developer>
            <name>redwinter</name>
            <email>2360564660@qq.com</email>
        </developer>
    </developers>

第四步 配置PGP的公钥信息

Windows下使用gpg4win来进行配置。下载地址 https://www.gpg4win.org/get-gpg4win.html
本人是windows系统,所以下载windows版本的进行安装,安装完之后gpg这个命令会自动生效,打开cmd输入:gpg --list-keys 查看自己的gpg公钥:
image
刚开始没有,就创建一个,使用命令: gpg --gen-key 进行创建,然后根据提示设置姓名,邮箱等信息,最后会生成Passphase的密钥,然后提示输入一个密码,输入你的密码之后公钥key就生成成功, 这个密码你需要记住,是一个8位的密码,在maven发布签名的时候会用到,然后你是用gpg --list-keys 就可以查看到刚刚生成的公钥key。
接下来使用 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <你的公钥key> 公钥信息发送到ubuntu.com服务器,后续推送maven仓库会做校验。

第五步 depoy项目

使用mvn clean deploy -P <profile-id> 这里的profile-id是你maven配置的prifile的id值,刚开始配置的是release ,那么就填写release就好了,执行之后maven会去编译最终上传到 https://s01.oss.sonatype.org/ 仓库。
**注意: 如果在deploy时提示code 401,那么就是你的用户名或者密码没设置正确,如果提示reasonphrase: forbidden 那么可能就是你的pom 文件中distributionManagement 设置有问题,因为 https://central.sonatype.org/publish/publish-maven/ 提示在2021年开始 https://oss.sonatype.org/ 这个地址已经不能使用了,我刚开始就是url地址写的是 https://oss.sonatype.org//content/repositories/snapshots 所以死活传不上去 **

<distributionManagement>
  <snapshotRepository>
    <id>ossrh</id>
    <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
  </snapshotRepository>
  <repository>
    <id>ossrh</id>
    <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
  </repository>
</distributionManagement>
<repositories>
        <repository>
            <id>oss-releases</id>
            <url>https://s01.oss.sonatype.org/content/repositories/releases</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>oss-snapshots</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
</repositories>

第六步,查看是否发布成功

进入 https://s01.oss.sonatype.org/ 使用sonatype 的账号和密码登录,然后就可以搜索到你上传的项目了

image

posted @ 2021-12-22 22:36  玲丶蹊  阅读(140)  评论(0编辑  收藏  举报