Deploying to OSSRH with Gradle in 2020
Tags: java, gradle
Published: 2020-04-29 01:22:29

---
title: Deploying to OSSRH with Gradle in 2020
published: true
description: Explain a modern way to deploy artifacts to OSSRH
tags: java, gradle
---

It seems that the method described in [the official page](https://central.sonatype.org/pages/gradle.html) is a little bit old. Here I'll introduce a modern way.

## Metadata and Signing

Use [the `maven-publish` plugin](https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven) instead of [the `maven` plugin](https://docs.gradle.org/current/userguide/maven_plugin.html). No need to configure the `artifacts` extension.

## Jar Files

The `classifier` property of `Jar` task is now [deprecated](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:classifier). Instead, we can use [`withJavadocJar()` and `withSourcesJar()` in the `java` extension](https://docs.gradle.org/current/userguide/java_plugin.html#sec:java-extension).

```groovy
java {
    withJavadocJar()
    withSourcesJar()
}
```

## Signing artifacts

We're using `maven-publish` plugin, so the `signing` extension in our build script will be like the following:

```groovy
ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

// -------------------------------------
// here 'publishing' extension will come
// -------------------------------------

signing {
    sign publishing.publications.mavenJava
}
tasks.withType(Sign) {
    onlyIf { isReleaseVersion }
}
```

## Metadata Definition and Upload

Instead of `uploadArchives` extension for the `maven` plugin, configure `publishing` extension for the `maven-publish` plugin.

```groovy
publishing {
    repositories {
        maven {
            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"
            }
        }
    }

    publications {
        mavenJava(MavenPublication) {
            pom {
                groupId = 'com.example'
                name = 'example project'
                description = 'Example Project to learn how to deploy to OSSRH'
                url = 'https://example.com/'
                from components.java
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                scm {
                    connection = 'scm:git:git@github.com:example.com/example.git'
                    developerConnection = 'scm:git:git@github.com:example.com/example.git'
                    url = 'https://github.com/example.com/example/'
                }
            }
        }
    }
}
```

## Deployment

Use the `publishToMavenLocal` task to publish artifacts to maven local repository.

Use the `publish` task to publish artifacts to maven remote repositories. Don't forget to visit [oss.sonatype.org](https://oss.sonatype.org/) to close and release your staging repository.