【記錄】2021使用gradle發(fā)布到mavenCentral

背景:之前自己弄的一些基礎(chǔ)庫,示例代碼 都是在github托管,個別會發(fā)布到j(luò)center上,最近看打開github 實(shí)在是太慢了就 想著遷移到 碼云 上托管吧,同時也該 重新搞搞自己的基礎(chǔ)庫,方便快速寫demo,同時發(fā)布到mavenCentral 上 (因?yàn)閖center 不維護(hù)了)

發(fā)布過程記錄如下:(參考: Android庫發(fā)布到Maven Central全攻略、 發(fā)布Android庫到MavenCentral教程
基本按上面的參考文章去做就好,只不過有些點(diǎn),文章沒提到,或者時間隔得久了,某些步驟有變

一、注冊
1、Maven Central是由sonatype運(yùn)營的,https://issues.sonatype.org注冊賬號
2、創(chuàng)建問題,按上面的文章進(jìn)行操作就好,直到問題狀態(tài)為關(guān)閉狀態(tài)

image.png

期間的交流如下
image.png

image.png

二、創(chuàng)建GPG秘鑰
1、因?yàn)間radle 上傳 的時候用到,我們就先來創(chuàng)建GPG秘鑰好了,我是只用了命令行去操作的,gpg --full-gen-key
2、加密方式選擇RSA and RSA,長度輸入4096,過期時間直接回車不用管,然后輸入一個user ID并且提供一個郵箱,我直接用的我sonatype的用戶名和郵箱。最后一步輸入'O',表示OK
3、之后會彈出一個對話框,讓輸入密碼。后面gradle用到
image.png

三、Gradle準(zhǔn)備

//publish-mavencentral.gradle
apply plugin: 'maven-publish'
apply plugin: 'signing'

task androidSourcesJar(type: Jar) {
    archiveClassifier.set("sources")
    from android.sourceSets.main.java.source

    exclude "**/R.class"
    exclude "**/BuildConfig.class"
}

Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream() ;
properties.load( inputStream )

ext {
    PUBLISH_GROUP_ID = 'com.gitee.zaiqiang231'
    PUBLISH_ARTIFACT_ID = 'base-lib'
    PUBLISH_VERSION = '1.1.5'
}

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

            // Two artifacts, the `aar` and the sources
            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
            artifact androidSourcesJar

            // Self-explanatory metadata for the most part
            pom {
                name = PUBLISH_ARTIFACT_ID
                description = 'base lib'
                // If your project has a dedicated site, use its URL here
                url = 'https://gitee.com/zaiqiang231'
                licenses {
                    license {
                        //協(xié)議類型,一般默認(rèn)Apache License2.0的話不用改:
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'zaiqiang'
                        name = 'zaiqiang'
                        email = '461804160@qq.com'
                    }
                }
                // Version control info, if you're using GitHub, follow the format as seen here
                scm {
                    //修改成你的Git地址:
                    connection = 'scm:git:gitee.com/zaiqiang231/BaseLib.git'
                    developerConnection = 'scm:git:ssh://gitee.com/zaiqiang231/BaseLib.git'
                    //分支地址:
                    url = 'https://gitee.com/zaiqiang231/BaseLib/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 {
                        println "--- dependency ${it.group} ${it.name} ${it.version}"
                        if(it.group != null && it.group.length() > 0){
                            println "添加 dependency ${it.group} ${it.name} ${it.version}"
                            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 = "baselib"

            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
}
//local.properties
signing.keyId=剛才獲取的秘鑰后8位,gpg --list-keys??梢圆榭磩倓偨⒌拿荑€
signing.password=秘鑰密碼
signing.secretKeyRingFile=/Users/wuyanqiang/secring.gpg
ossrhUsername=賬號
ossrhPassword=密碼

四、上傳
1、上傳 gpg (這一步應(yīng)該是 要的,一開始gradle 和這個key 沒有上傳總是報 403 拒絕,后面上傳完這個key 和 改了上面gradle里面的地址就成功了,我也不糾結(jié)了,主要是這個上傳key 找了幾個文章才解決了)命令操作如下:
上傳gpg http://www.itdecent.cn/p/c898c9082872

wuyanqiangdeMacBook-Pro:~ wuyanqiang$ gpg --keyserver keys.gnupg.net --send-keys DBCC9EBB38B960432204AC3E1617D07237F53108

gpg: sending key 1617D07237F53108 to hkp://hkps.pool.sks-keyservers.net

wuyanqiangdeMacBook-Pro:~ wuyanqiang$ gpg --keyserver keyserver.ubuntu.com --send-keys DBCC9EBB38B960432204AC3E1617D07237F53108

gpg: sending key 1617D07237F53108 to hkp://keyserver.ubuntu.com

2、gradle 上傳操作:
任務(wù):assemble
任務(wù):publishReleasePublicationToBaselibRepository
執(zhí)行完畢后,進(jìn)入https://oss.sonatype.org/ ,使用你的sonatype帳號和密碼登錄。
進(jìn)入左側(cè)的Staging Repositories 就能看到你上傳的東西了

3、先close, 再release。就算是發(fā)布成功了,第一次發(fā)布,回到之前建立的jira issue 回復(fù)一下,應(yīng)該就能加快同步了

到此 所有操作完畢

更新:
補(bǔ)充遇到的一個問題:發(fā)布后,依賴已發(fā)布的庫,發(fā)現(xiàn)庫內(nèi)的子依賴 沒有成功,遇到一個 依賴錯誤


image.png

image.png

發(fā)現(xiàn)其中有個依賴 有問題,排查發(fā)現(xiàn)是 api fileTree(dir: 'libs', include: ['*.jar']) 導(dǎo)致的,遍歷依賴時會有一個空依賴, 在上面的gradle 追加一段 過濾

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

                    project.configurations.implementation.allDependencies.each {
                        println "--- dependency ${it.group} ${it.name} ${it.version}"
                        if(it.group != null && it.group.length() > 0){
                            println "添加 dependency ${it.group} ${it.name} ${it.version}"
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容