第一次发布项目到maven中央库
Tag maven, 中央库, 项目发布, on by view 5924

昨天晚上决定将java版的gojapi发布到maven中央库上,于是多方查阅资料后开始动手,期间遇到不少问题,结果弄到很晚都没弄成功,直到今天上午才成功的将其上传成功。现记录一下。

用到软件:
maven
gpg4win-vanilla-2.2.3.exe

参考文档:
http://my.oschina.net/huangyong/blog/226738

密钥对:

# 生成密钥对
gpg --gen-key
# 查看公钥
gpg --list-keys
# 将公钥发布到 PGP 密钥服务器
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 82DC852E

具体操作见参考文档。

接下来就是修改配置和上传了,先不配置上传试试

mvn clean deploy

mvn_401_error.png
401错误,未授权。接下来在mvn/conf/setting.xml中填入用户名和密码。

<servers>
    <server>
        <id>oss</id>
        <username>duguying</username>
        <password>xxxxxxxx</password>
    </server>
</servers>

再次上传,只要pom.xml中的distributionManagement>repository>id与上面的id是一致的,便可上传成功。在oss管理的页面(https://oss.sonatype.org)close之后发现close失败。如下图

mvn_oss_close_failed.png
按照上面提示,源码包缺失,文档包缺失,加密缺失,etc。这时应该在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.0</modelVersion>
    <groupId>net.duguying.goj</groupId>
    <artifactId>gojapi</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>
	
	<name>goj judger client api</name>
    <url>https://github.com/gojudge/gojapi-java</url>
    <description>here is goj judger client api for java</description>
    <inceptionYear>2015</inceptionYear>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>

    <licenses>
        <license>
            <name>MIT License</name>
            <url>http://opensource.org/licenses/MIT</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <distributionManagement>
        <downloadUrl>https://oss.sonatype.org/content/groups/public</downloadUrl>
        <repository>
            <id>oss</id>
            <name>OSS Repository at sonatype</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
        </repository>
        <snapshotRepository>
            <id>oss-sonatype-snapshots</id>
            <name>OSS Snapshot Repository at sonatype</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

    <scm>
        <connection>scm:git:git://github.com/gojudge/gojapi-java.git</connection>
        <developerConnection>scm:git:git@github.com:gojudge/gojapi-java.git</developerConnection>
        <url>https://github.com/gojudge/gojapi-java</url>
        <tag>HEAD</tag>
    </scm>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <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>
                        <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>
                </plugins>
            </build>
            <distributionManagement>
                <snapshotRepository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </snapshotRepository>
                <repository>
                    <id>oss</id>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
                </repository>
            </distributionManagement>
        </profile>
    </profiles>

    <developers>
        <developer>
            <id>duguying</id>
            <name>duguying</name>
            <email>duguying2008@gmail.com</email>
        </developer>
    </developers>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>
		
		<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4</version>
        </dependency>
		
		<dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20131018</version>
        </dependency>
    </dependencies>

</project>

有了profile里面的内容便可以解决上面的问题,不过要想上传成功必须保证profiles>profile>distributionManagement>snapshotRepository>idprofiles>profile>distributionManagement>repository>id同样与setting.xml里面的server>id是一致的。否则就会报401错误。最后如下命令发布

mvn clean deploy -P release

这篇文章并没有详细的介绍如何将自己的java项目发布到maven中央库,而是描述了我发布的过程中遇到的问题,若想从头学习如何发布建议参考黄勇的博文 http://my.oschina.net/huangyong/blog/226738 。

maven 上传中可能遇到的一些问题

  1. gpg密钥丢失

    我换了一台电脑或者重装系统之后原来的 gpg 密钥已经不复存在了咋办。不用担心,你可以重新生成新的gpg密钥并上传,使用新的 gpg 密钥是完全没有问题的。

  2. gpg密钥上传后验证失败

    使用 --send-keys 上传了密钥之后,使用 --recv-keys 获取失败,或者偶尔成功偶尔失败,导致上传 maven 中央库后 close 操作后的 gpg 自动验证失败。此时你可以上传几个网络比较稳定的验证服务器作为备用服务器,例如 hkp://keyserver.ubuntu.com 。具体 maven 中央库支持哪些 pgp 服务器,可以从 gpg 验证报错信息中得出。