Recently, I finished my pet-project and wanted to publish it to maven central. As I found out later, publishing is not a trivial thing. In this post, I’m going to show you all steps which you need to go through to publish your artifact.

My project is a scala wrapper for a very useful library testcontainers-java. As you may assume, it’s written in scala, but all the following is applicable for java/groovy/whatever language project if it’s built by gradle.

Preparation

First of all, you need to request a group id reservation. To do so, you need to sign up in Sonatype’s JIRA and create an issue where you’ll have to ask to approve your group id. This is how I did that.

GPG Keys

The next step is a creation of a gpg key. You need to install some additional utilities, on macos it looks like that brew install gpg gpg2.

Now, tet’s create a key.

gpg --gen-key

Pick default answers to the first two questions. Then enter two years as an expiration period. And fill out personal information.

Now you can list all your keys:

gpg2 --list-secret-keys

Copy a keyid of your key and distribute it as follows:

gpg2 --keyserver hkp://pool.sks-keyservers.net --send-keys [keyid]

A bit more details on this step are here.

Gradle script

Now we can touch the project. Here are changes which should be made to your build.gradle:

...
apply plugin: 'maven'
apply plugin: 'signing'

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

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

artifacts {
    archives javadocJar, sourcesJar
}

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 'testcontainers-scala'
                packaging 'jar'
                description 'Scala wrapper for testcontainers-java'
                url 'https://github.com/dimafeng/testcontainers-scala'

                scm {
                    connection 'scm:git:git@github.com:dimafeng/testcontainers-scala.git'
                    developerConnection 'scm:git:git@github.com:dimafeng/testcontainers-scala.git'
                    url 'git@github.com:dimafeng/testcontainers-scala.git'
                }

                licenses {
                    license {
                        name 'The MIT License (MIT)'
                        url 'https://opensource.org/licenses/MIT'
                    }
                }

                developers {
                    developer {
                        name 'Your name'
                        email 'your email'
                    }
                }
            }
        }
    }
}

This code allows publishing of releases and snapshots to oss.sonatype.org. You just need to adjust pom.project section to match your project information.

And to make it all work, we need to modify ~/.gradle/gradle.properties by adding a several properties:

signing.keyId=#keyId
signing.password=#yourPassword
signing.secretKeyRingFile=/Users/user/.gnupg/secring.gpg
ossrhUsername=#Sonatype's JIRA username
ossrhPassword=# password

More information on it is here.

Publishing

It seems it’s time to deploy your artifact. Make sure your work with non-snapshot version and then execute:

./gradlew uploadArchives

Once it’s done, you need to promote your release. To do so, you have to go to this page. In the table with repositories, you’ll see one that looks like your group id but without dots. Go in there and then click ‘Close’ in the top menu. Refresh the view in a few seconds and go to the ‘Activity’ tab - if you did everything correctly on the previous steps, you’ll see no errors and a button ‘Release’ will be enabled (it’s located to the right of ‘Close’ button), click on it and that’s pretty much it.

Enjoy having your own artifact in maven cental!