如何将JAR包发布到Maven中央仓库?

将jar包发布到Maven中央仓库(Maven Central Repository),这样所有的Java开发者都可以使用Maven直接导入依赖,例如weixin-java-mp

<!-- https://mvnrepository.com/artifact/com.gitee.wxtoos/weixin-java-mp -->
<dependency>
    <groupId>com.gitee.wxtoos</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>3.5.0</version>
</dependency>

但是,Maven中央仓库并不支持直接发布jar包。我们需要将jar包发布到一些指定的第三方Maven仓库,然后该仓库再将jar包同步到Maven中央仓库。

其中,最"简单"的方式是通过Sonatype OSSRH仓库来发布jar包。接下来,我会介绍如何将jar包发布到Sonatype OSSRH。

本教程所使用的系统配置如下:

  • OS:macOS 10.14.2
  • JDK:1.8.0_192
  • Maven:3.5.4

1. 注册JIRA账号

JIRA是一个项目管理服务,类似于国内的Teambition。Sonatype通过JIRA来管理OSSRH仓库。

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

需要填写Email, Full Name, Username以及password,其中Username与Password后面的步骤需要用到,请记下来。

2. 创建issue

通过在JIRA上创建issue来申请发布新的jar包,Sonatype的工作人员会进行审核,审核不算严格,一般按照要求填写不会有问题。

创建链接:https://issues.sonatype.org/browse/OSSRH-52362

创建issue的时候需要填写下面这些信息:

  • Summary
  • Description
  • Group Id
  • Project URL
  • SCM url

大家可以参考我申请发布 weixin-java-mp 时所填写的内容:OSSRH-52362

本次申请很快就通过了,可能会存在今天提交明天后天才会审核通过,但是审核并不是那么严格。

3. 安装并配置GPG

发布到Maven仓库中的所有文件都要使用GPG签名,以保障完整性。因此,我们需要在本地安装并配置GPG。

安装GPG

MacBook安装GPG非常简单,下载并安装GPG Suite即可。

生成GPG密钥对

gpg --gen-key

生成密钥时将需要输入name、email以及password。password在之后的步骤需要用到,请记下来。

上传GPG公钥

将公钥上传到公共的密钥服务器,这样其他人才可以通过公钥来验证jar包的完整性。

发送地址:hkp://keyserver.ubuntu.com:11371

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys CAB4165C69B699D989D2A62BD74A11D3F9F41243

其中CAB4165C69B699D989D2A62BD74A11D3F9F41243为密钥的ID,可以通过gpg --list-keys命令查看

4. 配置Maven的setting.xml

setting.xml为Maven的全局配置文件,在MacBook上的位置为/usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml,我们需要将第1步配置的Username和Password添加到<servers></servers>标签中,这样我们才能将jar包部署到Sonatype OSSRH仓库:不要填错哦

<servers>
        <server>
                <id>ossrh</id>
                <username>https://issues.sonatype.org/ 用户名</username>
                <password>https://issues.sonatype.org/ 密码</password>
        </server>
</servers>

5. 配置项目的pom.xml

pom.xml挺长的。根据Sonatype OSSRH的要求,以下信息都必须配置:

  • Supply Javadoc and Sources
  • Sign Files with GPG/PGP
  • Sufficient Metadata
    • Correct Coordinates
    • Project Name, Description and URL
    • License Information
    • Developer Information
    • SCM Information

配置时参考我的pom.xml,根据需要修改即可。

<?xml version="1.0"?>
<project
  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"
  xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gitee.wxtoos</groupId>
  <artifactId>wx-java</artifactId>
  <version>3.5.0</version>
  <packaging>pom</packaging>
  <name>WxJava - Weixin/Wechat Java SDK</name>
  <description>微信开发Java SDK</description>
  <url>https://gitee.com/wxToos/WxJava</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>GuoFeng</name>
      <email>136566468@qq.com</email>
      <url>https://gitee.com/wxToos/WxJava</url>
    </developer>
  </developers>

  <scm>
    <connection>scm:git:https://gitee.com/wxToos/WxJava.git</connection>
    <developerConnection>scm:git:git@gitee.com:wxToos/WxJava.git</developerConnection>
    <url>https://gitee.com/wxToos/WxJava</url>
  </scm>

  <modules>
  </modules>

  <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <httpclient.version>4.5</httpclient.version>
    <jetty.version>9.4.17.v20190418</jetty.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- 由于jodd-http较新的3.8版本需要jdk8,故而此处采用较低版本 -->
      <dependency>
        <groupId>org.jodd</groupId>
        <artifactId>jodd-http</artifactId>
        <version>3.7.1</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.7.0</version>
        <scope>provided</scope>
      </dependency>

      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>${httpclient.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
      </dependency>
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.5</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.24</version>
      </dependency>
      <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.11</version>
      </dependency>
      <!-- 由于guava较新的21.0版本需要jdk8,故而此处采用较低版本 -->
      <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
      </dependency>
      <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
      </dependency>

      <!-- 测试所用依赖 -->
      <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.7</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.11</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>3.0</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>${jetty.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>${jetty.version}</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-guava</artifactId>
        <version>3.0.0</version>
        <scope>test</scope>
      </dependency>

      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

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

  <profiles>
    <profile>
      <id>doclint-java8-disable</id>
      <activation>
        <jdk>[1.8,)</jdk>
      </activation>
      <properties>
        <javadoc.opts>-Xdoclint:none</javadoc.opts>
      </properties>
    </profile>

    <profile>
      <id>release</id>
      <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>
            <configuration>
              <additionalparam>${javadoc.opts}</additionalparam>
              <charset>UTF-8</charset>
              <locale>zh_CN</locale>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.6</version>
            <executions>
              <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                  <goal>sign</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.17</version>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <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.1</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-checkstyle-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <skip>true</skip>
          <configLocation>quality-checks/google_checks.xml</configLocation>
          <includeTestSourceDirectory>true</includeTestSourceDirectory>
          <consoleOutput>true</consoleOutput>
          <failsOnError>true</failsOnError>
        </configuration>
        <executions>
          <execution>
            <phase>verify</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

6. 发布jar包

执行mvn clean deploy处理,即可将jar包发布到Sonatype OSSRH仓库。

mvn clean deploy -P release

如果存在一个项目中含有多个模块的情况下可以指定命令来当个上传

mvn clean deploy -projects XXXX

第一次执行mvn clean deploy命令时,需要输入GPG密钥的密码。

mvn clean deploy -P release 命令执行成功并有success 出现时,说明已经成功了。

7. close并release

mvn clean deploy命令执行成功,使用JIRA账号登陆:https://oss.sonatype.org/#stagingRepositories,就可以看到你所发布的jar包了,登录的账户名密码和 https://issues.sonatype.org/ 上的账户名密码一致

选中对于的repository之后,点击箭头所指的close,close时会检查发布的构件是否符合要求。若符合要求,则close成功,成功之后点击箭头所指的release,即可正式将jar包发布到Sonatype OSSRH仓库。

release成功大概2个小时之后,该构件就会同步到Maven中央仓库

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值