Search code examples
kotlingradlegradle-kotlin-dslsonatype

Error publishing to Sonatype because "repositoryUrl" is null


I've been trying to publish a project to Sonatype's repository, but I got an error and I can't find the solution online.

I have the following publishing block on my build.gradle.kts file:

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            pom {
                name.set("Keen")
                description.set("A genetic algorithm framework for Kotlin")
                url.set("https://github.com/r8vnhill/keen")
                licenses {
                    license {
                        name.set("Attribution 4.0 International (CC BY 4.0)")
                        url.set("https://creativecommons.org/licenses/by/4.0/legalcode")
                    }
                }
            }
            groupId = "cl.ravenhill"
            artifactId = "keen"
            version = projectVersion
            from(components["java"])
        }
    }
    repositories {
        maven {
            name = "sonatype"
            if (version.toString().endsWith("SNAPSHOT")) {
                maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") {
                    name = "ossrh"
                    credentials(PasswordCredentials::class)
                }
            } else {
                maven("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") {
                    name = "ossrh"
                    credentials(PasswordCredentials::class)
                }
            }
        }
    }
}

And the error I've got:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishMavenJavaPublicationToSonatypeRepository'.
> Failed to publish publication 'mavenJava' to repository 'sonatype'
   > Cannot invoke "java.net.URI.getScheme()" because "repositoryUrl" is null

Any idea of what could be happening?


Solution

  • The repository called 'sonatype' doesn't have a URL.

    Assuming the version is a snapshot version, e.g. 1.2.3-SNAPSHOT, what you've written is

    repositories { 
      maven {
        name = "sonatype"
        
        maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") { 
          name = "ossrh"
          credentials(PasswordCredentials::class)
        } 
      }
    }
    

    This is also equivalent to

    repositories { 
      maven {
        name = "sonatype"
      }
      maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") { 
        name = "ossrh"
        credentials(PasswordCredentials::class)
      } 
    }
    

    So repository 'sonatype' has a name, but doesn't have a URL.

    Gradle will create a publication task for each defined repository, even if the definition isn't valid (which can't be known ahead of time). If you ran the Gradle task

    ./gradlew publishMavenJavaPublicationToOssrhRepository
    

    it would succeed.

    To correct the problem, remove the 'sonatype' repository. It would also be more clear if you defined the target URL in a separate variable.

    val ossrhRepositoryUrl = if (version.toString().endsWith("SNAPSHOT")) {
      "https://s01.oss.sonatype.org/content/repositories/snapshots/"
    } else {
      "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
    }
    
    publishing {
      repositories {
        maven(ossrhRepositoryUrl) {
          name = "ossrh"
          credentials(PasswordCredentials::class)
        }
      }
    }