DEV Community

Discussion on: Deploying to OSSRH with Gradle in 2020

Collapse
 
ascertrobw profile image
RobWalker

Figured it out - nothing wrong in any of the above, but when using subprojects the 'version' gets evaluated for the root project not the subproject. Sorted it with the following:

ext {

...

    // Make sure we have a default for initial configuration evaluation
    isReleaseVersion = false    
}

allprojects {

   ...    

    // When it comes to publishing we need to ensure we have the subproject version set 
    // rather than the root project version
    afterEvaluate { project -> 
        ext.isReleaseVersion = !version.endsWith("SNAPSHOT")        
    }
}

...

    repositories {            
        maven {
            afterEvaluate { project -> 
                def releaseRepo = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
                def snapshotRepo = "https://oss.sonatype.org/content/repositories/snapshots/"
                url = (isReleaseVersion) ? releaseRepo : snapshotRepo                    
                credentials {
                    username = project.hasProperty('ossrhUsername') ? ossrhUsername : "Unknown user"
                    password = project.hasProperty('ossrhPassword') ? ossrhPassword : "Unknown password"
                }
            }
        }

...
signing {
    afterEvaluate { project -> 
        required { isReleaseVersion && gradle.taskGraph.hasTask("publish") }
        sign publishing.publications.mavenJava
    }
}