前段时间使用Gitee仓库搭建了一个Maven私有仓库,将一些开源包放到上面去,感觉使用起来还是不太方便,最近就折腾将这些包提交到Maven的中央仓库中。项目第一次提交Maven还是挺麻烦的,所以写个文章Mark一下。
 注册一个 sonatype.org 帐号,登陆并提交一个issue,没错,就是提交一个issue,具体可参考如下: 
 
 
New Project
settings.xml  在Maven的 settings.xml 文件中增加 <server> 配置,配置你sonatype的账号密码,参考如下: 
<servers>
        <server>
            <id>sonatype-nexus-snapshots</id>
            <username>demo</username>
            <password>******</password>
        </server>
        <server>
            <id>sonatype-nexus-staging</id>
            <username>demo</username>
            <password>******</password>
        </server>
        
    </servers> 
 pom.xml license 、 scm 、 developer 信息。 <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>
        <tag>master</tag>
        <url>git@gitee.com:centy/xxl-job-spring-boot-starter.git</url>
        <connection>scm:git:git@gitee.com:centy/xxl-job-spring-boot-starter.git</connection>
        <developerConnection>scm:git:git@gitee.com:centy/xxl-job-spring-boot-starter.git</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>centychen</name>
            <email>292462859@qq.com</email>
        </developer>
    </developers> 
 profile 配置,注意: distributionManagement.snapshotRepository 和 distributionManagement.repository 的id需与 settings.xml 中对应的server记录ID一致;distributionManagement的url根据官方反馈的url修改。 <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- Source -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>3.0.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>3.1.0</version>
                        <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.5</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--Compiler-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.8.0</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <fork>true</fork>
                            <verbose>true</verbose>
                            <encoding>UTF-8</encoding>
                            <showWarnings>false</showWarnings>
                        </configuration>
                    </plugin>
                    <!--Release-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>2.5.3</version>
                    </plugin>
                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <id>sonatype-nexus-snapshots</id>
                    <name>Sonatype Nexus Snapshots</name>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>sonatype-nexus-staging</id>
                    <name>Nexus Release Repository</name>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles> 
 brew install gpg 执行安装; gpg --gen-key 生成公私钥,按照提示信息一步步操作,需要记住加密使用的 Passphrase ,下面步骤需使用; gpg --list-keys 查看公钥ID,通过一下命令上传: gpg --send-keys [公钥ID] --keyserver hkp://keyserver.ubuntu.com:11371
***-SNAPSHOT 格式,如 1.0.0-SNAPSHOT 。 mvn clean deploy -P release -Dmaven.test.skip=true
 Deploy的时候会弹出一个输入 Passphrase 的页面,输入刚才生成pgp公私钥使用的密码。 
 
构建完成后,在 https://oss.sonatype.org/content/repositories/snapshots 中应该可以找到刚刚提交的snapshot版本。
***-RELEASE 或者无后缀格式,如 1.0.0-RELEASE 、 1.0.0 。 mvn clean deploy -P release -Dmaven.test.skip=true
 Deploy的时候会弹出一个输入 Passphrase 的页面,输入刚才生成pgp公私钥使用的密码。 
 
 登陆 https://oss.sonatype.org/ ,点击左侧 Staging Repositories ,输入你的group id查找,可看到deploy记录: 
 
 选中Deploy记录点击 Close 并 Confirm ,刷新后会发现记录状态已经变成 Closed 。 
 再选中记录点击 Release 并 Confirm 完成发布,发布完成后需要等待中央仓库同步,我是等了1个多小时才能在中央仓库搜索出来。 
 使用 mvn clean deploy 命令构建时,可能会报 gpg: signing failed: Inappropriate ioctl for device ,是因为无法弹出Passphrase页面,需要在系统环境变量中增加 export GPG_TTY=$(tty) 。 
 如果Deploy的时候报 Access denied to staging repository... 等错误,恭喜你,你的帐号权限有问题,需要再提一个issue处理该问题,可参考我提的 issue 。 
应该是执行close操作的数据校验有问题,比如pom.xml信息缺失等,我第一次提交的时候就没有在pom.xml中配置project name,校验就没有通过。留意页面下方Activity中的错误信息即可。