When building new projects I generally want to install the bare mininum 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 dont 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 gradle.build 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 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: "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'))
      }

      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 'http://www.example.com/example-application'

        scm {
          connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
          developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
          url 'http://foo.googlecode.com/svn/trunk/'
        }

        licenses {
          license {
            name 'The Apache License, Version 2.0'
            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
          }
        }

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

// Project Dependencies
repositories {
    mavenCentral()
}

dependencies {}

Configuration

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

1
2
3
4
5
6
signing.keyId=YourKeyId
signing.password=YourPublicKeyPassword
signing.secretKeyRingFile=PathToYourKeyRingFile

ossrhUsername=your-jira-id
ossrhPassword=your-jira-password