Maven系列(三)-- 将项目发布到MavenCentral

2023-03-15  本文已影响0人  nangongkuo

写在前面的话

这个坑挖了有两年多了,之前发布的是jcenter,可是一忙就给忘了写文章了,然后后面jcenter就关闭了,所以只能上传到MavenCentral了,发现还是有点麻烦的,所以就写了这篇文章,话不多说,开整

注册账号

1,首先选一注册一个Sonatype平台的账号,网址是(https://issues.sonatype.org/),下面是登录页面,点击注册:

image

2,注册的时候需要填入以下信息,即可完成注册:

image

创建问题(项目)

1,注册完成之后,需要新建一个项目,该项目用来保存你上传的库的版本和代码,该项目可以存储多个库,但是前提是这些库必须在同一个项目中,如我们很多时候在一个Project中有很多个module,每个module都会单独打包成一个aar供业务使用:

image

2,填写信息,以下是信息含义以及填写规范:

a,项目:选择“Community Support - Open Source Project Repository  Hosting (OSSRH)

b,问题类型:New Project

c,摘要:项目名称,如我的项目里只有一个库,是一个seekbar的test demo,那么我的项目名称叫“ZjfSeekbar”

d,描述:可以写上你对这个项目的描述,简单谢谢,非必填

e,附件:无

f,Group Id:重点就是这个Group Id,一般我们的项目都是开源的放在github上,那么我们就必须以我们的github的地址作为Group Id, 如我的github地址是“https://github.com/nangongkuo/”“io.github/nangongkuo”,那么我的Group Id是“io.github.nangongkuo”,**注意:不是com开头,而是以“****io****”开头,不然审核员会拒绝你的申请,让你重新修改,并且还得创建临时项目**,比较麻烦

g,Project Url:以你的github地址为准,上传之前你必须在github上面有一个项目,地址填上即可,如“https://github.com/nangongkuo/ZjfSeekbar”

h,SCM url:只需要在project url的后面加上“.git”即可,如“https://github.com/nangongkuo/ZjfSeekbar.git”

i,Already Synced to Central:选NO即可
image

3,然后点击创建,这中间可能遇到创建失败的问题,比如我刚才在第“步骤中第6步”的Group Id的设置中,如果用的不是“io”开头,而是以“com”开头的,就会遇到以下的错误:

image

意思就是github项目的Group Id必须以“io”开头,你需要重新在你的github里面创建一个名为指定名称的临时项目,我这里指定的临时名称为“OSSRH-89680”,以此来确定你对这个github账户拥有所有权,创建完毕之后,修改问题状态为“打开”即可,后面审核员会再次审核你的项目,等几分钟审核成功,就可以发布上传项目了,下面是审核成功的截图:


6.png

4,修改自动同步规则:因为这个Group Id是首次发布,发布项目后,要手动通知同步到Maven Central,将Already Synced to Central标记为Yes

image

创建GPG秘钥

发布之前需要先创建GPG秘钥,这就是MavenCentral比jecnter麻烦的地方,因为Sonatype平台对安全性有严格的要求

1,安装GPG:这里我使用的是Homebrew安装的

a,命令:brew install gpg

b,安装过程:
image

2,下载GPG管理客户端:

a,下载地址:https://gpgtools.org/,安装包如下
image
b,安装:安装完成之后会看到如下客户端,初始化的时候是展示的公钥,其中前两条是公钥,因为我这边私钥生成了两次,所以还会有私钥
image

3,检查安装:

a,查看版本命令: gpg -k ,可以看到如下版本信息
image

4,生成秘钥:

a,命令:gpg --full-generate-key

b,步骤:

    i,秘钥类型:1

    ii,RSA秘钥长度:4096

    iii,有效期限:0,永不过期

    iv,操作选择:O,确定

    v,弹窗:输入密码,这个密码是你的秘钥密码,红框中生成的pub后8位记得保留,我这里是“83C29AA6”,后面脚本里需要
image
    vi,保存秘钥:gpg --export-secret-keys -o secring.gpg

    vii,用“GPG客户端”对着刚才生成的私钥右击选择“Send Public Key to Key Server”将秘钥上传到服务端,然后就完成了秘钥的整个创建过程
image

MavenCentral上传脚本

1,脚本:在根目录下创建gradle的脚本“publish-mavencentral.gradle”:


apply plugin: 'maven-publish'

apply plugin: 'signing'

task androidSourcesJar(type: Jar) {

  classifier "sources"

  from android.sourceSets.main.java.source

  exclude "**/R.class"

  exclude "**/BuildConfig.class"

}

ext["signing.keyId"] = ''

ext["signing.password"] = ''

ext["signing.secretKeyRingFile"] = ''

ext["ossrhUsername"] = ''

ext["ossrhPassword"] = ''

File secretPropsFile = project.rootProject.file('local.properties')

if (secretPropsFile.exists()) {

  println "Found secret props file, loading props"

  Properties p = new Properties()

  p.load(new FileInputStream(secretPropsFile))

  p.each { name, value ->

    ext[name] = value

  }

} else {

  println "No props file, loading env vars"

}

publishing {

  publications {

    release(MavenPublication) {

      // The coordinates of the library, being set from variables that

      // we'll set up in a moment

      groupId PUBLISH_GROUP_ID

      artifactId PUBLISH_ARTIFACT_ID

      version PUBLISH_VERSION

      artifact androidSourcesJar

      // Two artifacts, the `aar` and the sources

      artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")

      // Self-explanatory metadata for the most part

      pom {

        name = PUBLISH_ARTIFACT_ID

        // 添加你的这个要上传的POM描述

        description = ' one custom seekbar'

        // If your project has a dedicated site, use its URL here

        url = 'https://github.com/nangongkuo/ZjfSeekbar'

        licenses {

          license {

            //协议类型,一般默认Apache License2.0的话不用改:

            name = 'The Apache License, Version 2.0'

            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'

          }

        }

        packaging = "aar"

        developers {

          developer {

            id = '邮箱'

            name = 'nangongkuo'

            email = '邮箱'

          }

        }

        // Version control info, if you're using GitHub, follow the format as seen here

        scm {

          //修改成你的Git地址:

          //          connection = 'scm:git:github.com/xxx/xxxx.git'

          //          developerConnection = 'scm:git:ssh://github.com/xxx/xxxx.git'

          connection = 'scm:git:github.com/nangongkuo/ZjfSeekbar'

          developerConnection = 'scm:git:ssh://github.com/nangongkuo/ZjfSeekbar'

          //分支地址:

          url = 'https://github.com/nangongkuo/ZjfSeekbar/tree/master'

        }

        // A slightly hacky fix so that your POM will include any transitive dependencies

        // that your library builds upon

        withXml {

          def dependenciesNode = asNode().appendNode('dependencies')

          project.configurations.implementation.allDependencies.each {

            def dependencyNode = dependenciesNode.appendNode('dependency')

            dependencyNode.appendNode('groupId', it.group)

            dependencyNode.appendNode('artifactId', it.name)

            dependencyNode.appendNode('version', it.version)

          }

        }

      }

    }

  }

  repositories {

    // The repository to publish to, Sonatype/MavenCentral

    maven {

      // This is an arbitrary name, you may also use "mavencentral" or

      // any other name that's descriptive for you

      name = "ZjfSeekbar"

      def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"

      def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"

      // You only need this if you want to publish snapshots, otherwise just set the URL

      // to the release repo directly

      url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

      // The username and password we've fetched earlier

      credentials {

        username ossrhUsername

        password ossrhPassword

      }

    }

  }

}

signing {

  sign publishing.publications

}

2,参数配置:然后针对变量,可以在local.properies中去添加


signing.keyId=83C29AA6

signing.password=

signing.secretKeyRingFile=/Users/zjf/gpg/secring.gpg

ossrhUsername= Sonatype平台注册的用户名

ossrhPassword=Sonatype平台注册的密码

3,gradle配置:找到你需要发布的modeul里的build.gradle文件,在尾部添加如下信息


ext {

  PUBLISH_GROUP_ID = "io.github.nangongkuo"  //项目包名 Group Id

  PUBLISH_ARTIFACT_ID = 'ZjfSeekbar'  //项目名 项目名称

  PUBLISH_VERSION = "1.0.0-ahpha12"    //版本号

}

apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"

发布项目到MavenCentral

1,生成aar:

a,如下图打开Android Studio的右侧的Gradle菜单,选择对应要发布的module下的Tasks->build->assemble

b,执行下图1处的assemble任务,就可以在下图2处看到生成的aar
image

2,发布aar到MavenCentral

a,在上图3处,执行名为“publishReleasePublicationToXXXRepository”(其中XXX为项目名,如我的就是“ZifSeekbar”)

b,上传成功之后进行下一步“Nexus Repository Manager”平台来进行上传管理操作

同步到公共仓库

1,同步仓库步骤:

a,打开“https://s01.oss.sonatype.org/”,进入到“Nexus Repository Manager”平台

b,选择左侧“Build Promotion”目录下的“Staging Repository”选项

c,点击右侧上部的“Refresh”按钮,就能在下面看到你自己的项目
image
d,选中该项目,点击上部操作栏的“close”按钮,close完毕之后,中间间隔十秒钟,然后再次点击“Refresh”按钮,这个时候该项目的状态就变成了close状态,上不操作栏的“Release”变成了可点击状态

e,点击“Release”按钮,等完成之后,该项目会在“Staging Repository”下面消失,过大概半小时左右就能在公共仓库里面看到该项目了

2,公共Public仓库查看该项目:

a,地址:格式为 repo1.maven.org/maven2/ + Group Id + 项目名称

    i,如:https://repo1.maven.org/maven2/io/github/nangongkuo/ZjfSeekbar/

b,示例:我的地址下的库
image

MavenCentral公共仓库依赖使用

1,依赖配置:

a,仓库配置:配置仓库mavenCentral()

b,依赖路径:implementation "io.github.nangongkuo:ZjfSeekbar:1.0.0-ahpha12@aar"

c,代码使用:

<com.zjf.seekbar.view.VHSeekBar

  android:id="@+id/fsb_vsb_ver_seek_bar"

  android:layout_width="match_parent"

  android:layout_height="28dp"

  android:layout_marginTop="100dp"

  android:paddingLeft="17.5dp"

  android:paddingStart="17.5dp"

  android:paddingRight="17.5dp"

  android:paddingEnd="17.5dp"

  app:vh_orientation="horizontal"

  app:vh_progress="0"

  app:vh_max_progress="100"

  app:vh_bg_color="@color/color_212121"

  app:vh_progress_color="@color/color_808080"

  app:vh_point_color="@color/color_white"

  app:vh_bg_radius="28dp"

  app:vh_progress_radius="24dp"

  app:vh_point_radius="16dp"

  app:vh_top_bg_space="24dp"/>

<com.zjf.seekbar.view.CircleShadowView

  android:id="@+id/seek_circle"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:visibility="gone"

  app:csv_bg_color="@color/color_white"

  app:csv_bg_diam="44dp"

  app:csv_text="0"

  app:csv_text_color="@color/color_333333"

  app:csv_text_size="18sp" />

2,大功告成:这样就能在你的项目中使用该库了,下面是我的Demo依赖的演示:

![image](https://img.haomeiwen.com/i329207/5dcda109de105147.gif?imageMogr2/auto-orient/strip)
上一篇 下一篇

猜你喜欢

热点阅读