如何向 maven 中央仓库提交jar包

发布时间: 更新时间: 总字数:1130 阅读时间:3m 作者:IP:上海 网址

之前开源 UEditor-for-aliyun-OSS 后,好多群友问是否能在 maven 仓库中下载,该文章主要介绍如何向 maven 中央仓库提交 jar 包。

简介

从来都是从中央仓库下载 jar,这次需要向中央仓库提交 jar, 利用 Sonatype OSSRH 可以把 jar 等资源提交给 Maven 的中央仓库。

Sonatype OSSRH 介绍

Sonatype OSSRH 使用 Nexus 为开源项目提供仓库管理服务,该仓库就是所谓 maven 的中央仓库,OSSRH 允许我们向 Maven 中央仓库提交二进制文件:

1. 提交(deploy)开发版本的二进制文件(snapshorts)

2. 阶段性的发布版本

3. 发布一个 release,然后同步他们到中央仓库。

准备阶段

注意:Sonatype 已于 2025 年 6 月 30 日退役旧版 OSSRH / JIRA 发布流程。新用户与项目应直接在 central.sonatype.com(Maven Central Portal)注册账号、创建并验证 namespace、生成发布 token。以下旧流程仅作历史记录:

  1. (旧版流程)注册一个 JIRA 账号:https://issues.sonatype.org/secure/Signup!default.jspa

  2. (旧版流程)创建一个工单:https://issues.sonatype.org/browse/OSSRH-17873 (旧版流程中只有当工单处于 Status:RESOLVED 时才可以向仓库提交 jar 包)

准备和配置

配置 pom.xml:

1. Javadoc and Sources

Jar 包必须能提供完成的 Javadoc and Sources 信息。Javadoc and Sources 的命名和 artifactId 和 version 有关,如下:

<groupId>cn.xiexianbin.ueditor</groupId>
<artifactId>UEditor-for-aliyun-OSS</artifactId>
<version>1.4.3</version>

生成的 jar 包为:

UEditor-for-aliyun-OSS-1.4.3-javadoc.jar
UEditor-for-aliyun-OSS-1.4.3-sources.jar

2. project name, description and url

<name>${project.groupId}:${project.artifactId}</name>
<description>Create By Xie Xianbin, Package for ueditor and aliyun's OSS.</description>
<url>https://github.com/xiexianbin/UEditor-for-aliyun-OSS</url>

3. 证书信息

<licenses>
    <license>
        <name>The Apache License, Version 2.0</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    </license>
</licenses>

4. 开发者信息

<developers>
    <developer>
        <name>xiexianbin</name>
        <email>me@xiexianbin.cn</email>
        <organization>xiexianbin</organization>
        <organizationUrl>http://www.xiexianbin.cn</organizationUrl>
    </developer>
</developers>

5. SCM 信息

<scm>
    <connection>scm:git:git://github.com/xiexianbin/UEditor-for-aliyun-OSS.git</connection>
    <developerConnection>scm:git:ssh://github.com/xiexianbin/UEditor-for-aliyun-OSS.git</developerConnection>
    <url>https://github.com/xiexianbin/UEditor-for-aliyun-OSS</url>
    <tag>1.4.3</tag>
</scm>

6. 完整的配置信息如下

<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>cn.xiexianbin.ueditor</groupId>
    <artifactId>UEditor-for-aliyun-OSS</artifactId>
    <version>1.4.3</version>
    <packaging>jar</packaging>

    <name>${project.groupId}:${project.artifactId}</name>
    <description>Create By Xie Xianbin, Package for ueditor and aliyun's OSS.</description>
    <url>https://github.com/xiexianbin/UEditor-for-aliyun-OSS</url>

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>xiexianbin</name>
            <email>me@xiexianbin.cn</email>
            <organization>xiexianbin</organization>
            <organizationUrl>http://www.xiexianbin.cn</organizationUrl>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:git://github.com/xiexianbin/UEditor-for-aliyun-OSS.git</connection>
        <developerConnection>scm:git:ssh://github.com/xiexianbin/UEditor-for-aliyun-OSS.git</developerConnection>
        <url>https://github.com/xiexianbin/UEditor-for-aliyun-OSS</url>
        <tag>1.4.3</tag>
    </scm>

