将Gradle项目发布到Maven Central库中

本文主要介绍如何一个由gradle构建的项目部署到Maven Central.html

网上大部分都是介绍如何将由maven构建的项目部署到Maven Central。与Gradle相关的比较少。java

申请帐号

前往 sonatype申请帐号。git

申请完,Create Issue。github

按照这个模板填。apache

这一块比较简单,网上教程也比较多。windows

Create Issue结束后,官方会须要你证实你拥有相对应的domain。app

证实有如下3个途径:dom

  1. Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-44681 (Fastest)
  2. Setup a redirect to your Github page (if it does not already exist)
  3. Send an email to central@sonatype.com referencing this issue from a ... email address

证实完毕以后,你就能够发布包了。maven

你就能够作下面几件事了:gradle

  1. Deploy snapshot artifacts into repository https://oss.sonatype.org/cont...
  2. Deploy release artifacts into the staging repository https://oss.sonatype.org/serv...
  3. Promote staged artifacts into repository 'Releases'
  4. Download snapshot and release artifacts from group https://oss.sonatype.org/cont...
  5. Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/cont...

构建Gradle

下面主要内容基于 官方英文教程,加上一些我的构建时候的一些收获。

build.gralde 文件修改

引入plugin

apply plugin: 'maven'
apply plugin: 'signing'
task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from javadoc
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives javadocJar, sourcesJar
}

引入UploadArchives task

引入UploadArchives这个task,记住更改里面的我的相关信息。

其中有ossrhUsernameossrhPassword这两个变量是定义在gradle.properties中的。

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            pom.project {
                name 'Example Application'
                packaging 'jar'
                // optionally artifactId can be defined here 
                description 'A application used as an example on how to set up 
                pushing its components to the Central Repository . '
                url 'http://www.example.com/example-application'

                scm {
                    connection 'scm:git:git@github.com:username/project.git'
                    developerConnection 'scm:git:git@github.com:username/project.git'
                    url 'https://github.com/username/project'
                }

                licenses {
                    license {
                        name 'The Apache License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }

                developers {
                    developer {
                        id 'manfred'
                        name 'Manfred Moser'
                        email 'manfred@sonatype.com'
                    }
                }
            }
        }
    }
}

编写gradle.properties

主要是将一些认证信息填在这里。(这些信息不要加入到版本管理中)。

如下3个信息怎么来下一章节来说
signing.keyId=YourKeyId
signing.password=YourPublicKeyPassword
signing.secretKeyRingFile=PathToYourKeyRingFile

ossrhUsername=your-jira-id  你在sonatype申请的帐号的用户名
ossrhPassword=your-jira-password  你在sonatype申请的帐号的密码

生成GPG加密信息

windows中能够安装gpg4win来生成相关信息。可是我我的在windows10中并无可以打开。
因此我使用了WSL来生成相关信息。若是你的系统是Linux也能够。

  1. 执行gpg --gen-key, 按照提示的信息填入密码,用户名等信息,这些信息记录下来。这里填入的密码就是上面gradle.properties中的signing.password
  2. 执行gpg --list-keys, 能够看到

    /root/.gnupg/pubring.gpg
    pub   2048R/B98765 2018-12-08
    uid                  
    sub 2048R/A123456
  3. 第一行即是对应的公钥文件位置,pug后面的B98765即是public key Id,这个id也就是上面gradle.properties中的signing.keyId
  4. 执行 gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys B98765将公钥发送到hkp://pool.sks-keyservers.net
  5. 记录下/root/.gnupg/secring.png的位置,这个位置即是上面gradle.properties中的signing.secretKeyRingFile的值。

发布过程

当上述步骤所有完成时,能够直接执行gradle uploadArchives

发布Snapshot版本

若是你的版本是snapshot的,你能够直接在https://oss.sonatype.org/content/repositories/snapshots中看到你的包。

发布Release版本

clipboard.png

若是你的版本是release版本。
登陆https://oss.sonatype.org/#welcome,选择Staging Repositories,而后在右边用groupId去搜索。
这样会找到你的项目。选中你的项目close而后confirm。过一会再来寻找一次该构建,点击Release在Confirm。过一会就应该能在https://oss.sonatype.org/content/groups/public中看到你的项目了。

注意点

这里Close可能触发一些错误,能够点击下方的Activity来查看错误的缘由。通常来讲,多是并无javadoc和source致使的。

相关文章
相关标签/搜索