java maven项目源码_Java开发|Maven中央仓库注册以及发布Java项目

49ce2bc488b44a91751d8a4377abd1a2.png
  • 1. 前言

  • 2. 注册 maven(oss)账号

    • 2.1 注册 JIRA 账号

    • 2.2 创建 issue

  • 3. 安装并配置 GPG

    • 3.1 安装 GPG

    • 3.2 生成 GPG 密钥对

    • 3.3 上传 GPG 公钥

  • 4. 配置 Maven 的 setting.xml

  • 5. 配置项目的 pom.xml

  • 6. 发布 jar 包

  • 7. close 并 release

  • 8. 参考链接

1. 前言

Maven 中央仓库之中包罗万象,无数优秀的开源 jar 包在其中声名远扬,那里可以说是任何一个 Java 程序员心目中的神圣宝地。

在我们漫长的编码生涯当中,或许你也封装过一些构思灵巧、使用方便的代码库,并且在生产环境中经受了极大的考验,在一定程度上很好地提高了生产力。

无论为是了分享,还是为了炫技,或是为了让别人把你写的代码按在地上摩擦,你想要把自己的代码打成 jar 包供他人调用。

把代码打成 jar 包并分享出去的方法有很多种,例如:用 mvn install 命令把现成的 jar 包安装到本地的 maven 仓库,然后在项目的 pom.xml 文件中配置依赖。另一种方式是,上传 jar 包到 maven 私有仓库(Nexus),然后开放出公共空间来供他人使用,像我们经常用到的阿里云镜像仓库,以及 CDH 的官方仓库等等。而今天重点要介绍的方式是,发布本地 jar 包到 maven 中央仓库,这样所有的 Java 开发者都可以使用 Maven(SBT、Gradle)直接导入依赖。

2. 注册 maven(oss)账号

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

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

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

  • Apache Maven 3.6.3
  • macOS 10.15.2
  • jdk 1.8

2.1 注册 JIRA 账号

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

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

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

注册页面如下:

c829f627f897f18d84a412b9261c1818.png
sign up

2.2 创建 issue

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

创建链接:

https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134

填写如下信息:

a8fd9e6f3a0d8f149c689f2289137ccb.png
create project

创建 issue 时候需要填写:

  • 项目概要
  • 项目描述
  • group id 对应着项目(pom.xml)的 group ip
  • Project URL 项目所在的 GitHub 地址,该项目可以是私有项目地址
  • SCM url 项目所在的 GitHub 地址的.git 地址

由于时差,前一天创建 issue,第二天早上才会有回应。当 issue 的 status 变为 RESOLVED,我们就可以进行下一步操作了。

3a7289d30c8f244cb6bbf31900c12b91.png
issue

如下图是审核的互动信息,其目的就是为了审核你这个 GitHub 地址是你自己所属的,当前 issue 的状态为已解决时,就可以进行如下的操作了。

e5000cc524d49272acfb146fea27eae5.png
shenhe

3. 安装并配置 GPG

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

3.1 安装 GPG

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

windows 的下载地址:https://www.gpg4win.org

其具体安装过程可以参考:https://www.jianshu.com/p/923913d904fb

3.2 生成 GPG 密钥对

gpg --gen-key

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

3.3 上传 GPG 公钥

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

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys CAB4165C69B699D989D2A62BD74A11D3F9F41243(你的)

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

gpg --list-keys

/Users/kiwenlau/.gnupg/pubring.kbx
----------------------------------
pub   dsa2048 2010-08-19 [SC] [expires: 2020-06-15]
      85E38F69046B44C1EC9FB07B76D78F0500D026C4
uid           [ unknown] GPGTools Team 
sub   elg2048 2010-08-19 [E] [expires: 2020-06-15]
sub   rsa4096 2014-04-08 [S] [expires: 2024-01-02]
pub   rsa2048 2019-01-03 [SC] [expires: 2021-01-02]
      CAB4165C69B699D989D2A62BD74A11D3F9F41243
uid           [ultimate] kiwenlau 
sub   rsa2048 2019-01-03 [E] [expires: 2021-01-02]

4. 配置 Maven 的 setting.xml

setting.xml 为 Maven 的全局配置文件,在 Mac 上的位置为/你的 maven 安装目录/conf/settings.xml,我们需要将第 1 步配置的 Username 和 Password 添加到标签中,这样我们才能将 jar 包部署到 Sonatype OSSRH 仓库:

<servers>
    <server>
       <id>ossrhid>
       <username>leojieusername>
       <password>passswordpassword>
     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" encoding="UTF-8"?>
