之前开源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,然后同步他们到中央仓库。

准备阶段

  1. 注册一个JIRA账号:https://issues.sonatype.org/secure/Signup!default.jspa

  2. 创建一个工单:https://issues.sonatype.org/browse/OSSRH-17873 (只有当这个jira当处于Status:RESOLVED时,才可以向maven仓库提交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

参考地址

http://central.sonatype.org/pages/ossrh-guide.html

http://central.sonatype.org/pages/requirements.html

http://central.sonatype.org/pages/apache-maven.html

http://stackoverflow.com/questions/29885887/gpg-no-default-secret-key-error-using-maven

https://keyring.debian.org/creating-key.html

http://maven.apache.org/plugins/maven-gpg-plugin/usage.html

http://www.cnblogs.com/gaoxing/p/4359795.html

编译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/