Uploading a java project to a maven repository

I’m looking for information on uploading my project to a maven repository. I don’t plan to add it soon, but I want to make some headway on doing so just so I understand any limitations code / structure wise.

Here’s my library so far, (the dev branch has the most recent stuff)
https://github.com/team-jgloom/jgloom/tree/dev

Is there any useful things I should know so far? I’ve heard horror stories on trying to upload to maven central which I’m trying to avoid.

Thanks! (Free internet points will be provided per any useful piece of advice)

If you choose oss.sonatype.org then please see: http://central.sonatype.org/pages/ossrh-guide.html
Since your project is based on Gradle, also see: http://yennicktrevels.com/blog/2013/10/11/automated-gradle-project-deployment-to-sonatype-oss-repository/

Sonatype is great for opensource projects. I already deploy to maven central through them, and I do use Gradle just like you. This consists of the following steps.

First get to the JIRA site, and create an account there. This is a normal registration that takes just a couple of minutes. From there, create a new ticket. As an example, here is mine that I made for WebGL4J: OSSRH-19459

It’d take some time, but it is usually fast. The next step is to configure your build script to push to the repositories.

First you’d need to apply the maven and signing plugins. These are standard plugins so there is no need for custom classpath.


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

group = "com.goharsha"
archivesBaseName = "webgl4j"
version = "0.2.9-SNAPSHOT"

Then you need to make sure that others are able to build even if they are lacking the keys locally. So define these properties right after the version variable.


// Check if the required properties are included, so we prevent our
// build from failing.
if (project.hasProperty('ossrhUsername'))
{
    project.ext.ossrhUsername = ossrhUsername
    project.ext.ossrhPassword = ossrhPassword
}
else
{
    // We might be running on another system where he is not
    // the owner who can push to OSSRH through maven. Set
    // default values so he can still build the library locally.
    project.ext.ossrhUsername = ''
    project.ext.ossrhPassword = ''
}

If these aren’t defined, we will get an issue, that is the users cannot build since gradle errors that the property is not defined. The properties are usually placed in [icode]~/.gradle/gradle.properties[/icode] file. Now finally include the config sections for maven and signing plugins.


signing {
    // Only sign the archives if there is a username. This allows one to build the library
    // on systems with no username set.
    if (ossrhUsername != '')
        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: project.ext.ossrhUsername, password: project.ext.ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: project.ext.ossrhUsername, password: project.ext.ossrhPassword)
            }

            pom.project {
                name 'WebGL4J'
                packaging 'jar'
                description 'A WebGL wrapper for the GWT platform using Java'
                url 'http://goharsha.com/WebGL4J'

                scm {
                    connection 'scm:git:git@github.com:sriharshachilakapati/WebGL4J.git'
                    developerConnection 'scm:git:git@github.com:sriharshachilakapati/WebGL4J.git'
                    url 'git@github.com:sriharshachilakapati/WebGL4J.git'
                }

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

                developers {
                    developer {
                        id 'sriharshachilakapati'
                        name 'Sri Harsha Chilakapati'
                        email 'sriharshachilakapati@gmail.com'
                    }
                }
            }
        }
    }
}

I’m not going to explain how to generate keys here, but once you did all this (also change my name and e-mails in the above script with yours! Don’t forget!!) you can just trigger your clean build and upload with the following command.


$ ./gradlew clean build javadoc uploadArchives

If your version is not a SNAPSHOT, then it will create a staging repository which you have to release from the sonatype website manually. The sync to central takes 10 minutes, but in search it will take upto 2 hours to show up.

Hope this helps.