<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.0modelVersion>

    <groupId>com.github.CCweixiaogroupId>
    <artifactId>hbase-sdkartifactId>
    <packaging>pompackaging>
    <version>1.0.0version>

    <name>hbase-sdkname>
    <url>https://github.com/CCweixiao/hbase-sdk.giturl>
    <description>hbase orm框架description>

    <licenses>
        
        <license>
            <name>MIT Licensename>
            <url>http://www.opensource.org/licenses/mit-license.phpurl>
        license>
    licenses>

    <scm>
        
        <url>https://github.com/CCweixiao/hbase-sdkurl>
        <connection>https://github.com/CCweixiao/hbase-sdk.gitconnection>
    scm>

    <developers>
        
        <developer>
            <id>leojieid>
            <name>leojiename>
            <url>https://www.jielongping.comurl>
            <email>leohjie1314@gmail.comemail>
            <roles>
                <role>Developerrole>
            roles>
            <organization>bigdataorganization>
            <timezone>+8timezone>
        developer>
    developers>

    <modules>
        
        <module>spring-boot-starter-hbasemodule>
        <module>hbase-sdk-coremodule>
        <module>spring-boot-starter-hbase-examplemodule>
    modules>

    <properties>
        <compiler.version>1.8compiler.version>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        

        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring-boot-2.version>2.1.7.RELEASEspring-boot-2.version>
        <hbase.version>1.4.3hbase.version>
        <gson.version>2.8.5gson.version>
        <junit.version>4.9junit.version>
    properties>

    <repositories>
        <repository>
            <id>aliyunid>
            <name>Nexus Release Repositoryname>
            <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
        <repository>
            <id>clouderaid>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/url>
        repository>
        <repository>
            <id>snapshotid>
            <name>Maven Snapshot Repositoryname>
            <url>https://repository.apache.org/snapshotsurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
        <repository>
            <id>wso2-maven2-repositoryid>
            <name>WSO2 Maven2 Repositoryname>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/url>
        repository>
        <repository>
            <id>wso2.releasesid>
            <name>WSO2 internal Repositoryname>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/url>
            <releases>
                <enabled>trueenabled>
                <updatePolicy>dailyupdatePolicy>
                <checksumPolicy>ignorechecksumPolicy>
            releases>
        repository>
    repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.github.CCweixiaogroupId>
                <artifactId>hbase-sdk-coreartifactId>
                <version>${project.version}version>
            dependency>
            <dependency>
                <groupId>com.github.CCweixiaogroupId>
                <artifactId>spring-boot-starter-hbaseartifactId>
                <version>${project.version}version>
            dependency>
        dependencies>
    dependencyManagement>


    <profiles>
        <profile>
            <id>defaultid>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-source-pluginartifactId>
                        <version>2.2.1version>
                        <executions>
                            <execution>
                                <phase>packagephase>
                                <goals>
                                    <goal>jar-no-forkgoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-javadoc-pluginartifactId>
                        <version>2.9.1version>
                        <executions>
                            <execution>
                                <phase>packagephase>
                                <goals>
                                    <goal>jargoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                    <plugin>
                        <groupId>org.apache.maven.pluginsgroupId>
                        <artifactId>maven-gpg-pluginartifactId>
                        <version>1.6version>
                        <executions>
                            <execution>
                                <phase>verifyphase>
                                <goals>
                                    <goal>signgoal>
                                goals>
                            execution>
                        executions>
                    plugin>
                plugins>
            build>
            <distributionManagement>
                <snapshotRepository>
                    
                    <id>ossrhid>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/url>
                snapshotRepository>
                <repository>
                    <id>ossrhid>
                    <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/url>
                repository>
            distributionManagement>
        profile>
    profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>
        resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    
                    <version>3.5.1version>
                    <configuration>
                        <source>${java.version}source>
                        <target>${java.version}target>
                        <compilerVersion>${java.version}compilerVersion>
                        <encoding>${project.build.sourceEncoding}encoding>
                        <optimize>trueoptimize>
                        <maxmem>1024mmaxmem>
                        <fork>truefork>
                        <compilerArgs>
                            <arg>-Xlint:all,-serial,-patharg>
                        compilerArgs>
                    configuration>
                    <executions>
                        <execution>
                            <phase>compilephase>
                            <goals>
                                <goal>compilegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        pluginManagement>
    build>


project>

6. 发布 jar 包

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

mvn clean deploy -Dmaven.test.skip=true  -projects hbase-sdk-core,spring-boot-starter-hbase

我的项目hbase-sdk含有多个模块,仅需部署 hbase-sdk-core 和 spring-boot-starter-hbase ,因此使用-projects 选项来指定。

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

7. close 并 release

mvn clean deploy 命令执行成功,使用 JIRA 账号登陆:https://oss.sonatype.org/#stagingRepositories,就可以看到你所发布的jar包了:

不墙访问不了,为了演示,只能盗图了。

ab2920d3744ccff56da7e70f39fd3289.png
yanshi

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

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

02aeedceeebce39fe184bbbf51467b43.png
maven

8. 参考链接

  • https://www.cnblogs.com/fundebug/p/how-to-deploy-jar-to-maven-central-repository.html
  • https://www.jianshu.com/p/923913d904fb

据说,关注我的人都找到了对象?

f83704744ece9ffa1fe5c5e4e1d1af50.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值