Deploying to maven central with gradle maven plugin

When building new projects I generally want to install the bare minimum and then build. The defaults on Sonatype’s website for deploying to OSSRH with Gradle modify the build configuration and causes the build to fail if you don’t have credentials set. Building without credentials can be useful for projects which have other contributors who may not be involved in the signing or publishing of artifacts. This post shows what should be changed included in your file in order to do both.

Errors encountered

The upload task need to be modified to allow builds without any signing credentials to avoid these two errors by just doing less.

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

This error is because the ‘ossrhUsername’ variable is unset.

Cannot perform signing task ':signArchives' because it has no configured signatory

And this one is because the username/password is set but empty.

Complete Example build.gradle

// Apply Plugins
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'signing'

// Metadata Definition
group = "com.example.applications"
archivesBaseName = "example-application"
version = "1.4.7"

tasks.withType(JavaCompile) {
	options.encoding = 'UTF-8'

// Create Jar Files
task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from javadoc

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource

artifacts {
    archives javadocJar, sourcesJar

// Signing and upload
signing {
    required { gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives

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

      repository(url: "") {
        authentication(userName: findProperty('ossrhUsername'), password: findProperty('ossrhPassword'))

      snapshotRepository(url: "") {
        authentication(userName: findProperty('ossrhUsername'), password: findProperty('ossrhPassword'))

      pom.project {
        name 'Example Application'
        packaging 'jar'
        // optionally artifactId can be defined here  
        description 'A application used as an example on how to set up pushing  its components to the Central Repository.'
        url ''

        scm {
          connection 'scm:svn:'
          developerConnection 'scm:svn:'
          url ''

        licenses {
          license {
            name 'The Apache License, Version 2.0'
            url ''

        developers {
          developer {
            id 'jspaetzel'
            name 'John Spaetzel'
            email '[email protected]'

// Project Dependencies
repositories {

dependencies {}


When you are ready to upload you’ll also need the credentials for signing in a file.