...

</project>

配置 pom.xml 文件

1. distributionManagement and Authentication

In order to configure Maven to deploy to the OSSRH Nexus Repository Manager with the Nexus Staging Maven plugin you have to configure it like this

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

<build>
  <plugins>
    <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://oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

2. Javadoc and Sources Attachments

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>2.2.1</version>
      <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-javadoc-plugin</artifactId>
      <version>2.9.1</version>
      <executions>
        <execution>
          <id>attach-javadocs</id>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

3. GPG Signed Components

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-gpg-plugin</artifactId>
      <version>1.5</version>
      <executions>
        <execution>
          <id>sign-artifacts</id>
          <phase>verify</phase>
          <goals>
            <goal>sign</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

4. Nexus Staging Maven Plugin for Deployment and Release

<build>
  <plugins>
    ...
    <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://oss.sonatype.org/</nexusUrl>
         <autoReleaseAfterClose>true</autoReleaseAfterClose>
      </configuration>
    </plugin>
  </plugins>
</build>

配置 maven_home/conf/settings.xml

1. 配置 JIRA 信息

<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>your-jira-id</username>
      <password>your-jira-pwd</password>
    </server>
  </servers>
</settings>

2. 配置 GPG 签名

<settings>
  <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>
</settings>

3. 或配置 profile

<profiles>
   <profile>
    <id>release</id>
    <build>
        <plugins>
            <plugin>
               <groupId>org.sonatype.plugins</groupId>
               <artifactId>nexus-staging-maven-plugin</artifactId>
               <version>1.6.3</version>
               <extensions>true</extensions>
               <configuration>
                 <serverId>ossrh</serverId>
                 <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                 <autoReleaseAfterClose>true</autoReleaseAfterClose>
               </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-release-plugin</artifactId>
                 <version>2.5</version>
                 <configuration>
                   <autoVersionSubmodules>true</autoVersionSubmodules>
                   <useReleaseProfile>false</useReleaseProfile>
                   <releaseProfiles>release</releaseProfiles>
                   <goals>deploy</goals>
                 </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-gpg-plugin</artifactId>
               <version>1.5</version>
               <executions>
                 <execution>
                   <id>sign-artifacts</id>
                   <phase>verify</phase>
                   <goals>
                     <goal>sign</goal>
                   </goals>
                 </execution>
               </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <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-javadoc-plugin</artifactId>
                <version>2.9</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
         </plugins>
    </build>
  </profile>
</profiles>

发布到 maven 中央仓库

If your version is a release version (does not end in -SNAPSHOT) and with this setup in place, you can run a deployment to OSSRH and an automated release to the Central Repository with the usual:

mvn clean deploy

mvn clean deploy -Dfile.encoding=UTF-8 -Dmaven.test.skip=true -X

With the property autoReleaseAfterClose set to false you can manually inspect the staging repository in the Nexus Repository Manager and trigger a release of the staging repository later with

mvn nexus-staging:release

If you find something went wrong you can drop the staging repository with

mvn nexus-staging:drop

编译 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>
	      	<aggregate>true</aggregate>
	      	<charset>UTF-8</charset>
	      	<encoding>UTF-8</encoding>
	      	<docencoding>UTF-8</docencoding>
	      </configuration>
	      <executions>
	        <execution>
	          <id>attach-javadocs</id>
	          <goals>
	            <goal>javadoc</goal>
	          </goals>
	        </execution>
	      </executions>
	    </plugin>

maven jar 包查看

https://oss.sonatype.org/

F&Q

checking for updates from xxx 时间长

mvn complie package 时,卡在 checking for updates from xxx 很久的解决方法,使用 -o 参数,脱机模式 Maven 不查询远程存储库是否更新

参考

  1. http://central.sonatype.org/pages/ossrh-guide.html
  2. http://central.sonatype.org/pages/requirements.html
  3. http://central.sonatype.org/pages/apache-maven.html
  4. http://stackoverflow.com/questions/29885887/gpg-no-default-secret-key-error-using-maven
  5. https://keyring.debian.org/creating-key.html
  6. http://maven.apache.org/plugins/maven-gpg-plugin/usage.html
  7. http://www.cnblogs.com/gaoxing/p/4359795.html