將Gradle項目發佈到Maven Central庫中

本文主要介紹如何一個由gradle構建的項目部署到Maven Central.

網上大部分都是介紹如何將由maven構建的項目部署到Maven Central。與Gradle相關的比較少。

申請賬號

前往 sonatype申請賬號。

申請完,Create Issue。

按照這個模板填。

這一塊比較簡單,網上教程也比較多。

Create Issue結束後,官方會需要你證明你擁有相對應的domain。

證明有以下3個途徑:

  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 [email protected] referencing this issue from a ... email address

證明完畢之後,你就可以發佈包了。

你就可以做下面幾件事了:

  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:[email protected]:username/project.git'
                    developerConnection 'scm:git:[email protected]: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 '[email protected]'
                    }
                }
            }
        }
    }
}

編寫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中看到你的項目了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章