部署到Maven中央存储库

您需要使您的Java库公开访问吗? 您的项目托管在GitHub上吗? 您是否喜欢“将所有功能都部署到Maven Central Repository”按钮的想法? 我将展示如何使用maven-release-plugin进行设置 。 源代码托管在GitHub上,因此还将描述对源代码控制的SSH访问。

为项目设置环境需要采取各种步骤。 我遵循了SonaType官方指南中的许多步骤,因此在需要时会参考它。 但是,本指南还介绍了配置“部署到Maven Central存储库”按钮不需要的许多技术。

考虑

我的第一种方法是创建代表“部署到Maven Central”按钮的Jenkins作业。 对于我来说,这个想法比从我的开发机器推动来的要干净得多。 我几乎没有尝试过,但是当maven-release-plugin将版本更新推送到GitHub时,无法使Jenkins SSH Agent插件注册我的SSH身份。 因此,非常简单的Linux Bash脚本涉及两个步骤。

1.创建SonaType JIRA票证以注册您的groupId

SonaType驱动最大的Nexus存储库之一。 需要先在其中部署Java工件,然后才能将其同步到Maven Central存储库。 为了将Java库发布到SonaType Nexus存储库中,需要JIRA票证。 SonaType指南包含有关如何创建它的详细说明。

选择反映您的顶级域的Maven groupId。 它通常与Java主程序包相同(例如com.google ,而不是com.google.guava )。 这是因为您不想为您的域/主包/ groupId下的每个项目/库创建JIRA票证。 这里是有关Java包命名约定的更多信息。

2.从SonaType OSS pom.xml继承您的Maven工件

SonaType父POM包含

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

此父POM的存在很方便,因为否则您将需要在POM中指定所有插件。 唯一的问题是插件版本已过时,因此我决定以这种方式覆盖POM中的某些版本:

<pluginManagement>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-gpg-plugin</artifactId>
			<version>1.5</version>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-release-plugin</artifactId>
			<version>2.4.2</version>
		</plugin>
	</plugins>
</pluginManagement>

重要版本更新是GPG插件之一,因为更新版本具有有关GPG签名一节所述的便捷功能。

3.配置Maven工件的GPG签名

由于Maven Central存储库的安全策略,因此需要这样做。 包括各个子步骤:

<profiles>
  <profile>
    <id>sonatype-oss-release</id>
    <properties>
      <gpg.keyname>F21879F3</gpg.keyname>
      <gpg.passphrase>*********</gpg.passphrase>
      <gpg.defaultKeyring>false</gpg.defaultKeyring>
      <gpg.useagent>true</gpg.useagent>
      <gpg.lockMode>never</gpg.lockMode>
      <gpg.homedir>/home/lkrnac/.gnupg</gpg.homedir>
      <gpg.publicKeyring>/home/lkrnac/.gnupg/pubring.gpg</gpg.publicKeyring>
      <gpg.secretKeyring>/home/lkrnac/.gnupg/secring.gpg</gpg.secretKeyring>
    </properties>
  </profile>
</profiles>

F21879F3是我的公共GPG密钥ID。 gpg –list-keys命令将为您列出它。 Passphase是您的GPG专用密钥的密码。 .gnupg文件夹通常在您的主目录中生成。

4.设置SonaType Nexus存储库的凭据

需要推送到SonaType Nexus存储库。 与SonaType JIRA凭据相同。 再次在settings.xml中

<servers>
  <server>
    <id>sonatype-nexus-snapshots</id>
    <username>lkrnac</username>
    <password>*************</password>
  </server>
  <server>
    <id>sonatype-nexus-staging</id>
    <username>lkrnac</username>
    <password>*************</password>
  </server>
</servers>

6.设置对GitHub的SSH访问

请遵循以下子步骤:

7.创建“部署到Maven Central”按钮

Maven-release-plugin需要推送/签入版本更新到源代码控制系统并标记修订。 就我而言,代码托管在GitHub上。 因此,在调用maven-release-plugin的目标之前,我需要注册我的SSH身份(在上一步中生成)。 这是通过位于pom.xml旁边的bash脚本maven-central-deploy.sh完成的:

!/bin/bash
# Deploy maven artefact in current directory into Maven central repository 
# using maven-release-plugin goals

read -p "Really deploy to maven cetral repository  (yes/no)? "

if ( [ "$REPLY" == "yes" ] ) then
  ssh-add ~/.ssh/lubos.krnac
  ssh-add -l
  mvn release:clean release:prepare release:perform -B -e | tee maven-central-deploy.log
  ssh-add -D
else
  echo 'Exit without deploy'
fi

使脚本可执行

chmod +x maven-central-deploy.sh

8.按下“部署到Maven Central”按钮

赶紧跑

./maven-central-deploy.sh

确认并根据需要输入SSH私钥通行证。

9.通过SonaType Nexus存储库发布工件

将工件推送到SonaType Nexus存储库时。 您需要释放它 。 最后,您需要在开始创建的SonaType JIRA问题中添加评论,然后等到有人检查您的工件并设置与Maven Central的同步。 当然,JIRA部分仅需要完成一次。

链接

  • 以这种方式配置的示例项目托管在GitHub上 。 (这是很小的测试库)。

参考:Lubos Krnac Java博客博客上,从我们的JCG合作伙伴 Lubos Krnac 部署到Maven Central Repository

翻译自: https://www.javacodegeeks.com/2014/03/deploy-to-maven-central-repository.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值