Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Last active December 11, 2021 00:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save diegopacheco/13c0754f0ab36482f4f804ef3f05f989 to your computer and use it in GitHub Desktop.
Save diegopacheco/13c0754f0ab36482f4f804ef3f05f989 to your computer and use it in GitHub Desktop.
How to publish a JAR into Sonatype Maven Central REPO?

How to publish a jar into Sonatype central repo

Order of Operations

  1. Create gpg key
  2. Export gpg key
  3. Publish gpg key to ubuntu server
  4. Create account at Sonatype JIRA
  5. Configure $HOME/.gradle/settings.gradle user and pass and keys settings
  6. Configure gradle plugins
  7. Release

Create gpg key

You can see the keys with gpg -K if you dont have a published key into Ubuntu Key Server please do:

gpg --gen-key
gpg -K
cd $HOME/.gnupg
gpg --export-secret-keys -o secring.gpg
gpg --send-keys --keyserver keyserver.ubuntu.com <KEY ID>

Create Account in Sonatype JIRA

Now you need create an account an open a ticket to reserve a groupip.

Configure $HOME/.gradle/settings.gradle

edit Configure $HOME/.gradle/settings.gradle with the folloing content.

ossrhUsername=???
ossrhPassword=???
nexusUsername=???
nexusPassword=???

signing.keyId=???
signing.password=???
signing.secretKeyRingFile=???

signing.keyId and signing.password you need get from USER TOKEN from NEXUS UI: https://oss.sonatype.org/#profile;User Token. You need login there using same user/pass you got from jira ticket. The property signing.secretKeyRingFil should be something like $HOME/.gnupg/secring.gpg

Configure Gradle Plugins

build.gradle

apply plugin: "eclipse"
apply plugin: "java"
apply plugin: "signing"
apply plugin: "maven"

group = "YOUR_GROUP_ID"
archivesBaseName = "YOUR_ARTIFACT_NAME"
version = "YOUR_VERSION"

sourceCompatibility = 1.8
targetCompatibility = 1.8

sourceSets {
    main.java.srcDirs = ["src/main/java"]
}

repositories {
  maven { url 'http://repo.spring.io/libs-milestone' }
  maven { url 'http://repo.spring.io/libs-release' }
  mavenCentral()
  maven { url "https://oss.sonatype.org/content/groups/public/" }
  mavenLocal()
}

dependencies {
	compile([

    ])
    testCompile([

    ])
}

eclipse {
    classpath {
       downloadSources=true
    }
}

task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from javadoc
}

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

artifacts {
    archives javadocJar, sourcesJar
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.9.0"
    }
}

apply plugin: 'io.codearte.nexus-staging'

signing {
     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: ossrhUsername, password: ossrhPassword)
             }
             snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
             }

             pom.project {
                 name project.name
                 description project.description
                 packaging 'jar'
                 url 'https://github.com/YOU/YOUR_REPO'
                 scm {
                     connection 'scm:git:https://github.com/YOU/YOUR_REPO.git'
                     developerConnection 'scm:git:git@github.com:YOU/YOUR_REPO.git'
                     url 'https://github.com/YOU/YOUR_REPO.git'
                 }
                 licenses {
                     license {
                         name 'The Unlicense'
                         url 'http://unlicense.org/'
                         distribution 'repo'
                     }
                 }
                 developers {
                     developer {
                         id = 'YOUR_ID_ANY_SRING'
                         name = 'YOUR_NAME'
                         email = 'YOUR_EMAIL'
                     }
                 }
             }
         }
     }
}

nexusStaging {
  username = "${nexusUsername}"
  password = "${nexusPassword}"
  packageGroup = "${group}"
}

Release

./gradlew uploadArchives
./gradlew closeAndReleaseRepository

Goto: https://oss.sonatype.org/#staging-upload

Resources

@vlsi
Copy link

vlsi commented Mar 21, 2021

Central uses both keyservers: keyserver.ubuntu.com and pool.sks-keyservers.net

@NicklasWallgren
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment