如何将开源项目发布到Maven中央仓库

若要将开源项目包发布到maven中央仓库,官方指南指出需要具备如下必要条件

  • 是releases版本
  • javadocsources
  • 经过GPG签名
  • 必要的POM信息
  • 开源包的坐标,即groupIdartifactId

那么如何发布呢?这里采用最便捷通用的一种方式,使用Sonatype提供的OSSRH(Open Source Software Repository Hosting )来进行上传。

sonatype

首先进入网站https://issues.sonatype.org/secure/Dashboard.jspa(自行创建账号登陆),然后创建一个issue,如下所示:

在这里插入图片描述

关于Group Id,可以填写自己的github地址,也可以填写自己的域名。在创建完成issue之后,等待工作人员的回复。如果是Group Id填写的是个人的域名,便会收到让我们去证明域名时自己的相关回复,只需要按照提示去完成相关操作,然后在issue下进行回复即可。如下所示:

在这里插入图片描述

gpg 安装

笔者用的是ubuntu系统,所以这里以在ubuntu上的操作示例。

直接包源下载gpg:

$ sudo apt install gpg

生成密钥(过程中会需要设置密码,后面发布包的过程需要该密码进行验证):

lazycece@ubuntu:~$ gpg --gen-key 
gpg (GnuPG) 2.2.4; Copyright (C) 2017 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Note: Use "gpg --full-generate-key" for a full featured key generation dialog.

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <[email protected]>"

真实姓名: lazycece
电子邮件地址: [email protected]
您选定了这个用户标识:
    “lazycece <[email protected]>”

Change (N)ame, (E)mail, or (O)kay/(Q)uit? o
我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
gpg: 密钥 ***************** 被标记为绝对信任
gpg: directory '/home/lazycece/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/lazycece/.gnupg/openpgp-revocs.d/********************************.rev'
公钥和私钥已经生成并经签名。
********************************
pub   rsa3072 2020-01-12 [SC] [有效至:2022-01-11]
      
uid                      lazycece <[email protected]>
sub   rsa3072 2020-01-12 [E] [有效至:2022-01-11]

可以使用如下命令查看公钥:

$ gpg --list-keys 

上传公钥到到公共的密钥服务器,这里用的时ubuntu的服务器:

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

配置maven

修改maven的setting.xml文件,加上sonatype的账号信息:

    <server>
      <id>ossrh</id>
      <username>username</username>
      <password>password</password>
    </server>

在项目的pom文件中添加必要的信息,详见官网https://central.sonatype.org/pages/requirements.html,除了这些必要信息便是配置发布环境和相关的maven插件,这里给出样例(可自行根据自己项目情况配置),如下所示:

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>${maven.gpg.plugin.version}</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <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>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin.version}</version>
                <configuration>
                    <skipTests>${maven.test.skip}</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>${maven.source.plugin.version}</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>${maven.javadoc.plugin.version}</version>
                <configuration>
                    <skip>${maven.javadoc.skip}</skip>
                    <additionalparam>-tag date</additionalparam>
                    <locale>en_US</locale>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>${maven.deploy.plugin.version}</version>
                <configuration>
                    <skip>${maven.deploy.skip}</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

发布包

发布包可以用maven命令,亦可以在idea上直接操作。在执行的过程中,需要进行gpg验证,输入安装gpg时设置的密码即可。当发布完成之后,前往sonatype仓库https://oss.sonatype.org,进行closerelease操作(SNAPSHOT版本包无此操作),相关界面如下所示:

在这里插入图片描述
release完包之后,2小时之后即可在maven中央仓库=http://repo1.maven.org/maven2搜索到自己的包。此时要做的便是去issue下回复发布结果,然后关闭issue。

当然我们可以直接使用sonatype仓库来引用已发布成功的包,只需在项目pom中指定repositories即可,如下所示:

	<repositories>
		<repository>
			<id>sonatype</id>
			<name>sonatype</name>
			<url>https://oss.sonatype.org/content/groups/public</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>

猜你喜欢

转载自blog.csdn.net/qq_28851503/article/details/104468387