5

I'm trying to get my first Gradle project uploaded to Maven Central. I've followed Sonatype's documentation for this and have created an uploadArchives task for generating the metadata.

uploadArchives {
  repositories {
    mavenDeployer {
      beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

      repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
        authentication(userName: ossrhUsername, password: ossrhPassword)
      }

      snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
        authentication(userName: ossrhUsername, password: ossrhPassword)
      }
...etc. etc.

The task references two properties, "ossrhUsername" and "ossrhPassword", which I'm supposed to define in gradle.properties; however, if I do not have a gradle.properties with these properties, the build fails with an error, even for non-uploadArchives tasks.

$ gradlew test

Could not get unknown property 'ossrhUsername' for object of type org.gradle.api.publication.maven.internal.deployer.DefaultGroovyMavenDeployer.

I would like for the build to succeed (apart from the uploadArchives task, of course) without having to define these properties in a gradle.properties file.

How do I go about doing this?

Would it be simpler to just manage a separate pom.xml exclusively for Maven Central uploading?

EDIT The identified potential duplicate is about where to externalize credentials. My question is how to ensure the Gradle build still executes successfully despite the credentials not being externalized in gradle.properties. I would like for others to be able to clone the repo and execute Gradle without having to define OSSRH credential properties.

Community
  • 1
  • 1
bdkosher
  • 5,753
  • 2
  • 33
  • 40
  • Possible duplicate of [Where to put Gradle configuration (i.e. credentials) that should not be committed?](http://stackoverflow.com/questions/12749225/where-to-put-gradle-configuration-i-e-credentials-that-should-not-be-committe) – yonisha Apr 30 '17 at 19:59
  • 1
    Add an onlyIf predicate to your task, and check that the project has these properties before using them. See Project.hasProperty in the DSL documentation – JB Nizet Apr 30 '17 at 23:57
  • 1
    The `onlyIf` approach won't work, since it only affects the *execution phase*. The mentioned error already occurs in *configuration phase*. Using `findProperty` is the correct way. – Lukas Körfer May 03 '17 at 14:38
  • I went forward to use `gradle.properties` with empty values for all required keys. I put my personal keys into `~/.gradle/gradle.properties`, which overwrites project's local `gradle.properties`. – koppor Sep 10 '17 at 15:22

3 Answers3

7

Instead of referencing the property directly, I passed the property name as a String to findProperty, which eliminated the error. The API documentation indicates this method is "Incubating", but it's been around since version 2.13.

  repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
    authentication(userName: findProperty('ossrhUsername'), password: findProperty('ossrhPassword'))
  }

  snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
    authentication(userName: findProperty('ossrhUsername'), password: findProperty('ossrhPassword'))
  }
bdkosher
  • 5,753
  • 2
  • 33
  • 40
0

I just commented the user name password as it was not required and it worked

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 06:28
0

This is how I fixed the issue in gradle 7

repositories {
    maven {
        def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
        def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
        url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        credentials(PasswordCredentials) {
            username = hasProperty('ossrhUsername')?ossrhUsername:''
            password = hasProperty('ossrhPassword')?ossrhPassword:''
        }
    }
}    
xtian
  • 2,908
  • 2
  • 30
  • 43