Search code examples
mavengradle

Cannot publish to Maven central (keep getting 401)


Recently I was trying to publish to maven central, but without success.

I created an account using my github account, thus i now have a verified namespace io.github.smyrgeorge under namescpaces tab.

I also generated a User token in the View account page, so as a result I got something like:

<server>
    <id>${server}</id>
    <username>A_USERNAME</username>
    <password>A_PASSWORD</password>
</server>

Then from my project I tried to publish to maven, but without success.

I'm using gradle with the maven-publish plugin, my config is as follows:

publishing {
    repositories {
        maven {
            name = "OSSRH"
            url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
            credentials {
                username = "A_USERNAME"
                password = "A_PASSWORD"
            }
        }
    }

    publications {
        create<MavenPublication>("mavenJava") {
            from(components["java"])
            artifactId = tasks.jar.get().archiveBaseName.get()
        }
    }
}

So, I keep getting the following error:

What went wrong: Execution failed for task ':actor4k:publishMavenJavaPublicationToOSSRHRepository'. Failed to publish publication 'mavenJava' to repository 'OSSRH' Could not PUT 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/io/github/smyrgeorge/actor4k/0.1.0/actor4k-0.1.0.jar'. Received status code 401 from server: Unauthorized

I'm I missing something?


Solution

  • Recently I found another way.

    I managed to do it by using the com.vanniktech.maven.publish plugin.

    Details here: https://github.com/vanniktech/gradle-maven-publish-plugin.

    Documentation here: https://vanniktech.github.io/gradle-maven-publish-plugin/

    Tutorial: https://www.droidcon.com/2024/04/18/publishing-kotlin-multiplatform-libraries-with-sonatype-central/

    I'm using it in my project that I mentioned above: https://github.com/smyrgeorge/actor4k/blob/main/actor4k/build.gradle.kts

    Here is the build.gradle.kts file:

    import com.vanniktech.maven.publish.SonatypeHost
    import org.gradle.api.tasks.testing.logging.TestLogEvent
    import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    
    plugins {
        ...
        // https://github.com/vanniktech/gradle-maven-publish-plugin
        id("com.vanniktech.maven.publish") version "0.28.0"
    }
    
    ...
    
    group = rootProject.group
    version = rootProject.version
    
    mavenPublishing {
        coordinates(
            groupId = group as String,
            artifactId = name,
            version = version as String
        )
    
        pom {
            name = "actor4k"
            description = "A small actor system written in kotlin using Coroutines (kotlinx.coroutines)."
            url = "https://github.com/smyrgeorge/actor4k"
    
            licenses {
                license {
                    name = "MIT License"
                    url = "https://github.com/smyrgeorge/actor4k/blob/main/LICENSE"
                }
            }
    
            developers {
                developer {
                    id = "smyrgeorge"
                    name = "Yorgos S."
                    email = "smyrgoerge@gmail.com"
                    url = "https://smyrgeorge.github.io/"
                }
            }
    
            scm {
                url = "https://github.com/smyrgeorge/actor4k"
                connection = "scm:git:https://github.com/smyrgeorge/actor4k.git"
                developerConnection = "scm:git:git@github.com:smyrgeorge/actor4k.git"
            }
        }
    
        // Configure publishing to Maven Central
        publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
    
        // Enable GPG signing for all publications
        signAllPublications()
    }
    
    

    Also, keep in mind that you have to create a gradle.properties (DO NOT commit this file since it contains credentials, you have to append it to your .gitignore) file:

    mavenCentralUsername=<YOUR_CENTRAL_SONATYPE_TOKEN_USERNAME>
    mavenCentralPassword=<YOUR_CENTRAL_SONATYPE_TOKEN_PASSWORD>
    signing.keyId=<GPG_PUBLIC_KEY_ID>
    signing.password=<GPG_PASSPHRASE>
    signing.secretKeyRingFile=<HOME>/secring.gpg
    

    After all you simple need to run:

    ./gradlew publishAndReleaseToMavenCentral