Uploading to Maven Central with Gradle, preventing unknown property error
I am trying to get my first Gradle project uploaded to Maven Central. I followed the sonatum documentation for this and created a task uploadArchives
to generate 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 refers to two properties, "ossrhUsername" and "ossrhPassword", which I must define in gradle.properties
; however, if I don't have gradle.properties
these properties, the build will fail even for tasks not uploadArchives
.
$ gradlew test
Failed to get unknown property "ossrhUsername" for object of type org.gradle.api.publication.maven.internal.deployer.DefaultGroovyMavenDeployer.
I would like the build to be successful (besides the task uploadArchives
, of course) without having to define these properties in a file gradle.properties
.
How should I do it?
Wouldn't it be easier to just manage a separate pom.xml
download exclusively for Maven Central?
EDIT identified potential duplicate is a question of where to externalize the credentials. My question is how to ensure that the Gradle construct still runs successfully even though the credentials have not been externalized in gradle.properties
. I would like others to be able to clone the repo and execute Gradle without having to define OSSRH credential properties.
source to share
Instead of directly referencing the property, I passed the property name as a String to findProperty
, which fixed the error. The API documentation indicates that this method is "Incubation", but it was around 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'))
}
source to share