401 Unauthorized when trying to publish artifacts to sonatype repository with new url with snapshots as well as release

18 views Asked by At

This is the content of my build.gradle.kts file

import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.bundling.Jar
import java.util.*

plugins {
    id("maven-publish")
    id("signing")
}

// Stub secrets to let the project sync and build without the publication values set up
ext["signing.keyId"] = null
ext["signing.password"] = null
ext["signing.secretKeyRingFile"] = null
ext["ossrhUsername"] = null
ext["ossrhPassword"] = null

// Grabbing secrets from local.properties file or from environment variables, which could be used on CI
val secretPropsFile = project.rootProject.file("local.properties")
if (secretPropsFile.exists()) {
    secretPropsFile.reader().use {
        Properties().apply { load(it) }
    }.onEach { (name, value) ->
        ext[name.toString()] = value
    }
} else {
    ext["signing.keyId"] = System.getenv("SIGNING_KEY_ID")
    ext["signing.password"] = System.getenv("SIGNING_PASSWORD")
    ext["signing.secretKeyRingFile"] = System.getenv("SIGNING_SECRET_KEY_RING_FILE")
    ext["ossrhUsername"] = System.getenv("OSSRH_USERNAME")
    ext["ossrhPassword"] = System.getenv("OSSRH_PASSWORD")
}

val javadocJar by tasks.registering(Jar::class) {
    archiveClassifier.set("javadoc")
}

fun getExtraString(name: String) = ext[name]?.toString()

publishing {
// Configure maven central repository
    repositories {
        maven {
            isAllowInsecureProtocol = true
            name = "sonatype"
            var repositoryUrl = ""
            repositoryUrl = if (version.toString().endsWith("SNAPSHOT")) {
                "https://s01.oss.sonatype.org/content/repositories/snapshots/"
            } else {
                "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            }

            url = uri(repositoryUrl)
            credentials {
                username = getExtraString("ossrhUsername")
                password = getExtraString("ossrhPassword")
            }
        }
    }

// Configure all publications
    publications.withType<MavenPublication> {
        // Stub javadoc.jar artifact
        artifact(javadocJar.get())

        // Provide artifacts information requited by Maven Central
        pom {
            name.set("Candlesticks Chart library")
            description.set("Candlesticks Chart Library for Kotlin Multiplatform to draw candle chart.")
            url.set("https://github.com/yashctn88/candlestickschartkmm")

            licenses {
                license {
                    name.set("MIT")
                    url.set("https://opensource.org/licenses/MIT")
                }
            }
            developers {
                developer {
                    id.set("yashctn88")
                    name.set("Yash Kalariya")
                    email.set("[email protected]")
                }
            }
            scm {
                url.set("https://github.com/yashctn88/candlestickschartkmm/tree/main")
            }
        }
    }
}

signing {
    if (getExtraString("signing.keyId") != null) {
        sign(publishing.publications)
    }
}

val signingTasks = tasks.withType()
tasks.withType().configureEach {
    mustRunAfter(signingTasks)
}

I have double verified the credentials and namespace and gpg keys. I am able to push with local host setup on the nexus repository manger local instance but same when i change the url and the username and token that i have obtained from the central.sonatype.org its failing with the 401 unauthorised code

Tried multiple times with this setup with local host setting nexus repo manager upload is working but when i set it to live server url its failing.

0

There are 0 answers