Android發(fā)布到maven central

準(zhǔn)備1

創(chuàng)建上傳空間并驗(yàn)證,用自己的github驗(yàn)證
https://central.sonatype.com/publishing/namespaces

準(zhǔn)備2

生成 GPG 密鑰(ai就行)

查看私鑰

gpg --list-secret-keys --keyid-format LONG

輸出結(jié)果

sec   rsa4096/ABCDEF1234567890 2024-01-01 [SC]

      1234567890ABCDEF1234567890ABCDEF1234

uid   [ultimate] Your Name <[you@email.com](mailto:you@email.com)>

生成簽名key

gpg --export-secret-keys -a ABCDEF1234567890 > signing.key

查看短key id

gpg --list-secret-keys --keyid-format SHORT
輸出結(jié)果

sec   rsa4096/F15BDBF5 2026-02-06 [SC]

      2439E7FADB65F3B96D997E9637D204E3F15BDBF5

uid           [ 絕對 ] xxxx <[xxxx@qq.com](mailto:xxxx@qq.com)>

ssb   rsa4096/F64AB908 2026-02-06 [E]

gradle.properties GPG配置

signing.keyId=短key id
signing.password=密碼
signing.gnupg.useLegacyGpg=true
signing.keyFile=生成簽名key地址

配置發(fā)版

項(xiàng)目根目錄 build.gradle.kts

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.android.library) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.kotlin.compose) apply false
    //增加插件
    id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
}

nexusPublishing {
    repositories {
        sonatype {
            // 發(fā)布到 Central Publishing Portal 的 staging endpoint
            nexusUrl.set(uri("https://central.sonatype.com/api/v1/maven/"))

            // 使用 Central Publishing Portal token
            username = project.findProperty("centralUsername")?.toString() ?: ""
            password = project.findProperty("centralPassword")?.toString() ?: ""
        }
    }

    // 所有子項(xiàng)目的 groupId
    packageGroup.set("nameSpace")
}

lib build.gradle.kts配置

android {
.......xxxxxxxxxxxx
 publishing {
        singleVariant("release") {
            withSourcesJar()
        }
    }
}

android同級增加

afterEvaluate {
    task javadocJar(type: Jar) {
        archiveClassifier = 'javadoc'
        from "$buildDir/javadoc"
    }

    publishing {
        publications {
            release(MavenPublication) {
                groupId = 'nameSpace.lib'
                artifactId = 'id'
                version = project.property("lib_websocket_version") ?: "1.0.0"
                from components.findByName("release")

                artifact javadocJar

                pom {
                    name = project.property("pom.name")
                    description = project.property("pom.description")
                    url = project.property("pom.url")

                    licenses {
                        license {
                            name = project.property("pom.license.name")
                            url = project.property("pom.license.url")
                        }
                    }

                    developers {
                        developer {
                            id = project.property("pom.developer.id")
                            name = project.property("pom.developer.name")
                            email = project.property("pom.developer.email")
                        }
                    }

                    scm {
                        connection = project.property("pom.scm.connection")
                        developerConnection = project.property("pom.scm.connection")
                        url = project.property("pom.scm.url")
                    }
                }
            }
        }
    }

    signing {
        required = true

        def keyFile = findProperty("signing.keyFile")
        def keyPass = findProperty("signing.password")
        def keyId   = findProperty("signing.keyId")

        if (!keyFile || !keyPass || !keyId) {
            throw new GradleException(
                    "PGP signing config missing: " +
                            "keyFile=${keyFile}, keyId=${keyId}"
            )
        }

        useInMemoryPgpKeys(
                keyId,
                file(keyFile).text,
                keyPass
        )

        sign publishing.publications.release
    }
}

項(xiàng)目根目錄 build.gradle.kts 增加配置

pom.name=test Library
pom.description=A high-performance library for Android based on Netty
pom.url=https://github.com/xxxx
pom.scm.url=https://github.com.xxxxx.git
pom.scm.connection=scm:git:https://github.com/xxxxx.git
pom.license.name=MIT License
pom.license.url=https://opensource.org/licenses/MIT
pom.developer.id=xxx
pom.developer.name=xx
pom.developer.email=xxx.com

# Library version
lib_websocket_version=1.0.0

centralUsername=xxx
centralPassword=xxx

發(fā)布到本地

image.png

本地發(fā)布路徑

~/.m2/repository/<groupId路徑>/<artifactId>/<version>/

依賴測試本地路徑 settings.gradle.kts

pluginManagement {
    repositories {
        gradlePluginPortal() 
        google()
        mavenCentral()
        mavenLocal() //增加
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        mavenLocal() //增加
    }
}

implementation("groupId:artifactId:version")

手動發(fā)布

https://central.sonatype.com/publishing/deployments
上傳 本地發(fā)布路徑所有文件

?著作權(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)容