Extremely Serious

Category: Repository

Publishing to Maven Central with Gradle

Pre-requisites

  1. Create an account to sonatype.
  2. Request for a group id from sonatype by using Jira to create an issue.
  3. Generate PGP keys to be used for signing the binaries (e.g. jar file). For windows, you can follow the procedure here.
  4. Wait for the completion of the issue you've created from item 2.

Gradle Configuration

  1. Update your gradle.properties to include the following properties:
    nexusUsername=<SONATYPE_USERNAME>
    nexusPassword=<SONATYPE_PASSWORD>
    
    signing.keyId=<PGP_PUBLIC_KEY_ID>
    signing.password=<PGP_PASS_PHRASE>
    signing.secretKeyRingFile=<PGP_EXPORTED_PRIVATE_KEY>
    
  2. In your build.gradle file,  add the Gradle Sonatype Nexus Plugin like the following:
    plugins {
       id "java"
       id "com.bmuschko.nexus" version "2.3.1" // Gradle Sonatype Nexus Plugin
    }
  3. Add the following plugin configurations:
    modifyPom {
        project {
            name '<PROJECT_NAME>'
            description '<PROJECT_DESCRIPTION>'
            url '<PROJECT_WEBSITE>'
            inceptionYear '<PROJECT_INCEPTION_YEAR>'
    
            scm {
                url '<PROJECT_SCM_ADDRESS>'
                connection '<PROJECT_SCM_ADDRESS>'
                developerConnection '<PROJECT_SCM_ADDRESS>'
            }
    
            licenses {
                license {
                    name '<PROJECT_LICENSE_NAME>'
                    url '<PROJECT_LICENSE_ADDRESS>'
                    distribution 'repo'
                }
            }
    
            developers {
                developer {
                    id '<DEVELOPER_ID>'
                    name '<DEVELOPER_NAME>'
                    email '<DEVELOPER_EMAIL>'
                }
            }
        }
    }
    
    extraArchive {
        sources = true
        tests = true
        javadoc = true
    }
    
    nexus {
        sign = true
        repositoryUrl = '<SONATYPE_RELEASE_REPOSITORY>'
        snapshotRepositoryUrl = '<SONATYPE_SNAPSHOT_REPOSITORY>'
    }
  4. Add the Gradle Nexus Staging plugin like the following:
    plugins {
        id 'java'
        id "com.bmuschko.nexus" version "2.3.1" // Gradle Sonatype Nexux Plugin
        id "io.codearte.nexus-staging" version "0.21.1" // Gradle Nexus Staging Plugin
    }
  5. After adding the plugin save the build.gradle file.
  6. Get the staging profile ID by running the following command:
    gradlew getStagingProfile
  7. Add the following plugin configuration:
    nexusStaging {
        stagingProfileId = "<STAGING_PROFILE_ID>"
    }
  8. Save the build.gradle file again.

Uploading to Sonatype Repository

Run the following command:

gradlew publishToSonatype

Publishing to Maven Central

Run the following command:

gradlew closeAndReleaseSonatypeStagingRepository

Errors in Publishing

If there are any errors after running the preceding command

  1. Sign in to the following address using your sonatype credentials: https://oss.sonatype.org/
  2. Click the Staging Repositories menu and investigate the error from there.
  3. Once the errors were identified and corrected locally, upload it again to sonatype repository before publishing it again.

Do this error correction process until all the errors were corrected.

Successful Publishing Validation

After around 10 minutes, navigate to your registered group id from sonatype, starting from the following address:

https://repo.maven.apache.org/maven2/

After around 2 hours, your artifact id may be searchable in maven central from the following address:

https://search.maven.org/